From 241e864fb315205cfd0816fc35258f88dd77fb65 Mon Sep 17 00:00:00 2001 From: Daniel Fischer Date: Mon, 25 Mar 2019 11:42:34 -0700 Subject: [PATCH] Support for passThreadControl --- .gitignore | 2 ++ pass_thread_control.go | 19 +++++++++++++++++++ response.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 pass_thread_control.go diff --git a/.gitignore b/.gitignore index ac5a2b7..0ca33b0 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ _testmain.go # Configuration cmd/bot/config.json + +.idea diff --git a/pass_thread_control.go b/pass_thread_control.go new file mode 100644 index 0000000..ca072f4 --- /dev/null +++ b/pass_thread_control.go @@ -0,0 +1,19 @@ +package messenger + +import "encoding/json" + +func unmarshalPassThreadControl(data []byte) (passThreadControl, error) { + var r passThreadControl + err := json.Unmarshal(data, &r) + return r, err +} + +func (r *passThreadControl) marshal() ([]byte, error) { + return json.Marshal(r) +} + +type passThreadControl struct { + Recipient Recipient `json:"recipient"` + TargetAppID int64 `json:"target_app_id"` + Metadata string `json:"metadata"` +} diff --git a/response.go b/response.go index 071d7de..6c4bbb2 100644 --- a/response.go +++ b/response.go @@ -23,6 +23,10 @@ type ImageAspectRatio string const ( // SendMessageURL is API endpoint for sending messages. SendMessageURL = "https://graph.facebook.com/v2.11/me/messages" + // ThreadControlURL is the API endpoint for passing thread control. + ThreadControlURL = "https://graph.facebook.com/v2.6/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 // ImageAttachment is image attachment type. ImageAttachment AttachmentType = "image" @@ -320,6 +324,37 @@ func (r *Response) DispatchMessage(m interface{}) error { return checkFacebookError(resp.Body) } +// PassThreadToInbox Uses Messenger Handover Protocol for live inbox +// https://developers.facebook.com/docs/messenger-platform/handover-protocol/#inbox +func (r *Response) PassThreadToInbox() error { + p := passThreadControl{ + Recipient: r.to, + TargetAppID: InboxPageID, + Metadata: "Passing to inbox secondary app", + } + + data, err := json.Marshal(p) + if err != nil { + return err + } + + req, err := http.NewRequest("POST", ThreadControlURL, bytes.NewBuffer(data)) + if err != nil { + return err + } + + req.Header.Set("Content-Type", "application/json") + req.URL.RawQuery = "access_token=" + r.token + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + return checkFacebookError(resp.Body) +} + // SendMessage is the information sent in an API request to Facebook. type SendMessage struct { MessagingType MessagingType `json:"messaging_type"`