From 74b5cbe4acbbbd0c1cf50fdae7575927df15fb30 Mon Sep 17 00:00:00 2001 From: Wilson Wang Date: Wed, 15 Mar 2017 10:29:26 -0700 Subject: [PATCH] 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 --- messenger.go | 9 +++++++++ response.go | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/messenger.go b/messenger.go index a763ec0..18128cf 100644 --- a/messenger.go +++ b/messenger.go @@ -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{ diff --git a/response.go b/response.go index 61e095d..e3f23e5 100644 --- a/response.go +++ b/response.go @@ -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.