Facebook recently opened up their Messenger platform to enable bots to converse with users through Facebook Apps and on Facebook Pages. In support of this, the Facebook Messenger team created a comprehensive set of documentation describing the functionality on offer. While they offer a lot of functionality and platform-level integration for free, you still need to create a publicly-accessible application that Facebook can communicate with in order to communicate with your end users.
In this tutorial, we'll walk through creating your own messenger bot with a back-end ready to go, powered by Backand and live - all in 10 minutes.
You can chat with the simple bot example to see the end result: http://m.me/1150283998341709
Messenger bots are programs that receive messages from an interface, process those messages, and then send results back to the caller. These results are then displayed to the user by the controlling application - Facebook Messenger in this case. A bot's response can be as simple as echoing text back to the user, or as complex as ordering and shipping new computer components to the end user. The only restrictions are that the bot is authenticated to communicate with Facebook, and that Facebook has approved the bot and allows it to communicate with the world at large.
Luckily, Backand handles the majority of the server and connectivity functionality for you, allowing you to focus on building out responsive content.
Follow these steps to build out the back-end to your bot:
-
Sign up for free at https://www.backand.com (if you don't already have an account).
-
Create a new app in the Backand dashboard, then navigate to that app's management page.
-
In the new app, open menu
Objects --> Items
, and click on theActions
tab. In the Actions tab, click on theFacebook Messanger Bot
template. -
Click 'Save'
At this point you have a back-end server that is ready to integrate with Facebook Messenger. However, you need to make a few more configuration changes before you're ready to test.
Once you have the back-end and are ready to integrate, the first thing you need to do is create a Facebook Page at https://www.facebook.com/pages/create/?ref_type=pages_browser. This page will serve as the central integration point between your Backand application and Facebook, as the messenger bot is authorized to interact with Facebook on behalf of this page.
Once you have the app's page up and running, you need to configure the Facebook integration. To do so:
- Add a new app at https://developers.facebook.com/quickstarts/?platform=web. Name it and click "Create New Facebook App ID":
- Add an email address, select your app's category, and add a web site for your app (any URL is OK):
- Next, skip the quick start and go to the App Dashboard and click
Add Product
under the headingProduct Settings
. Once there, selectMessenger
from the available options:
Once the app page is created, and the app registered, you need to tell Facebook where to send its messages for processing. You can do this with the following steps:
- In the
Webhooks
section, clickSetup Webhooks
.
- Enter the URL for the webhook - this should link back to your Backand application, and will resemble the below:
https://api.backand.com/1/objects/action/items?
name=FBMessengerBot&Authorization=basic+{{master token}}:{{user key}}
Note: The webhook URL uses Backand's basic authentication {{master token}}. You can find the master token in the Security & Auth--> Configuration
section. It also requires a {{user key}} for your app - you can find this in the Security & Auth--> Team
section. Simply click on the key icon near one of the Admins to obtain the user key.
-
Verify Token: my_test_token
-
Select message_deliveries, messages, messaging_optins, and messaging_postbacks under Subscription Fields.
Once you've configured the app in Facebook, it's time to tie it back into your Backand application. In the Token Generation section, select your Page. A Page Access Token
will be generated for you. Copy this Page Access Token
, and navigate back to your Backand app. Open the Action
section of your Items
object, and paste the token into the Backand Action where indicated (PAGE_ACCESS_TOKEN).
Finally, you need to subscribe to the webhooks available for your page. This is managed in the Webhooks
section of the Facebook configuration:
At this point, you're ready to test your bot! Navigate to your Facebook Page and send it a message using Facebook Messenger. You should see the same message echoed back to you with a prefix of "Back& bot says:". This is also sent to the javascript console for immediate analysis.
At this point your basic bot is working. From here, you can customize the bot's server side to build out functionality for your users
A bot is only useful if people choose to interact with it. Below are some methods you can use to advertise your bot and draw in users.
As a part of their initial documentation, Facebook provided an easy method to add a Chat button to your website. The documentation on how to do this is available here.
You can also implement a shortlink that can be used to initiate a chat with your app. Simply use a URL of the form https://m.me/<PAGE_USERNAME>
to begin a Messenger chat.
At this point, you have a chatbot interacting with Facebook Messenger, driven by Backand as a back-end. You can customize the bot's code to meet your needs by following the informationa available in this backand guide.
You can also review Facebook's complete guide on Messenger integration, which provides more comprehensive detail on the platform's capabilities. These can be used to enhance your message responses, adding web-enabled content that your users can follow.
Eventually, you'll want to get your bot approved for use by the public. You can find out how to do that here.
Finally, you can enhance your app's intelligence with an AI integration. Find out how to get started at Wit.ai!
What is wrong?
https://api.backand.com/1/objects/items?name=FBMessengerBot&Authorization=basic+{me master ket}:{me user key}
The object "FBMessengerBot" was not found.
In Objects > Items -> and tabs Action setting Backand Action = FBMessengerBot