Facebook Messenger Amazon Lex chatbot

Set up FB chatbot with Amazon Lex

Create a Facebook Messenger chatbot, powered by Amazon Lex. ...

Second post on this blog, here we go! There are many ways to make chatbots, in this post I’ll explain how to set up a Facebook Page for Messenger chatbot integration, along with a 3rd party service called Amazon Lex.

What is Amazon Lex and Facebook Chatbot

A chatbot is a type of software that is designed to listen to and respond to users in a conversation. You often see chatbots applied to automated text messaging / phone call services, as well as on website and social media platforms. Chatbots are often paired Artificial Intelligence (AI) and Machine Learning (ML), to create systems that can understand a users ‘intention’ behind their message – rather than just a specific phrase, allowing the chatbot’s conversational performance to appear more seamless.

Amazon Lex is a service from Amazon Web Services, that allows developers to create a conversational artificial intelligence. This technology is the performs the ‘intention’ recognition that I described above (as well as triggering programmatic responses via Lamda functions), it is used to power Amazon Alexa and can also be used as the brain to a chatbot.

A Facebook Chatbot is a special type of Facebook App, which is built into Messenger. It is an app that can be programmed to automatically respond to user messages.

When you put Amazon Lex and Facebook Messenger Bot together, you get a chatbot, which is hosted on and interacted with via Facebook’s platform – which has a ‘brain’ powered by the same technology that enables Alexa, via Amazon Lex.

For this example, I’ll be using a Facebook page called ‘Deathroy – the Lincoln Goose‘ (a Facebook Page that was created for a famous goose from the city where I went to university – its a long story).

Simplified drawing of how a chatbot works, using Facebook Page ‘Deathroy – the Lincoln Goose‘ – with my AWS account as an example

Having explained the theory of the chatbot, there are a few things you need to have done prior to being able to follow along with this tutorial:

Assumptions of your technical knowledge

For this post, you will need to have

  • Set up your own Facebook Page (not a Facebook Profile or Group)
  • A fairly good understanding of how to use Facebook
  • Activated your Facebook profile as a Developer account

Seems pretty simple, lets get go!

Step 1: Create a new Facebook App ID

Open a web browser and navigate to https://developers.facebook.com/apps/, then click on ‘Add a New App’ and enter in the name of your Facebook app, along with your email address (I’ve blurred mine in the below screenshot) – click the blue ‘Create App ID’ button once you’ve entered your details.

Facebook App ID creation page, with entered details and blue ‘Create App ID’ button

Step 2: Add Messenger functionality to your App

Add Messenger functionality to your app by clicking the ‘Set Up’ button within the Messenger option at the top left of the other options.

Facebook App Products, Messenger functionality is at the top left.

Step 3: Add Messaging privileges

Scroll down to where it says ‘App Review for Messenger’ and add ‘pages_messaging’ by clicking the ‘Add to Submission’ button on the right.

Permissions to grant the Facebook Messenger app, as it is a messenging bot – it will need the ‘pages_messenging’ permission

Step 4: Generate Page Access Tokens

Scroll back up the page and generate an access token for a facebook page you own by clicking on the blue ‘Add or Remove Pages’ button within the ‘Access Tokens’ section.

User interface for generating a Page Access Token, to allow programmatic access to the Facebook Page.

Step 5: Copy Page Access Tokens

Tick the ‘I Understand’ tick box and then copy the key access token that is displayed. Make note of it somewhere as you won’t be able to view it again for security reasons.

The Facebook Page Access Token, make a note of this key

Step 6: Copy the App Secret key

In the left navigation bar, click ‘Settings’ and ‘Basic’ to view the app details. To the right of the screen you will see a hidden key under ‘App Secret’, click the ‘Show’ button and make a note of the revealed characters – this will be needed in a later step.

Facebook App Secret key within the developer console

Step 7: Open a new web tab for AWS

Open a new tab in your web browser and navigate to aws.amazon.com and sign in to your Amazon Web Services account.

Amazon Web Services log in screen

Step 8: Navigate to Amazon Lex

Navigate to Amazon Lex within the AWS interface and make sure you are in a region that supports Amazon Lex, in my case I’m from the England – so the closest supporting region is Ireland. Once done, click on the ‘Create’ button and enter the name of the bot you’re creating, in my case is it ‘Deathroy’.

Amazon Lex service within the AWS web portal

Step 9: Create an Alias

To identify and version your not, create an Alias within the ‘Settings’ tab by clicking the blue plus icon – in my case I called the Alias ‘Dev’.

Setting up an Alias for Amazon Lex

Step 10: Set up Facebook Channel information

Then open the ‘Channels’ tab click on the ‘Facebook’ channel to see form that needs to be filled. Fill out all the details on the form – apart from ‘Verify Token’, this will be provided in the next step. Ensure you remember to use the ‘Page Access Token’ that you generated from step 5 and the ‘App Secret Key’ that you copied from step 6.

Facebook Channel details form within Amazon Lex

Step 11: Create a Verify Token

Go back to your other tab in your web browser that contains your Facebook Messenger app. Find the area labelled ‘Edit Callback Url’ and write a phrase or set of characters into the area labelled ‘Verify Token’ – remember this set of characters for the next step because you’ll need to provide the same ‘Verification Token’ to Amazon Lex. Leave the ‘Callback URL’ blank for now, you will be provided this url in the next step.

Callback URL details within Facebook App portal

Step 12: Generate callback URL

Go back to your AWS tab and enter the token you’ve just created into ‘Verify Token’, then click Activate. You will then be able to create and copy an Endpoint URL, which you then need to paste into the ‘Callback URL’ field in the Facebook app browser tab from the previous step and click the blue ‘Verify and Save’ button.

The generated callback URL with Amazon Lex

Step 13: Set up an intent

In the Amazon Lex browser tab, navigate to Editor and click on the small blue plus icon to create a new intent and give it a name. In this case I named mine ‘WeatherAtBrayford’.

Setting up an intent for Amazon Lex

Step 14: Enter intent details

Enter various phrases that you would like then trigger your intent, in my case I entered in several different phrases that asked you what the weather was like – e.g ‘What is the weather like?’. These will be triggers for an action that you’d like the bot to execute.

Then scroll down to the ‘Fulfilment’ section and create a message by ticking the ‘Message’ tick box, in my case I wrote ‘It is good lol’. This is what the bot will say in response when the intent has been recognised. Then click the ‘Safe Intent’ button.

Intent details form within Amazon Lex

Step 15: Build the bot

At the top right of the screen click the grey ‘Build’ button and then the blue ‘Build’ button – to compile the all the configurations you’ve set up into a bot that can be deployed.

Building the bot using Amazon Lex

Step 16: Publish the bot

Once the bot has been built, click on the blue ‘Publish’ button, select your Alias (in my case Dev) and click on the blue ‘Publish’ button.

Publishing the bot to the Facebook page via Amazon Lex

Step 17: Test the bot

Once the bot has been published, open Facebook Messenger and try to message your bot with the key phrase you entered in as an ‘Intent’, in my case this is ‘What is the weather like?’.

Then if all worked correctly, the bot will respond, yay!!!! 😀

Please note that you have not yet gone through Facebooks Bot approval process, so this will only respond to you + others that you give special privileges!

Approving the bot for public use will have to be another blog post!!

Testing the bot within Facebook Messenger

Share this post