added messaging_type for FB send API request (#37)
* added messaging_type for FB send API request * added note in readme
This commit is contained in:
parent
2a76034a05
commit
525ba1bedc
@ -15,6 +15,7 @@ This is a Go library for making bots to be used on Facebook messenger. It is bui
|
|||||||
`paked/messenger` is a pretty stable library however, changes will be made which might break backwards compatibility. For the convenience of its users, these are documented here.
|
`paked/messenger` is a pretty stable library however, changes will be made which might break backwards compatibility. For the convenience of its users, these are documented here.
|
||||||
|
|
||||||
|
|
||||||
|
- 06/2/18: Added messaging_type field for message send API request as it is required by FB
|
||||||
- [23/1/17](https://github.com/paked/messenger/commit/1145fe35249f8ce14d3c0a52544e4a4babdc15a4): Updating timezone type to `float64` in profile struct
|
- [23/1/17](https://github.com/paked/messenger/commit/1145fe35249f8ce14d3c0a52544e4a4babdc15a4): Updating timezone type to `float64` in profile struct
|
||||||
- [12/9/16](https://github.com/paked/messenger/commit/47f193fc858e2d710c061e88b12dbd804a399e57): Removing unused parameter `text string` from function `(r *Response) GenericTemplate`.
|
- [12/9/16](https://github.com/paked/messenger/commit/47f193fc858e2d710c061e88b12dbd804a399e57): Removing unused parameter `text string` from function `(r *Response) GenericTemplate`.
|
||||||
- [20/5/16](https://github.com/paked/messenger/commit/1dc4bcc67dec50e2f58436ffbc7d61ca9da5b943): Leaving the `WebhookURL` field blank in `Options` will yield a URL of "/" instead of a panic.
|
- [20/5/16](https://github.com/paked/messenger/commit/1dc4bcc67dec50e2f58436ffbc7d61ca9da5b943): Leaving the `WebhookURL` field blank in `Options` will yield a URL of "/" instead of a panic.
|
||||||
|
16
messenger.go
16
messenger.go
@ -384,37 +384,37 @@ func (m *Messenger) Response(to int64) *Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send will send a textual message to a user. This user must have previously initiated a conversation with the bot.
|
// Send will send a textual message to a user. This user must have previously initiated a conversation with the bot.
|
||||||
func (m *Messenger) Send(to Recipient, message string) error {
|
func (m *Messenger) Send(to Recipient, message string, messagingType MessagingType, tags ...string) error {
|
||||||
return m.SendWithReplies(to, message, nil)
|
return m.SendWithReplies(to, message, nil, messagingType, tags...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendGeneralMessage will send the GenericTemplate message
|
// SendGeneralMessage will send the GenericTemplate message
|
||||||
func (m *Messenger) SendGeneralMessage(to Recipient, elements *[]StructuredMessageElement) error {
|
func (m *Messenger) SendGeneralMessage(to Recipient, elements *[]StructuredMessageElement, messagingType MessagingType, tags ...string) error {
|
||||||
r := &Response{
|
r := &Response{
|
||||||
token: m.token,
|
token: m.token,
|
||||||
to: to,
|
to: to,
|
||||||
}
|
}
|
||||||
return r.GenericTemplate(elements)
|
return r.GenericTemplate(elements, messagingType, tags...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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, messagingType MessagingType, tags ...string) error {
|
||||||
response := &Response{
|
response := &Response{
|
||||||
token: m.token,
|
token: m.token,
|
||||||
to: to,
|
to: to,
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.TextWithReplies(message, replies)
|
return response.TextWithReplies(message, replies, messagingType, tags...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attachment sends an image, sound, video or a regular file to a given recipient.
|
// 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 {
|
func (m *Messenger) Attachment(to Recipient, dataType AttachmentType, url string, messagingType MessagingType, tags ...string) error {
|
||||||
response := &Response{
|
response := &Response{
|
||||||
token: m.token,
|
token: m.token,
|
||||||
to: to,
|
to: to,
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.Attachment(dataType, url)
|
return response.Attachment(dataType, url, messagingType, tags...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// classify determines what type of message a webhook event is.
|
// classify determines what type of message a webhook event is.
|
||||||
|
67
response.go
67
response.go
@ -16,10 +16,11 @@ import (
|
|||||||
|
|
||||||
// AttachmentType is attachment type.
|
// AttachmentType is attachment type.
|
||||||
type AttachmentType string
|
type AttachmentType string
|
||||||
|
type MessagingType 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.11/me/messages"
|
||||||
|
|
||||||
// ImageAttachment is image attachment type.
|
// ImageAttachment is image attachment type.
|
||||||
ImageAttachment AttachmentType = "image"
|
ImageAttachment AttachmentType = "image"
|
||||||
@ -29,6 +30,15 @@ const (
|
|||||||
VideoAttachment AttachmentType = "video"
|
VideoAttachment AttachmentType = "video"
|
||||||
// FileAttachment is file attachment type.
|
// FileAttachment is file attachment type.
|
||||||
FileAttachment AttachmentType = "file"
|
FileAttachment AttachmentType = "file"
|
||||||
|
|
||||||
|
// ResponseType is response messaging type
|
||||||
|
ResponseType MessagingType = "RESPONSE"
|
||||||
|
// UpdateType is update messaging type
|
||||||
|
UpdateType MessagingType = "UPDATE"
|
||||||
|
// MessageTagType is message_tag messaging type
|
||||||
|
MessageTagType MessagingType = "MESSAGE_TAG"
|
||||||
|
// NonPromotionalSubscriptionType is NON_PROMOTIONAL_SUBSCRIPTION messaging type
|
||||||
|
NonPromotionalSubscriptionType MessagingType = "NON_PROMOTIONAL_SUBSCRIPTION"
|
||||||
)
|
)
|
||||||
|
|
||||||
// QueryResponse is the response sent back by Facebook when setting up things
|
// QueryResponse is the response sent back by Facebook when setting up things
|
||||||
@ -66,31 +76,47 @@ type Response struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Text sends a textual message.
|
// Text sends a textual message.
|
||||||
func (r *Response) Text(message string) error {
|
func (r *Response) Text(message string, messagingType MessagingType, tags ...string) error {
|
||||||
return r.TextWithReplies(message, nil)
|
return r.TextWithReplies(message, nil, messagingType, tags...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TextWithReplies sends a textual message with some replies
|
// TextWithReplies sends a textual message with some replies
|
||||||
func (r *Response) TextWithReplies(message string, replies []QuickReply) error {
|
// messagingType should be one of the following: "RESPONSE","UPDATE","MESSAGE_TAG","NON_PROMOTIONAL_SUBSCRIPTION"
|
||||||
|
// only supply tags when messagingType == "MESSAGE_TAG" (see https://developers.facebook.com/docs/messenger-platform/send-messages#messaging_types for more)
|
||||||
|
func (r *Response) TextWithReplies(message string, replies []QuickReply, messagingType MessagingType, tags ...string) error {
|
||||||
|
var tag string
|
||||||
|
if len(tags) > 0 {
|
||||||
|
tag = tags[0]
|
||||||
|
}
|
||||||
|
|
||||||
m := SendMessage{
|
m := SendMessage{
|
||||||
|
MessagingType: messagingType,
|
||||||
Recipient: r.to,
|
Recipient: r.to,
|
||||||
Message: MessageData{
|
Message: MessageData{
|
||||||
Text: message,
|
Text: message,
|
||||||
Attachment: nil,
|
Attachment: nil,
|
||||||
QuickReplies: replies,
|
QuickReplies: replies,
|
||||||
},
|
},
|
||||||
|
Tag: tag,
|
||||||
}
|
}
|
||||||
return r.DispatchMessage(&m)
|
return r.DispatchMessage(&m)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AttachmentWithReplies sends a attachment message with some replies
|
// AttachmentWithReplies sends a attachment message with some replies
|
||||||
func (r *Response) AttachmentWithReplies(attachment *StructuredMessageAttachment, replies []QuickReply) error {
|
func (r *Response) AttachmentWithReplies(attachment *StructuredMessageAttachment, replies []QuickReply, messagingType MessagingType, tags ...string) error {
|
||||||
|
var tag string
|
||||||
|
if len(tags) > 0 {
|
||||||
|
tag = tags[0]
|
||||||
|
}
|
||||||
|
|
||||||
m := SendMessage{
|
m := SendMessage{
|
||||||
|
MessagingType: messagingType,
|
||||||
Recipient: r.to,
|
Recipient: r.to,
|
||||||
Message: MessageData{
|
Message: MessageData{
|
||||||
Attachment: attachment,
|
Attachment: attachment,
|
||||||
QuickReplies: replies,
|
QuickReplies: replies,
|
||||||
},
|
},
|
||||||
|
Tag: tag,
|
||||||
}
|
}
|
||||||
return r.DispatchMessage(&m)
|
return r.DispatchMessage(&m)
|
||||||
}
|
}
|
||||||
@ -107,8 +133,14 @@ func (r *Response) Image(im image.Image) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Attachment sends an image, sound, video or a regular file to a chat.
|
// Attachment sends an image, sound, video or a regular file to a chat.
|
||||||
func (r *Response) Attachment(dataType AttachmentType, url string) error {
|
func (r *Response) Attachment(dataType AttachmentType, url string, messagingType MessagingType, tags ...string) error {
|
||||||
|
var tag string
|
||||||
|
if len(tags) > 0 {
|
||||||
|
tag = tags[0]
|
||||||
|
}
|
||||||
|
|
||||||
m := SendStructuredMessage{
|
m := SendStructuredMessage{
|
||||||
|
MessagingType: messagingType,
|
||||||
Recipient: r.to,
|
Recipient: r.to,
|
||||||
Message: StructuredMessageData{
|
Message: StructuredMessageData{
|
||||||
Attachment: StructuredMessageAttachment{
|
Attachment: StructuredMessageAttachment{
|
||||||
@ -118,6 +150,7 @@ func (r *Response) Attachment(dataType AttachmentType, url string) error {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Tag: tag,
|
||||||
}
|
}
|
||||||
return r.DispatchMessage(&m)
|
return r.DispatchMessage(&m)
|
||||||
}
|
}
|
||||||
@ -184,8 +217,14 @@ func (r *Response) AttachmentData(dataType AttachmentType, filename string, file
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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, messagingType MessagingType, tags ...string) error {
|
||||||
|
var tag string
|
||||||
|
if len(tags) > 0 {
|
||||||
|
tag = tags[0]
|
||||||
|
}
|
||||||
|
|
||||||
m := SendStructuredMessage{
|
m := SendStructuredMessage{
|
||||||
|
MessagingType: messagingType,
|
||||||
Recipient: r.to,
|
Recipient: r.to,
|
||||||
Message: StructuredMessageData{
|
Message: StructuredMessageData{
|
||||||
Attachment: StructuredMessageAttachment{
|
Attachment: StructuredMessageAttachment{
|
||||||
@ -198,14 +237,21 @@ func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButto
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Tag: tag,
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.DispatchMessage(&m)
|
return r.DispatchMessage(&m)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenericTemplate is a message which allows for structural elements to be sent
|
// GenericTemplate is a message which allows for structural elements to be sent
|
||||||
func (r *Response) GenericTemplate(elements *[]StructuredMessageElement) error {
|
func (r *Response) GenericTemplate(elements *[]StructuredMessageElement, messagingType MessagingType, tags ...string) error {
|
||||||
|
var tag string
|
||||||
|
if len(tags) > 0 {
|
||||||
|
tag = tags[0]
|
||||||
|
}
|
||||||
|
|
||||||
m := SendStructuredMessage{
|
m := SendStructuredMessage{
|
||||||
|
MessagingType: messagingType,
|
||||||
Recipient: r.to,
|
Recipient: r.to,
|
||||||
Message: StructuredMessageData{
|
Message: StructuredMessageData{
|
||||||
Attachment: StructuredMessageAttachment{
|
Attachment: StructuredMessageAttachment{
|
||||||
@ -217,6 +263,7 @@ func (r *Response) GenericTemplate(elements *[]StructuredMessageElement) error {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Tag: tag,
|
||||||
}
|
}
|
||||||
return r.DispatchMessage(&m)
|
return r.DispatchMessage(&m)
|
||||||
}
|
}
|
||||||
@ -258,8 +305,10 @@ func (r *Response) DispatchMessage(m interface{}) error {
|
|||||||
|
|
||||||
// SendMessage is the information sent in an API request to Facebook.
|
// SendMessage is the information sent in an API request to Facebook.
|
||||||
type SendMessage struct {
|
type SendMessage struct {
|
||||||
|
MessagingType MessagingType `json:"messaging_type"`
|
||||||
Recipient Recipient `json:"recipient"`
|
Recipient Recipient `json:"recipient"`
|
||||||
Message MessageData `json:"message"`
|
Message MessageData `json:"message"`
|
||||||
|
Tag string `json:"tag,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MessageData is a message consisting of text or an attachment, with an additional selection of optional quick replies.
|
// MessageData is a message consisting of text or an attachment, with an additional selection of optional quick replies.
|
||||||
@ -271,8 +320,10 @@ type MessageData struct {
|
|||||||
|
|
||||||
// SendStructuredMessage is a structured message template.
|
// SendStructuredMessage is a structured message template.
|
||||||
type SendStructuredMessage struct {
|
type SendStructuredMessage struct {
|
||||||
|
MessagingType MessagingType `json:"messaging_type"`
|
||||||
Recipient Recipient `json:"recipient"`
|
Recipient Recipient `json:"recipient"`
|
||||||
Message StructuredMessageData `json:"message"`
|
Message StructuredMessageData `json:"message"`
|
||||||
|
Tag string `json:"tag,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// StructuredMessageData is an attachment sent with a structured message.
|
// StructuredMessageData is an attachment sent with a structured message.
|
||||||
|
Loading…
Reference in New Issue
Block a user