Add support for sending attachments by a URL (#15)
This commit is contained in:
parent
6970a1b09a
commit
39df036128
10
messenger.go
10
messenger.go
@ -305,6 +305,16 @@ func (m *Messenger) SendWithReplies(to Recipient, message string, replies []Quic
|
|||||||
return response.TextWithReplies(message, replies)
|
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.
|
// classify determines what type of message a webhook event is.
|
||||||
func (m *Messenger) classify(info MessageInfo, e Entry) Action {
|
func (m *Messenger) classify(info MessageInfo, e Entry) Action {
|
||||||
if info.Message != nil {
|
if info.Message != nil {
|
||||||
|
49
response.go
49
response.go
@ -11,9 +11,21 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AttachmentType is attachment type.
|
||||||
|
type AttachmentType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// SendMessageURL is API endpoint for sending messages.
|
// SendMessageURL is API endpoint for sending messages.
|
||||||
SendMessageURL = "https://graph.facebook.com/v2.6/me/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
|
// 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
|
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
|
// ButtonTemplate sends a message with the main contents being button elements
|
||||||
func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButton) error {
|
func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButton) error {
|
||||||
m := SendStructuredMessage{
|
m := SendStructuredMessage{
|
||||||
@ -268,7 +315,7 @@ type StructuredMessageData struct {
|
|||||||
// StructuredMessageAttachment is the attachment of a structured message.
|
// StructuredMessageAttachment is the attachment of a structured message.
|
||||||
type StructuredMessageAttachment struct {
|
type StructuredMessageAttachment struct {
|
||||||
// Type must be template
|
// 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 is the information for the file which was sent in the attachment.
|
||||||
Payload StructuredMessagePayload `json:"payload"`
|
Payload StructuredMessagePayload `json:"payload"`
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user