This article is written and authorized by Mahdi Azarboon.
Amazon Lex enables you to quickly build a chatbot in a few minutes. A few months ago, I tried to integrate a Lex chatbot with Facebook — and spent way too long because of the poor documentation and guidance.
To help save you some time and effort, this blog explains how the integration works and provides a simple and updated step-by-step guide for building your own chatbot using Amazon Lex and Facebook Messenger.
The overall integration is fairly straightforward. To get started, a developer simply creates a Facebook Page and a Facebook App. A page visitor communicates to your Facebook App by means of Facebook Messenger.
On the backend, the Facebook App sends and receives message events to Amazon Lex — this enables visitors to have back and forth communication with your Facebook App.
The Facebook App is subscribed to the Facebook Page and listens for events. Whenever a visitor sends a message to the page by means of Facebook Messenger, a webhook event is sent to the Facebook App.
The Facebook App can subscribe to variety of page events. For example, to receive messages the Facebook App must be subscribed to the following page events: messages, messaging_postbacks.
Without Lex integration, the developer needs to build their own webhook implementation to respond to these events. As an example of webhook implementation, developer can use Facebook Starter Project and must to deploy it somewhere.
However, Lex can abstract this level of difficulty away by providing a webhook implementation. To do this, Lex provides an integration channel that developer can use to connect with their Facebook App. With this approach, a developer can focus on building the bot logic — without worrying about creating their own webhook implementation.
To integrate Amazon Lex with Facebook, the current Lex documentation redirects users to Facebook’s Quick Start Tutorial. There, you are instructed to create your own webhook implementation and then host it somewhere.
You don’t need to do this.
You can integrate Lex and Messenger without deploying a separate application! There are also a few other points in the tutorial which are confusing, and their documentation is very poor.
In this step-by-step tutorial, we’ll walk through all the relevant points with a simple guide to integrating these services for your chatbot.
For the sake of this tutorial, you can choose “Community or public figure” for your page, and then you can choose page name as well as a narrower page category. We will use this page in later steps.
Once you confirm the prompts, it generates Page Access Token. Copy the Access Token and save it. You will need it soon.
Click Activate and if you have done everything correctly, you should be able to see Callback Urls on the bottom of the page. Copy the endpoint url.
To provide some clarity on this process, I have tested from Lex console. You can see the bot reaction on the right side of page, so you can see what I expect to get from my Messenger once connected with Lex. For brevity, I have created a Bot with minimum logic.
You can bypass the manual invitation process, by submitting your Facebook App for Facebook App Review and making it public. At the moment, Facebook can take some weeks to review your app — however this might vary from some days to some months depending on the permissions you are asking.
For bot-related permissions the review-time is usually relatively short. There is no way currently to speed up the process, so make sure you are in no rush when going live with your bot. You can get updated information about Facebook reviewing process on its documentation.
From December 2018, the permissions are granted at page level, so for each page that you would like to connect with your app, you need to do the review separately. You will also need to verify either a business or pass verification for individual developers which has been available since December 2018.
I would like to thank Pinja Kuosmanen for helping me with this post! She helped me as advisor and reviewer.
Before publishing this post, I deleted my Facebook App, Page and Lex app. Therefore, credentials available in the screenshots can’t bring you anywhere. So, you don’t need to waste your time trying to hack me.