1
0
mirror of synced 2024-11-22 12:56:06 +03:00

Support for passThreadControl

This commit is contained in:
Daniel Fischer 2019-03-25 11:42:34 -07:00
parent faf2432c8f
commit 241e864fb3
3 changed files with 56 additions and 0 deletions

2
.gitignore vendored
View File

@ -25,3 +25,5 @@ _testmain.go
# Configuration # Configuration
cmd/bot/config.json cmd/bot/config.json
.idea

19
pass_thread_control.go Normal file
View File

@ -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"`
}

View File

@ -23,6 +23,10 @@ type ImageAspectRatio string
const ( const (
// 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/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 is image attachment type.
ImageAttachment AttachmentType = "image" ImageAttachment AttachmentType = "image"
@ -320,6 +324,37 @@ func (r *Response) DispatchMessage(m interface{}) error {
return checkFacebookError(resp.Body) 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. // SendMessage is the information sent in an API request to Facebook.
type SendMessage struct { type SendMessage struct {
MessagingType MessagingType `json:"messaging_type"` MessagingType MessagingType `json:"messaging_type"`