Whether or not you are in favor of chatbots or not, in many ways they can be game changers. That is a bigger topic on its own right, in this very post I will just spend some words on writing about observations made and how to utilize  Facebook Messenger chatbots. So first things first, of course the official API documentation is the first great entry point. With version 1.2 some very interesting features around payment were introduced and since the whole platform is in flux a lot more will certainly come soon.

So basically you need a Facebook page and wire it up with a custom endpoint that features webhooks Facebook will (extensively) use. All very well explained here. After the page is wired up that webhook of yours will receive events like this:

{"sender":{"id":"925740457553273"},"recipient":{"id":"1387842364565702"},"timestamp":1472298240493,"message":{"mid":"mid.1472298240485:5db7a3720a27f7b785","seq":61,"text":"bla bla bla toller chat"}}

The so called “Page Access Token” is the number one token you will need to just do anything with your bot and it is directly tied to your  Facebook page. Try the Graph explorer to easily get access to: User Access Tokens, Page Access Tokens and more.

The tokens you acquire are directly linked to permissions you have asked the user for, of course. The type of tokens determine the type of action you are able to perform.

General thumb of rule, User Access Tokens are required for pulling rich information from the user’s timeline and you have to explicitly gain the permissions via the user beforehand  meaning the user has to be prompted with a dialog asking for all the permissions you need. People do not like to give extensive permissions so be very careful with that.

Page Access Tokens on the other hand originate from your Facebook page and attached app and can deliver you insights about users without prompting them first for permissions. In fact you can use these tokens to start conversations – however only if the user initiates the conversation and of course there are Facebook guide lines around what type of content can be messaged, in particular for push (chat) messaging.

 

Nice, but I want to know about who is talking with my bot

Well, you can get some user data just using a page access token:

https://graph.facebook.com/v2.6/<USER_ID>?access_token=PAGE_ACCESS_TOKEN.

The catch here is, you have to use the recipient id gained when the conversation started and apply a valid Page Access Token as the access_token parameter. Only in that combination will it work.

The returned data is limited and looks like this:

{ 
  "first_name": "Zorro", 
  "last_name": "Montana", 
  "profile_pic": "https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13606582_10154237760705900_4094680234132927774_n.jpg?oh=d2fc650700705c015a63e601e645984&oe=5842946C", 
  "locale": "en_US", 
  "timezone": 2, 
  "gender": "male"
}

Clearly, the downside there is that you do not get as much data as you could get via the Graph User API.

So in short, you will have to work with landing pages / deep links in order to interact with the Graph User API (and/or other resources at hands) in order to learn more about your chat bot consumers.

How to manage the conversation

In fact the given chat samples on their own will not help you much having a half-way decent interaction with your consumers. The reason for that is that your bot needs to make sense of the language, the intent of messages thrown at it and then act based on that intent. Services like Microsoft’s luis.ai help to translate user input into intents you can work with.

screen-shot-2016-10-04-at-16-58-14

A typical luis.ai link looks like this:

https://api.projectoxford.ai/luis/v1/application?id=abc&subscription-key=def&q=where%20can%20I%20get%20pizza%3F

Id is the id of your luis.ai app instance, subscription-key is tied to your outlook.com account, the query is the URL encoded payload containing the “user query”. That is the question you want to post against luis.ai.

For the sample query the response would look like this, using pre-built sets of intents:

{ 

  "query": "where can I get pizza?", 
  "intents": [ 
    { 
      "intent": "builtin.intent.places.find_place" 
    } 
  ], 
  "entities": [ 
    { 
      "entity": "pizza", 
      "type": "builtin.places.product" 
    } 
  ] 

}

The tedious part here is that you need to train the system and specify the entities to differentiate between, however luis.ai is well done and makes it fairly easy to deal with that part. Keep in mind luis.ai is very young and plenty of changes are expected, so be prepared for code changes.

And the chatbot is still not responsive (to everybody)!

When you utilize a chatbot for the first time it will only respond to your own inquiries. To make it work for the rest of the world you have to “submit if tor review”. To be more precise, you need to submit its capabilities for review.

In particular for pages_messaging_subscriptions application permissions this is kinda intense – you have to provide a screen cast of why and how you want to message people pro-actively after the first conversation. Oh and your users need to keep chatting with your bot at least within 24 hours since the last time you deliver a message, otherwise your messages will no longer be considered. Facebook wants to make very sure you do not annoy anybody and you have keep these constraints in mind, while new ones might pop up anytime.

Important: You can find about the status of the pending review via the developer console. After the application permissions were granted you still have to put your app to “public” via the Developer cosole -> App review. Only then will it be possible for others to chat with your bot at all.

Have fun creating your own chat bots and what I would love to see are some well made chat bot (text) adventures! Go for it!

 

Advertisements