mintymike
mintymike

Reputation: 27

How does pass_thread_control work in Facebook Handover Protocol?

I'm trying to test the pass_thread_control function on Facebook Messenger, to have my Dialogflow bot direct ongoing conversation to a human operator. So far I'm stuck at even trying to get a "success" code in Graph API Explorer. I have reviewed Facebook's documentation ( https://developers.facebook.com/docs/messenger-platform/reference/handover-protocol/pass-thread-control/ ), carefully looked through different threads here or elsewhere. I have:

  1. Subscribed my Facebook Page to receive messaging_handovers.
  2. Set the Dialogflow chatbot app as the Primary Receiver.
  3. Set the Page Inbox as Secondary Receiver.

...And I keep on getting various errors. For example, I try this request in Graph API explorer:

POST to https://graph.facebook.com/v5.0/me/pass_thread_control

with params:

{
  "recipient": {
    "id": "myPageID"
  },
  "target_app_id": "263902037430900"
}

{
  "error": {
    "message": "Unsupported post request. Object with ID 'me' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api",
    "type": "GraphMethodException",
    "code": 100,
    "error_subcode": 33,
    "fbtrace_id": "AipGijCLKQOwOl6L792ZEgG"
  }
}

Maybe the issue is the recipient PSID? This is the only parameter I have no idea where to get. What is the page scoped app-id? How do I get it?

Or maybe I missed some permissions...?

Any help getting me unstuck much appreciated...

Upvotes: 0

Views: 705

Answers (1)

mintymike
mintymike

Reputation: 27

Okay, I actually managed to figure it out.

  1. First and foremost - I discovered the Page I was trying to pass the thread control to wasn't linked to my Facebook Business account and the chatbot app was. I added the page in Facebook Business Manager so that it's linked to the same business account as the chatbot app. NOTE: I am not sure that this is a prereq for everyone, so take caution. It might not be required in all scenarios.

  2. To retrieve the Page PSID, which can then be used in the 'recipient' param of the POST request to pass_thread_control, Sent a GET request using Graph API Explorer as shown here: https://developers.facebook.com/docs/facebook-login/connecting-accounts#examples

  3. Even though the example request does not contain the appsecret_proof param, I used it and haven't tested the request without it. A very simple way of generating appsecret_proof using PHP is shown here: https://developers.facebook.com/docs/graph-api/securing-requests#generate-proof

  4. Then when providing the PSID obtained using method shown in point 2., I got "success:true" while testing the pass_thread_control, which did pass the thread control to Secondary Receiver = Page Inbox. Yaaay! 😊

All of the above is described also in this thread, which helped me figure it out, so credit to Sunil: Is Facebook Messenger PSID PageScope constant for User

Upvotes: 1

Related Questions