1
0
mirror of synced 2024-11-25 14:26:09 +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
// Mux is shared mux between several Messenger objects
Mux *http.ServeMux
// SendAPIVersion is a Send API version
SendAPIVersion string
}
// 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)
verify bool
appSecret string
sendAPIVersion string
}
// New creates a new Messenger. You pass in Options in order to affect settings.
@ -99,12 +102,17 @@ func New(mo Options) *Messenger {
token: mo.Token,
verify: mo.Verify,
appSecret: mo.AppSecret,
sendAPIVersion: mo.SendAPIVersion,
}
if mo.WebhookURL == "" {
mo.WebhookURL = "/"
}
if m.sendAPIVersion == "" {
m.sendAPIVersion = DefaultSendAPIVersion
}
m.verifyHandler = newVerifyHandler(mo.VerifyToken)
m.mux.HandleFunc(mo.WebhookURL, m.handle)
@ -368,6 +376,7 @@ func (m *Messenger) dispatch(r Receive) {
resp := &Response{
to: Recipient{info.Sender.ID},
token: m.token,
sendAPIVersion: m.sendAPIVersion,
}
switch a {
@ -429,6 +438,7 @@ func (m *Messenger) Response(to int64) *Response {
return &Response{
to: Recipient{to},
token: m.token,
sendAPIVersion: m.sendAPIVersion,
}
}
@ -442,6 +452,7 @@ func (m *Messenger) SendGeneralMessage(to Recipient, elements *[]StructuredMessa
r := &Response{
token: m.token,
to: to,
sendAPIVersion: m.sendAPIVersion,
}
return r.GenericTemplate(elements, messagingType, metadata, tags...)
}
@ -451,6 +462,7 @@ func (m *Messenger) SendWithReplies(to Recipient, message string, replies []Quic
response := &Response{
token: m.token,
to: to,
sendAPIVersion: m.sendAPIVersion,
}
return response.TextWithReplies(message, replies, messagingType, metadata, tags...)
@ -461,6 +473,7 @@ func (m *Messenger) Attachment(to Recipient, dataType AttachmentType, url string
response := &Response{
token: m.token,
to: to,
sendAPIVersion: m.sendAPIVersion,
}
return response.Attachment(dataType, url, messagingType, metadata, tags...)

View File

@ -23,10 +23,12 @@ type TopElementStyle string
type ImageAspectRatio string
const (
// DefaultSendAPIVersion is a default Send API version
DefaultSendAPIVersion = "v2.11"
// 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 = "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 = 263902037430900
@ -112,6 +114,7 @@ func getFacebookQueryResponse(r io.Reader) (QueryResponse, error) {
type Response struct {
token string
to Recipient
sendAPIVersion string
}
// 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("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 {
return qr, err
}
@ -365,7 +368,7 @@ func (r *Response) DispatchMessage(m interface{}) (QueryResponse, error) {
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 {
return res, err
}
@ -397,7 +400,7 @@ func (r *Response) PassThreadToInbox() error {
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 {
return err
}