1
0
mirror of synced 2024-11-29 00:26:12 +03:00

ability to specify send api version

This commit is contained in:
Pavel 2021-06-04 14:42:12 +03:00
parent da2c3ee447
commit c4c6c4faa3
2 changed files with 37 additions and 21 deletions

View File

@ -48,6 +48,8 @@ type Options struct {
WebhookURL string WebhookURL string
// Mux is shared mux between several Messenger objects // Mux is shared mux between several Messenger objects
Mux *http.ServeMux Mux *http.ServeMux
// SendAPIVersion is a Send API version
SendAPIVersion string
} }
// MessageHandler is a handler used for responding to a message containing text. // MessageHandler is a handler used for responding to a message containing text.
@ -86,6 +88,7 @@ type Messenger struct {
verifyHandler func(http.ResponseWriter, *http.Request) verifyHandler func(http.ResponseWriter, *http.Request)
verify bool verify bool
appSecret string appSecret string
sendAPIVersion string
} }
// New creates a new Messenger. You pass in Options in order to affect settings. // New creates a new Messenger. You pass in Options in order to affect settings.
@ -95,16 +98,21 @@ func New(mo Options) *Messenger {
} }
m := &Messenger{ m := &Messenger{
mux: mo.Mux, mux: mo.Mux,
token: mo.Token, token: mo.Token,
verify: mo.Verify, verify: mo.Verify,
appSecret: mo.AppSecret, appSecret: mo.AppSecret,
sendAPIVersion: mo.SendAPIVersion,
} }
if mo.WebhookURL == "" { if mo.WebhookURL == "" {
mo.WebhookURL = "/" mo.WebhookURL = "/"
} }
if m.sendAPIVersion == "" {
m.sendAPIVersion = DefaultSendAPIVersion
}
m.verifyHandler = newVerifyHandler(mo.VerifyToken) m.verifyHandler = newVerifyHandler(mo.VerifyToken)
m.mux.HandleFunc(mo.WebhookURL, m.handle) m.mux.HandleFunc(mo.WebhookURL, m.handle)
@ -366,8 +374,9 @@ func (m *Messenger) dispatch(r Receive) {
} }
resp := &Response{ resp := &Response{
to: Recipient{info.Sender.ID}, to: Recipient{info.Sender.ID},
token: m.token, token: m.token,
sendAPIVersion: m.sendAPIVersion,
} }
switch a { switch a {
@ -427,8 +436,9 @@ func (m *Messenger) dispatch(r Receive) {
// Response returns new Response object. // Response returns new Response object.
func (m *Messenger) Response(to int64) *Response { func (m *Messenger) Response(to int64) *Response {
return &Response{ return &Response{
to: Recipient{to}, to: Recipient{to},
token: m.token, token: m.token,
sendAPIVersion: m.sendAPIVersion,
} }
} }
@ -440,8 +450,9 @@ func (m *Messenger) Send(to Recipient, message string, messagingType MessagingTy
// SendGeneralMessage will send the GenericTemplate message. // SendGeneralMessage will send the GenericTemplate message.
func (m *Messenger) SendGeneralMessage(to Recipient, elements *[]StructuredMessageElement, messagingType MessagingType, metadata string, tags ...string) (QueryResponse, error) { func (m *Messenger) SendGeneralMessage(to Recipient, elements *[]StructuredMessageElement, messagingType MessagingType, metadata string, tags ...string) (QueryResponse, error) {
r := &Response{ r := &Response{
token: m.token, token: m.token,
to: to, to: to,
sendAPIVersion: m.sendAPIVersion,
} }
return r.GenericTemplate(elements, messagingType, metadata, tags...) return r.GenericTemplate(elements, messagingType, metadata, tags...)
} }
@ -449,8 +460,9 @@ func (m *Messenger) SendGeneralMessage(to Recipient, elements *[]StructuredMessa
// 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, messagingType MessagingType, metadata string, tags ...string) (QueryResponse, error) { func (m *Messenger) SendWithReplies(to Recipient, message string, replies []QuickReply, messagingType MessagingType, metadata string, tags ...string) (QueryResponse, error) {
response := &Response{ response := &Response{
token: m.token, token: m.token,
to: to, to: to,
sendAPIVersion: m.sendAPIVersion,
} }
return response.TextWithReplies(message, replies, messagingType, metadata, tags...) return response.TextWithReplies(message, replies, messagingType, metadata, tags...)
@ -459,8 +471,9 @@ func (m *Messenger) SendWithReplies(to Recipient, message string, replies []Quic
// 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, messagingType MessagingType, metadata string, tags ...string) (QueryResponse, error) { func (m *Messenger) Attachment(to Recipient, dataType AttachmentType, url string, messagingType MessagingType, metadata string, tags ...string) (QueryResponse, error) {
response := &Response{ response := &Response{
token: m.token, token: m.token,
to: to, to: to,
sendAPIVersion: m.sendAPIVersion,
} }
return response.Attachment(dataType, url, messagingType, metadata, tags...) return response.Attachment(dataType, url, messagingType, metadata, tags...)

View File

@ -23,10 +23,12 @@ type TopElementStyle string
type ImageAspectRatio string type ImageAspectRatio string
const ( const (
// DefaultSendAPIVersion is a default Send API version
DefaultSendAPIVersion = "v2.11"
// SendMessageURL is API endpoint for sending messages. // SendMessageURL is API endpoint for sending messages.
SendMessageURL = "https://graph.facebook.com/v2.11/me/messages" SendMessageURL = "https://graph.facebook.com/%s/me/messages"
// ThreadControlURL is the API endpoint for passing thread control. // ThreadControlURL is the API endpoint for passing thread control.
ThreadControlURL = "https://graph.facebook.com/v2.6/me/pass_thread_control" ThreadControlURL = "https://graph.facebook.com/%s/me/pass_thread_control"
// InboxPageID is managed by facebook for secondary pass to inbox features: https://developers.facebook.com/docs/messenger-platform/handover-protocol/pass-thread-control // InboxPageID is managed by facebook for secondary pass to inbox features: https://developers.facebook.com/docs/messenger-platform/handover-protocol/pass-thread-control
InboxPageID = 263902037430900 InboxPageID = 263902037430900
@ -110,8 +112,9 @@ func getFacebookQueryResponse(r io.Reader) (QueryResponse, error) {
// Response is used for responding to events with messages. // Response is used for responding to events with messages.
type Response struct { type Response struct {
token string token string
to Recipient to Recipient
sendAPIVersion string
} }
// SetToken is for using DispatchMessage from outside. // SetToken is for using DispatchMessage from outside.
@ -249,7 +252,7 @@ func (r *Response) AttachmentData(dataType AttachmentType, filename string, file
multipartWriter.WriteField("recipient", fmt.Sprintf(`{"id":"%v"}`, r.to.ID)) multipartWriter.WriteField("recipient", fmt.Sprintf(`{"id":"%v"}`, r.to.ID))
multipartWriter.WriteField("message", fmt.Sprintf(`{"attachment":{"type":"%v", "payload":{}}}`, dataType)) multipartWriter.WriteField("message", fmt.Sprintf(`{"attachment":{"type":"%v", "payload":{}}}`, dataType))
req, err := http.NewRequest("POST", SendMessageURL, &body) req, err := http.NewRequest("POST", fmt.Sprintf(SendMessageURL, r.sendAPIVersion), &body)
if err != nil { if err != nil {
return qr, err return qr, err
} }
@ -365,7 +368,7 @@ func (r *Response) DispatchMessage(m interface{}) (QueryResponse, error) {
return res, err return res, err
} }
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data)) req, err := http.NewRequest("POST", fmt.Sprintf(SendMessageURL, r.sendAPIVersion), bytes.NewBuffer(data))
if err != nil { if err != nil {
return res, err return res, err
} }
@ -397,7 +400,7 @@ func (r *Response) PassThreadToInbox() error {
return err return err
} }
req, err := http.NewRequest("POST", ThreadControlURL, bytes.NewBuffer(data)) req, err := http.NewRequest("POST", fmt.Sprintf(ThreadControlURL, r.sendAPIVersion), bytes.NewBuffer(data))
if err != nil { if err != nil {
return err return err
} }