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.
@ -95,16 +98,21 @@ func New(mo Options) *Messenger {
}
m := &Messenger{
mux: mo.Mux,
token: mo.Token,
verify: mo.Verify,
appSecret: mo.AppSecret,
mux: mo.Mux,
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)
@ -366,8 +374,9 @@ func (m *Messenger) dispatch(r Receive) {
}
resp := &Response{
to: Recipient{info.Sender.ID},
token: m.token,
to: Recipient{info.Sender.ID},
token: m.token,
sendAPIVersion: m.sendAPIVersion,
}
switch a {
@ -427,8 +436,9 @@ func (m *Messenger) dispatch(r Receive) {
// Response returns new Response object.
func (m *Messenger) Response(to int64) *Response {
return &Response{
to: Recipient{to},
token: m.token,
to: Recipient{to},
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.
func (m *Messenger) SendGeneralMessage(to Recipient, elements *[]StructuredMessageElement, messagingType MessagingType, metadata string, tags ...string) (QueryResponse, error) {
r := &Response{
token: m.token,
to: to,
token: m.token,
to: to,
sendAPIVersion: m.sendAPIVersion,
}
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.
func (m *Messenger) SendWithReplies(to Recipient, message string, replies []QuickReply, messagingType MessagingType, metadata string, tags ...string) (QueryResponse, error) {
response := &Response{
token: m.token,
to: to,
token: m.token,
to: to,
sendAPIVersion: m.sendAPIVersion,
}
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.
func (m *Messenger) Attachment(to Recipient, dataType AttachmentType, url string, messagingType MessagingType, metadata string, tags ...string) (QueryResponse, error) {
response := &Response{
token: m.token,
to: to,
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
@ -110,8 +112,9 @@ func getFacebookQueryResponse(r io.Reader) (QueryResponse, error) {
// Response is used for responding to events with messages.
type Response struct {
token string
to Recipient
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
}