Handing the conversation over to FB Page Inbox or to another bot

A chatbot deployed on Facebook will manage all conversations initiated through Facebook Messenger on the integrated Facebook Page.

By using the Facebook Handover Protocol, you can enhance the Messenger experience by allowing the chatbot and the Page owner or administrator to share control of the conversation. This lets you seamlessly pass control between the chatbot and the Facebook Page Inbox, enabling users to interact directly with the Page administrator when needed.

This is especially useful when you want to allow users to communicate with the Page Inbox administrator while still having a chatbot deployed.

Prerequisite

  • You need the access token you generated when connecting the Facebook page with you app.

To handover the conversation from a chatbot to a Facebook page Inbox or to another bot:

Step 1. Adjust your Facebook Page app webhook subscriptions

  1. In Facebook Developer Portal, go to your app details.
  2. On the main, click Messenger > API Settings.
  3. In the Generate access tokens section, hover the mouse over the info icon below Webhook Subscription and click the Edit button. The Edit Page Subscriptions pop-up appears.
  4. Select messaging_handovers and click Confirm.

Step 2. Assign the FB app as the Primary Receiver for your FB page

To assign the Facebook app as the Primary Receiver for your Facebook page:

  1. Access the Facebook page and go to the page settings.
  2. On the Audience and visibility menu, click Page setup.
  3. Click the View button next to Advanced Messaging.
  4. In the App settings - Handover Protocol sections, click the Configure button next to the Messenger receiver. A pop-up appears.
  5. Select your Facebook app as the primary receiver for the handover protocol.
  6. Hint:  If you have multiple bots deployed in the Facebook channel, select the Facebook app created for the specific bot you want to handle conversations by default in Messenger as the primary receiver.
  7. Close the pop-up.

Step 3. Configure the integration for passing thread control

Create an integration task with a POST request to the Facebook Handover API endpoint and place the integration for passing thread control on the flow step where you do the handover.

This section provides you with the information needed to configure the integration for passing thread control.

Prerequisite

  • Create a REST connector app, which has the https://graph.facebook.com as Base URL and in the variable section has PageAccessToken (the access token of the Facebook page where the bot was deployed).

Create a REST connector action for passing thread control and select [[ChatUser]] as Request and Response entity.

Create a REST integration task and configure the Request for passing thread control as described below.

Make a POST request to Facebook pass_thread_control endpoint: /v2.6/me/pass_thread_control?access_token={access_token}

In the Parameters tab, map the access_token parameter to DRUID local variable @PageAccessToken.

Select UseBodyTemplate as RequestType and in the BodyTemplate field, enter the following code:

Copy
{
    "recipient":{"id":"{{userId}}"},
    "target_app_id":263902037430900
}

Set the target_app_id property of the request to specific value as follows:

  • To handover the conversations to the Facebook Page Inbox, enter “263902037430900”
  • To hand over the conversations to another bot, enter the App ID of the Facebook app created for that bot.

Click the Body tab and map the userId provided in the request (BodyTemplate field) to [[ChatUser]].UserId, which will store the values returned by the API call.

Note:  The conversations are handed over from the bot to the Facebook Inbox page (or to another bot as configured in the REST API request) for 24 hours. If the conversations are handed over from the bot to the Facebook page Inbox, when the Facebook page owner marks the conversation as DONE (in the Page Inbox), it is handed back to the bot.

Place the integration for passing thread control on the flow step where you do the handover.

Step 4. (Optional) Author the bot to handle the conversation hand over back to it

Whenever the conversation is handed back to the bot, DRUID Platform triggers the Welcome flow and sets [[ChatUser]].ReturningFromFacebookHandover = true.

Important!  Set [[ChatUser]].ReturningFromFacebookHandover to false on the same step where you place the integration for passing thread control (to make sure that [[ChatUser]].ReturningFromFacebookHandover becomes true only when the conversation is returned to the bot (or to clean up any older context).

You can create a dedicated branch in the Welcome flow to address the cases when you want to let the users know that they’re talking to the bot again, a branch step that will be executed when [[ChatUser]].ReturningFromFacebookHandover = true (set condition on step).

If you don’t want to let the user know, add an empty step to the Welcome flow or simply do nothing.

Note:   If you handle this situation as a separate branch in the Welcome flow, perform a cleanup of [[ChatUser]].ReturningFromFacebookHandover for further accessing the Welcome flow from the conversation (from other areas than Facebook handover). To do the cleanup, make sure to set [[ChatUser]].ReturningFromFacebookHandover back to “false” after the branch was reached in conversation (set variable [[ChatUser]].ReturningFromFacebookHandover = false).