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

Add support for sending attachments by a URL (#15)

This commit is contained in:
Bo-Yi Wu 2016-10-08 22:28:21 -05:00 committed by Harrison Shoebridge
parent 6970a1b09a
commit 39df036128
2 changed files with 58 additions and 1 deletions

View File

@ -305,6 +305,16 @@ func (m *Messenger) SendWithReplies(to Recipient, message string, replies []Quic
return response.TextWithReplies(message, replies)
}
// Attachment sends an image, sound, video or a regular file to a given recipient.
func (m *Messenger) Attachment(to Recipient, dataType AttachmentType, url string) error {
response := &Response{
token: m.token,
to: to,
}
return response.Attachment(dataType, url)
}
// classify determines what type of message a webhook event is.
func (m *Messenger) classify(info MessageInfo, e Entry) Action {
if info.Message != nil {

View File

@ -11,9 +11,21 @@ import (
"net/http"
)
// AttachmentType is attachment type.
type AttachmentType string
const (
// SendMessageURL is API endpoint for sending messages.
SendMessageURL = "https://graph.facebook.com/v2.6/me/messages"
// ImageAttachment is image attachment type.
ImageAttachment AttachmentType = "image"
// AudioAttachment is audio attachment type.
AudioAttachment AttachmentType = "audio"
// VideoAttachment is video attachment type.
VideoAttachment AttachmentType = "video"
// FileAttachment is file attachment type.
FileAttachment AttachmentType = "file"
)
// QueryResponse is the response sent back by Facebook when setting up things
@ -130,6 +142,41 @@ func (r *Response) Image(im image.Image) error {
return nil
}
// Attachment sends an image, sound, video or a regular file to a chat.
func (r *Response) Attachment(dataType AttachmentType, url string) error {
m := SendStructuredMessage{
Recipient: r.to,
Message: StructuredMessageData{
Attachment: StructuredMessageAttachment{
Type: dataType,
Payload: StructuredMessagePayload{
Url: url,
},
},
},
}
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
}
// ButtonTemplate sends a message with the main contents being button elements
func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButton) error {
m := SendStructuredMessage{
@ -268,7 +315,7 @@ type StructuredMessageData struct {
// StructuredMessageAttachment is the attachment of a structured message.
type StructuredMessageAttachment struct {
// Type must be template
Type string `json:"type"`
Type AttachmentType `json:"type"`
// Payload is the information for the file which was sent in the attachment.
Payload StructuredMessagePayload `json:"payload"`
}