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:
parent
bbd304b464
commit
74b5cbe4ac
@ -330,6 +330,15 @@ func (m *Messenger) Send(to Recipient, message string) error {
|
|||||||
return m.SendWithReplies(to, message, nil)
|
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.
|
// 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 {
|
func (m *Messenger) SendWithReplies(to Recipient, message string, replies []QuickReply) error {
|
||||||
response := &Response{
|
response := &Response{
|
||||||
|
35
response.go
35
response.go
@ -72,6 +72,38 @@ func (r *Response) TextWithReplies(message string, replies []QuickReply) error {
|
|||||||
Recipient: r.to,
|
Recipient: r.to,
|
||||||
Message: MessageData{
|
Message: MessageData{
|
||||||
Text: message,
|
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,
|
QuickReplies: replies,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -299,9 +331,10 @@ type SendMessage struct {
|
|||||||
Message MessageData `json:"message"`
|
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 {
|
type MessageData struct {
|
||||||
Text string `json:"text,omitempty"`
|
Text string `json:"text,omitempty"`
|
||||||
|
Attachment *StructuredMessageAttachment `json:"attachment,omitempty"`
|
||||||
QuickReplies []QuickReply `json:"quick_replies,omitempty"`
|
QuickReplies []QuickReply `json:"quick_replies,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user