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

Add AttachmentWithReplies method for sending attachment together with replies. (#27)

* 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
This commit is contained in:
Wilson Wang 2017-03-15 10:29:26 -07:00 committed by Harrison Shoebridge
parent bbd304b464
commit 74b5cbe4ac
2 changed files with 45 additions and 3 deletions

View File

@ -330,6 +330,15 @@ func (m *Messenger) Send(to Recipient, message string) error {
return m.SendWithReplies(to, message, nil)
}
// SendGeneralMessage will send the GenericTemplate message
func (m *Messenger) SendGeneralMessage(to Recipient, elements *[]StructuredMessageElement) error {
r := &Response{
token: m.token,
to: to,
}
return r.GenericTemplate(elements)
}
// SendWithReplies sends a textual message to a user, but gives them the option of numerous quick response options.
func (m *Messenger) SendWithReplies(to Recipient, message string, replies []QuickReply) error {
response := &Response{

View File

@ -72,6 +72,38 @@ func (r *Response) TextWithReplies(message string, replies []QuickReply) error {
Recipient: r.to,
Message: MessageData{
Text: message,
Attachment: nil,
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
}
// AttachmentWithReplies sends a attachment message with some replies
func (r *Response) AttachmentWithReplies(attachment *StructuredMessageAttachment, replies []QuickReply) error {
m := SendMessage{
Recipient: r.to,
Message: MessageData{
Attachment: attachment,
QuickReplies: replies,
},
}
@ -299,10 +331,11 @@ type SendMessage struct {
Message MessageData `json:"message"`
}
// MessageData is a text message with optional replies to be sent.
// MessageData is a message consisting of text or an attachment, with an additional selection of optional quick replies.
type MessageData struct {
Text string `json:"text,omitempty"`
QuickReplies []QuickReply `json:"quick_replies,omitempty"`
Text string `json:"text,omitempty"`
Attachment *StructuredMessageAttachment `json:"attachment,omitempty"`
QuickReplies []QuickReply `json:"quick_replies,omitempty"`
}
// SendStructuredMessage is a structured message template.