2016-04-13 10:01:42 +03:00
|
|
|
package messenger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2016-04-16 11:01:32 +03:00
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/jpeg"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
2016-04-13 10:01:42 +03:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2016-10-09 06:28:21 +03:00
|
|
|
// AttachmentType is attachment type.
|
|
|
|
type AttachmentType string
|
|
|
|
|
2016-04-13 10:01:42 +03:00
|
|
|
const (
|
2016-04-14 03:23:45 +03:00
|
|
|
// SendMessageURL is API endpoint for sending messages.
|
2016-04-13 10:01:42 +03:00
|
|
|
SendMessageURL = "https://graph.facebook.com/v2.6/me/messages"
|
2016-10-09 06:28:21 +03:00
|
|
|
|
|
|
|
// 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"
|
2016-04-13 10:01:42 +03:00
|
|
|
)
|
|
|
|
|
2016-10-07 16:28:27 +03:00
|
|
|
// QueryResponse is the response sent back by Facebook when setting up things
|
|
|
|
// like greetings or call-to-actions
|
|
|
|
type QueryResponse struct {
|
|
|
|
Error *QueryError `json:"error,omitempty"`
|
|
|
|
Result string `json:"result,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryError is representing an error sent back by Facebook
|
|
|
|
type QueryError struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Code int `json:"code"`
|
|
|
|
FBTraceID string `json:"fbtrace_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkFacebookError(r io.Reader) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
qr := QueryResponse{}
|
|
|
|
err = json.NewDecoder(r).Decode(&qr)
|
|
|
|
if qr.Error != nil {
|
|
|
|
err = fmt.Errorf("Facebook error : %s", qr.Error.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-14 03:23:45 +03:00
|
|
|
// Response is used for responding to events with messages.
|
2016-04-13 10:01:42 +03:00
|
|
|
type Response struct {
|
|
|
|
token string
|
|
|
|
to Recipient
|
|
|
|
}
|
|
|
|
|
2016-04-14 03:23:45 +03:00
|
|
|
// Text sends a textual message.
|
2016-04-13 10:01:42 +03:00
|
|
|
func (r *Response) Text(message string) error {
|
2016-07-08 16:49:35 +03:00
|
|
|
return r.TextWithReplies(message, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextWithReplies sends a textual message with some replies
|
|
|
|
func (r *Response) TextWithReplies(message string, replies []QuickReply) error {
|
2016-04-13 10:01:42 +03:00
|
|
|
m := SendMessage{
|
|
|
|
Recipient: r.to,
|
|
|
|
Message: MessageData{
|
2016-07-08 16:49:35 +03:00
|
|
|
Text: message,
|
|
|
|
QuickReplies: replies,
|
2016-04-13 10:01:42 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
2016-09-08 10:55:48 +03:00
|
|
|
return err
|
2016-04-13 10:01:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-16 11:01:32 +03:00
|
|
|
// Image sends an image.
|
|
|
|
func (r *Response) Image(im image.Image) error {
|
|
|
|
imageBytes := new(bytes.Buffer)
|
2016-11-11 06:28:32 +03:00
|
|
|
err := jpeg.Encode(imageBytes, im, nil)
|
2016-04-16 11:01:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-11 06:28:32 +03:00
|
|
|
return r.AttachmentData(ImageAttachment, "meme.jpg", imageBytes)
|
2016-04-16 11:01:32 +03:00
|
|
|
}
|
|
|
|
|
2016-10-09 06:28:21 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2016-11-11 06:28:32 +03:00
|
|
|
// AttachmentData sends an image, sound, video or a regular file to a chat via an io.Reader.
|
|
|
|
func (r *Response) AttachmentData(dataType AttachmentType, filename string, filedata io.Reader) error {
|
|
|
|
var b bytes.Buffer
|
|
|
|
w := multipart.NewWriter(&b)
|
|
|
|
|
|
|
|
data, err := w.CreateFormFile("filedata", filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(data, filedata)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteField("recipient", fmt.Sprintf(`{"id":"%v"}`, r.to.ID))
|
|
|
|
w.WriteField("message", fmt.Sprintf(`{"attachment":{"type":"%v", "payload":{}}}`, dataType))
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", SendMessageURL, &b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.URL.RawQuery = "access_token=" + r.token
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", w.FormDataContentType())
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var res bytes.Buffer
|
|
|
|
res.ReadFrom(resp.Body)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-04 11:09:34 +03:00
|
|
|
// ButtonTemplate sends a message with the main contents being button elements
|
2016-05-03 16:42:25 +03:00
|
|
|
func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButton) error {
|
2016-05-04 11:09:34 +03:00
|
|
|
m := SendStructuredMessage{
|
2016-05-03 16:42:25 +03:00
|
|
|
Recipient: r.to,
|
2016-05-04 11:09:34 +03:00
|
|
|
Message: StructuredMessageData{
|
|
|
|
Attachment: StructuredMessageAttachment{
|
|
|
|
Type: "template",
|
|
|
|
Payload: StructuredMessagePayload{
|
|
|
|
TemplateType: "button",
|
|
|
|
Text: text,
|
|
|
|
Buttons: buttons,
|
|
|
|
Elements: nil,
|
|
|
|
},
|
|
|
|
},
|
2016-05-03 16:42:25 +03:00
|
|
|
},
|
|
|
|
}
|
2016-05-04 11:09:34 +03:00
|
|
|
|
2016-05-03 16:42:25 +03:00
|
|
|
data, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
2016-09-08 10:55:48 +03:00
|
|
|
return err
|
2016-05-03 16:42:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2016-10-07 16:28:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-03 16:42:25 +03:00
|
|
|
defer resp.Body.Close()
|
2016-05-04 11:09:34 +03:00
|
|
|
|
2016-10-07 16:28:27 +03:00
|
|
|
return checkFacebookError(resp.Body)
|
2016-05-03 16:42:25 +03:00
|
|
|
}
|
|
|
|
|
2016-05-04 11:09:34 +03:00
|
|
|
// GenericTemplate is a message which allows for structural elements to be sent
|
2016-09-08 10:55:48 +03:00
|
|
|
func (r *Response) GenericTemplate(elements *[]StructuredMessageElement) error {
|
2016-05-04 11:09:34 +03:00
|
|
|
m := SendStructuredMessage{
|
2016-05-03 16:42:25 +03:00
|
|
|
Recipient: r.to,
|
2016-05-04 11:09:34 +03:00
|
|
|
Message: StructuredMessageData{
|
|
|
|
Attachment: StructuredMessageAttachment{
|
|
|
|
Type: "template",
|
|
|
|
Payload: StructuredMessagePayload{
|
|
|
|
TemplateType: "generic",
|
|
|
|
Buttons: nil,
|
|
|
|
Elements: elements,
|
|
|
|
},
|
|
|
|
},
|
2016-05-03 16:42:25 +03:00
|
|
|
},
|
|
|
|
}
|
2016-05-04 11:09:34 +03:00
|
|
|
|
2016-05-03 16:42:25 +03:00
|
|
|
data, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
2016-09-08 10:55:48 +03:00
|
|
|
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)
|
2016-10-07 16:28:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-08 10:55:48 +03:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-10-07 16:28:27 +03:00
|
|
|
return checkFacebookError(resp.Body)
|
2016-09-08 10:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SenderAction sends a info about sender action
|
|
|
|
func (r *Response) SenderAction(action string) error {
|
|
|
|
m := SendSenderAction{
|
|
|
|
Recipient: r.to,
|
|
|
|
SenderAction: action,
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-05-03 16:42:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2016-10-07 16:28:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-03 16:42:25 +03:00
|
|
|
defer resp.Body.Close()
|
2016-05-04 11:09:34 +03:00
|
|
|
|
2016-10-07 16:28:27 +03:00
|
|
|
return checkFacebookError(resp.Body)
|
2016-05-03 16:42:25 +03:00
|
|
|
}
|
|
|
|
|
2016-04-14 03:23:45 +03:00
|
|
|
// SendMessage is the information sent in an API request to Facebook.
|
2016-04-13 10:01:42 +03:00
|
|
|
type SendMessage struct {
|
|
|
|
Recipient Recipient `json:"recipient"`
|
|
|
|
Message MessageData `json:"message"`
|
|
|
|
}
|
|
|
|
|
2016-07-08 16:49:35 +03:00
|
|
|
// MessageData is a text message with optional replies to be sent.
|
2016-04-13 10:01:42 +03:00
|
|
|
type MessageData struct {
|
2016-07-08 16:49:35 +03:00
|
|
|
Text string `json:"text,omitempty"`
|
|
|
|
QuickReplies []QuickReply `json:"quick_replies,omitempty"`
|
2016-04-13 10:01:42 +03:00
|
|
|
}
|
2016-05-03 16:42:25 +03:00
|
|
|
|
2016-05-04 11:09:34 +03:00
|
|
|
// SendStructuredMessage is a structured message template.
|
2016-05-03 16:42:25 +03:00
|
|
|
type SendStructuredMessage struct {
|
2016-05-04 11:09:34 +03:00
|
|
|
Recipient Recipient `json:"recipient"`
|
2016-05-03 16:42:25 +03:00
|
|
|
Message StructuredMessageData `json:"message"`
|
|
|
|
}
|
|
|
|
|
2016-05-04 11:09:34 +03:00
|
|
|
// StructuredMessageData is an attachment sent with a structured message.
|
2016-05-03 16:42:25 +03:00
|
|
|
type StructuredMessageData struct {
|
|
|
|
Attachment StructuredMessageAttachment `json:"attachment"`
|
|
|
|
}
|
|
|
|
|
2016-05-04 11:09:34 +03:00
|
|
|
// StructuredMessageAttachment is the attachment of a structured message.
|
2016-05-03 16:42:25 +03:00
|
|
|
type StructuredMessageAttachment struct {
|
2016-05-04 11:09:34 +03:00
|
|
|
// Type must be template
|
2016-10-09 06:28:21 +03:00
|
|
|
Type AttachmentType `json:"type"`
|
2016-05-03 16:42:25 +03:00
|
|
|
// Payload is the information for the file which was sent in the attachment.
|
|
|
|
Payload StructuredMessagePayload `json:"payload"`
|
|
|
|
}
|
|
|
|
|
2016-05-04 11:09:34 +03:00
|
|
|
// StructuredMessagePayload is the actual payload of an attachment
|
2016-05-03 16:42:25 +03:00
|
|
|
type StructuredMessagePayload struct {
|
2016-05-04 11:09:34 +03:00
|
|
|
// TemplateType must be button, generic or receipt
|
2016-09-08 10:55:48 +03:00
|
|
|
TemplateType string `json:"template_type,omitempty"`
|
2016-05-04 11:09:34 +03:00
|
|
|
Text string `json:"text,omitempty"`
|
|
|
|
Elements *[]StructuredMessageElement `json:"elements,omitempty"`
|
|
|
|
Buttons *[]StructuredMessageButton `json:"buttons,omitempty"`
|
2016-09-08 10:55:48 +03:00
|
|
|
Url string `json:"url,omitempty"`
|
2016-05-03 16:42:25 +03:00
|
|
|
}
|
|
|
|
|
2016-05-04 11:09:34 +03:00
|
|
|
// StructuredMessageElement is a response containing structural elements
|
2016-05-03 16:42:25 +03:00
|
|
|
type StructuredMessageElement struct {
|
2016-05-04 11:09:34 +03:00
|
|
|
Title string `json:"title"`
|
|
|
|
ImageURL string `json:"image_url"`
|
2016-09-08 10:55:48 +03:00
|
|
|
ItemURL string `json:"item_url"`
|
2016-05-04 11:09:34 +03:00
|
|
|
Subtitle string `json:"subtitle"`
|
|
|
|
Buttons []StructuredMessageButton `json:"buttons"`
|
2016-05-03 16:42:25 +03:00
|
|
|
}
|
|
|
|
|
2016-05-04 11:09:34 +03:00
|
|
|
// StructuredMessageButton is a response containing buttons
|
2016-05-03 16:42:25 +03:00
|
|
|
type StructuredMessageButton struct {
|
2016-09-08 10:55:48 +03:00
|
|
|
Type string `json:"type"`
|
|
|
|
URL string `json:"url,omitempty"`
|
2017-02-06 19:05:01 +03:00
|
|
|
Title string `json:"title,omitempty"`
|
2016-09-08 10:55:48 +03:00
|
|
|
Payload string `json:"payload,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendSenderAction is the information about sender action
|
|
|
|
type SendSenderAction struct {
|
|
|
|
Recipient Recipient `json:"recipient"`
|
|
|
|
SenderAction string `json:"sender_action"`
|
2016-05-03 16:42:25 +03:00
|
|
|
}
|