1
0
mirror of synced 2024-11-22 04:46:05 +03:00

Reuse Post Code (#28)

* add json omitempty on button Title field

Share Button only allows the Type field to be presented in the serialized data. Otherwise, you got facebook error:

"Facebook error : (#100) Param [elements][0][buttons][0][title] must be a non-empty UTF-8 encoded string"

* add attachement to senddata

* changes after pull request reviews

* add PostToMessenger method and ensure the result check in it

* change the function name
This commit is contained in:
Wilson Wang 2017-04-09 09:58:35 -07:00 committed by Harrison Shoebridge
parent 66c2f90a07
commit 64c15a121b

View File

@ -76,26 +76,7 @@ func (r *Response) TextWithReplies(message string, replies []QuickReply) error {
QuickReplies: replies,
},
}
data, err := json.Marshal(m)
if err != nil {
return err
}
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.URL.RawQuery = "access_token=" + r.token
client := &http.Client{}
resp, err := client.Do(req)
defer resp.Body.Close()
return err
return r.DispatchMessage(&m)
}
// AttachmentWithReplies sends a attachment message with some replies
@ -107,26 +88,7 @@ func (r *Response) AttachmentWithReplies(attachment *StructuredMessageAttachment
QuickReplies: replies,
},
}
data, err := json.Marshal(m)
if err != nil {
return err
}
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.URL.RawQuery = "access_token=" + r.token
client := &http.Client{}
resp, err := client.Do(req)
defer resp.Body.Close()
return err
return r.DispatchMessage(&m)
}
// Image sends an image.
@ -153,26 +115,7 @@ func (r *Response) Attachment(dataType AttachmentType, url string) error {
},
},
}
data, err := json.Marshal(m)
if err != nil {
return err
}
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.URL.RawQuery = "access_token=" + r.token
client := &http.Client{}
resp, err := client.Do(req)
defer resp.Body.Close()
return err
return r.DispatchMessage(&m)
}
// AttachmentData sends an image, sound, video or a regular file to a chat via an io.Reader.
@ -230,28 +173,7 @@ func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButto
},
}
data, err := json.Marshal(m)
if err != nil {
return err
}
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.URL.RawQuery = "access_token=" + r.token
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return checkFacebookError(resp.Body)
return r.DispatchMessage(&m)
}
// GenericTemplate is a message which allows for structural elements to be sent
@ -269,29 +191,7 @@ func (r *Response) GenericTemplate(elements *[]StructuredMessageElement) error {
},
},
}
data, err := json.Marshal(m)
if err != nil {
return err
}
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.URL.RawQuery = "access_token=" + r.token
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return checkFacebookError(resp.Body)
return r.DispatchMessage(&m)
}
// SenderAction sends a info about sender action
@ -300,7 +200,11 @@ func (r *Response) SenderAction(action string) error {
Recipient: r.to,
SenderAction: action,
}
return r.DispatchMessage(&m)
}
// DispatchMessage posts the message to messenger, return the error if there's any
func (r *Response) DispatchMessage(m interface{}) error {
data, err := json.Marshal(m)
if err != nil {
return err
@ -314,14 +218,14 @@ func (r *Response) SenderAction(action string) error {
req.Header.Set("Content-Type", "application/json")
req.URL.RawQuery = "access_token=" + r.token
client := &http.Client{}
resp, err := client.Do(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
return nil
}
return checkFacebookError(resp.Body)
}