Facebook Messenger : Handover Protocol

It’s more than a year since Facebook launched Messenger bot platform. Chatbots and chatbot development have become one of the hot areas in business as well as in technology ever since. Combined with Natural Language Processing and some Machine Learning capabilities a chatbot can be very handy in several business applications. Chatbots are automated conversational agents which collects data through conversation with clients and perform some actions and give the result back to the user. One of the typical use cases is customer help desk chatbot, which can make conversation with the user and can answer customer queries.

People often think that a chatbot can replace human and can make a complete takeover. But it is not the case because chatbots are not meant to be a replacement for human labor; its purpose is to help and assist humans to provide a better result to the business. Chatbots cannot completely replace humans because there may have some unhandled cases with the user messages or the user wants to talk or chat with the human agent. In either case, there should be some mechanism that can bypass the workflow from bot to human or more specifically from one scenario to another. That is if we want our bot to have different aspects to be handled (eg: purchase products, help desk etc.) we needed more than one Facebook app to be installed on the page and the bot should be able to switch between the apps.

Handover Protocol

Messenger v2.0 introduced a new feature named “Handover Protocol” which allows different developers can collaborate to create the different scenarios and vendors and developers will be able to pass control back and forth depending on the direction the conversation is taking. The feature is currently in its beta version. We can use this feature to set two different Facebook apps for a page. The page receives messages and out of multiple apps that are subscribed to the page, one of them can be selected and used to send the reply.

How does it work?

First, create two Facebook apps and a Facebook page. Subscribe the two apps to the new page. Go to the Page Settings > Messenger Platform > Subscribed Apps > Role. Set one of the apps as the primary receiver and the other one as secondary. Setup the web hooks for both apps and test the chatbot is sending and receiving messages through the web hook.

The primary app has the default control over the thread and can handle any incoming messages. The app that has the thread control handle messages while all other apps can listen to all the messages through the standby channel. This allows all the apps to get the full context of the conversation and easily continue the conversation when it receives the thread control. Only the thread controlling app has the access to the normal messaging channel and all other apps for the page can optionally subscribe to the standby channel to listen to the conversation.

When we want to switch to the secondary app at a point in the conversation pass the thread control to the secondary app that we want to use. The primary app passes the thread control to a secondary app by making a call to the Pass Thread Control API. The secondary app gets notified this via the pass_thread_control webhook event.

The Pass Thread Control API request looks like:

curl -X POST -H "Content-Type: application/json" -d '{
"recipient":{"id":"USER_ID"},
"target_app_id":123456789
}' "https://graph.facebook.com/v2.6/me/pass_thread_control?access_token=PAGE_ACCESS_TOKEN"

The target_app_id is the app id of the secondary app to which we are passing the thread control. The PAGE_ACCESS_TOKEN is the access token of the primary app to the page. If the request is the success we get a response like :

{ "success":true }

Now the secondary app has the thread control. The secondary app handles further incoming messages. When the secondary app finishes and if we want to continue the conversation with the primary app, make another Pass Thread Control API request as:

curl -X POST -H "Content-Type: application/json" -d '{
"recipient":{"id":"USER_ID"},
}' "https://graph.facebook.com/v2.6/me/pass_thread_control?access_token=PAGE_ACCESS_TOKEN"

The primary receiver application can get a full list of the secondary apps on the page that are currently active at any time by making an API request to Secondary Receivers List API. This allows selecting one of the suitable active secondary apps easily.

curl -X GET "https://graph.facebook.com/v2.6/me/secondary_receivers?  fields=id,name&access_token=PAGE_ACCESS_TOKEN_OF_PRIMARY_APP"

The response will look like :

{
"data":[
{
"id":"12345678910",
"name":"David's Composer"
},
{
"id":"23456789101",
"name":"Messenger Rocks"
}
]
}

Thus this new feature provides a better way to manage different scenarios with respect to incoming messages. Handover Protocol is in closed beta currently. We can hope for the announcement of this exciting feature soon.

Onchatbot is a chatbot development agency from India. We develop chatbots for e-commerce, real estate, restaurants, pizza stores, colleges, universities etc. You can read more about us here https://www.onchatbot.com/
Author
Jyothish G (Chatbot Developer @ Onchatbot.com)
https://www.linkedin.com/in/jyothish-g-a8430576/