How to build FB Chat Bot using Amazon Lex (Part 1)

16 May, 2018 | 3 minutes read

Amazon Lex is a service for building conversational chatbots quickly and easily. It provides the deep functionality and flexibility of natural language understanding and automatic speech recognition, thus allowing developers to build highly engaging user experiences. To create a bot the developer just needs to specify the basic conversation flow in the Amazon Lex console.

To follow along with this tutorial you will need an AWS account.  If you don’t have one, you can create it on the following link https://aws.amazon.com/. Once logged in on the console choose Amazon Lex from the services section. Then, choose to create a new bot.

How to create a Lex bot
How to create a Lex bot

  • You can create your own custom bot or try out some of the sample bots. The samples are useful to give you a feel of what you can do using Amazon Lex. For this tutorial we will create a custom bot.
  • In case you want to use voice commands as well, you can pick one of the many built-in voices provided by Amazon. For our application we didn’t choose any.
  • The session timeout specifies the time period from the last sent message, in which the Lex bot remembers who is chatting with. It’s important to note that new updates of the bot will not be available for sessions that have already started before, so it’s a good practice to keep the session short unless you are storing some information regarding the conversation with the user in the session.

Once the bot is created, we can create one or more intents. Let us create an intent that will give information to the user about Interworks blog posts.

Creating an intent
Creating an intent
  • Utterances are the phrases that will trigger this intent. The more utterances an intent has, the more material the bot has to learn from. This allows Lex to trigger this intent even with an utterance that we have not entered, if it’s somewhat similar to one of the existing utterances.
  • The Lambda initialization and validation section are used to validate the input of the user.
  • The slots section allows us to create variables in our utterances and do specific actions based on the values of those variables.
  • Confirmation prompts are optional questions that prompt the user to confirm or cancel the fulfillment of the intent.
  • For fulfillment, we will select a lambda function. This function needs to be created by the time we connect it to Lex. We will come back to the code for the lambda function a bit later.

After we connect our lambda function we can build and test our chatbot. Clicking on the Test Bot button will open a chat where we can write to our bot.

Testing the chat bot
Testing the chat bot

 Lambda function

The following code snippet demonstrates the functionality for the blogs intent:

  Code snippet
Code snippet

The Message class is a simple function that allows us to set the content and the content type of each message.

message class
Message class

The actual lambda function contains too many files which makes it a bit difficult to show all of them in one blog. For the full code you can check out our GIT repository: ChatBotGitUrl

Conclusion

Amazon Lex enables developers to build conversational chatbots. With Amazon Lex, no deep learning expertise is necessary. To create a bot, we just need to specify the basic conversation flow and Lex takes care of the rest.

In part 2, we will see how to connect our chat bot to a Facebook page. Stay tuned.