Creating A Chatbot Using Amazon Lex Service - Day One - Initial Bot

Introduction

In this tutorial series, we’ll discuss how to create a real-time chatbot using Amazon Lex service and deploy the bot over Facebook messenger. The reader of the tutorial series does not need to have prior knowledge of bot creation as the article will demonstrate the bot creation and deployment step by step using explanatory pictures. Trust me, creating your own bot and seeing it work is much more fun and exciting than the mere thought of creating a bot. We’ll set up an AWS account to leverage Lex Services and Facebook developers account to deploy the bot on your own Facebook page and see it working. Let’s jump directly into the tutorial.

Series Info

Following is the four-tutorial series to learn how to develop a chatbot using Amazon Lex service and deploy the same on Facebook Messenger.

  1. Creating a Chatbot using Amazon Lex Service: Day 1 (Initial Bot)
  2. Creating a Chatbot using Amazon Lex Service: Day 2 (AWS Lambda Functions)
  3. Creating a Chatbot using Amazon Lex Service: Day 3 (Slots in AWS Lex)
  4. Creating a Chatbot using Amazon Lex Service: Day 4 (Facebook Messenger Integration)

Setup AWS account

  1. Go to aws.amazon.com and sign in using your email address if you have an account or create a new account.

    Creating A Chatbot Using Amazon Lex Service
  1. Provide the relevant details like email address and password while creating a new account.

    Creating A Chatbot Using Amazon Lex Service
  1. On the next step, you can choose to create a Personal type account and provide the needed information there.

    Creating A Chatbot Using Amazon Lex Service
  1. The next page is the payment information page where you need to provide the payment mode and its details. There's nothing to worry about, as when you enter your card details it deducts a minimal amount; i.e., 2 INR which would be reverted to the account after successful verification of the card.

“With Amazon Lex, you pay only for what you use. You are charged based on the number of text or voice requests processed by your bot, at $0.004 per voice request, and $.00075 per text request. For example, the cost for 1,000 speech requests would be $4.00, and 1,000 text requests would cost $0.75. Your usage is measured in “requests processed”, which are added up at the end of the month to generate your monthly charges.

You can try Amazon Lex for free. From the date you get started with Amazon Lex, you can process up to 10,000 text requests and 5,000 speech requests per month for free for the first year.”  Read more about Lex pricing here.

Creating A Chatbot Using Amazon Lex Service

 

  1. Once you've successfully entered the console, choose the region as US East (N. Virginia).

    Creating A Chatbot Using Amazon Lex Service

Create Bot using Amazon Lex

After setting up the AWS account, it is time to create the bot using Lex service. Let’s get familiar with the terminology before we start.

A bot is a top-level component that that is deployed. It is a kind of container. The bot that we’ll develop will have one or more intents. Each one represents a different topic that a bot can understand and perform an action to fulfill it.

A Slot is something that we define to get the kind of information we need from the bot user to fulfill the intent.

A Lambda function is simply a place to execute your code that doesn't require you to provision or manage a server or is serverless. Lambda supports multiple programming languages and is how you integrate your bot with other systems. Your AWS account includes 1 million free requests per month and up to 3. 2 million seconds of total execution time. Please visit the AWS Lambda pricing page for more details.

  1. In the console, search for the service type Lex as shown in the following image.

    Creating A Chatbot Using Amazon Lex Service
  1. You’ll be shown the Amazon Lex landing page. Click on Get Started to start creating the bot.

    Creating A Chatbot Using Amazon Lex Service
  1. We can try the existing bot samples and can create our own as well. Since we are creating a bot from scratch, choose the option of the Custom bot and give it a name.

    Creating A Chatbot Using Amazon Lex Service
  1. Provide details like bot name. For output voice, choose None as this will only be a text-based bot. Set the timeout to 5 minutes. The IAM role is created for us, so there is no action needed on this line. For COPPA we can choose No.

    Creating A Chatbot Using Amazon Lex Service
  1. Once done, you’ll see the BotService page. Now it’s time to create the intent as the bot is only just a container and it needs intents. Click on create an intent button to create a new intent.

    Creating A Chatbot Using Amazon Lex Service
  1. On the popup, it shows the option to search for existing intents, create new or import an intent. We’ll select Create intent option to create a new intent.

    Creating A Chatbot Using Amazon Lex Service
  1. Give a unique name to the intent. In our case, it is “OperatingHours”. The purpose of this intent is to tell the user about the operating business hours of the company for which the user is seeking information. For e.g. when you reach out to a company’s contact us via chat section, the bot welcomes you and if the user asks, “what are your working hours?” then the bot should respond with the company’s working hours and tell that to the user.

    Creating A Chatbot Using Amazon Lex Service
  1. Adding intent needs some more actions to be performed. For example, Sample utterances; i.e., the kind of questions a user can ask to bot --  “what are your working hours?”, “what time do you open?”, “what time do you close?” etc. Note that we do not have to provide all possible examples. The built-in language model will recognize similar phrases. Next is Lambda initialization and validation. This is where we can link to the code in an AWS Lambda function to validate the user inputs if we are collecting information as part of the intent. Slots are how we collect data from the user to pass into validation or fulfillment. For this intent, we don't need any specific data from the user, so we will not define any Slots.

    Creating A Chatbot Using Amazon Lex Service
  1. Provide some sample utterances as shown in the following picture.

    Creating A Chatbot Using Amazon Lex Service
  1. A confirmation prompt is for double checking with the user that we have understood everything correctly from the user before the bot fulfills the intent. So, enable Confirmation prompt to see how it works. Since we didn't collect any data from the user, ask a simple question. If the user says yes, they move onto fulfillment. If they say no, then the message we put in this Cancel box will display, and the user will exit the intent.

    Creating A Chatbot Using Amazon Lex Service
  1. Finally, fulfillment is how the bot performs the result action. Typically, this is done by calling an AWS Lambda function to execute the appropriate business logic. There is also an option to return the parameters to the client. For now, you can choose that option as there is no lambda function.

  2. Now, build the bot via via the Build option as shown in the following image.

    Creating A Chatbot Using Amazon Lex Service
  1. Once you click built, you’ll be prompted with a message to continue testing your bot while it is building. Click on Build.

    Creating A Chatbot Using Amazon Lex Service
  1. After a successful build, the prompt will be shown as shown in the following picture.

    Creating A Chatbot Using Amazon Lex Service

Test the Bot

Our first version of Bot is created now, and we can test it.

  1. At the right side of the page, you can see the option to test the latest bot. Start typing in now 😊.

    Creating A Chatbot Using Amazon Lex Service
  1. If you type anything other than the utterances defined in the intent, the bit doesn’t recognize that and gives a defined response as shown below.

    Creating A Chatbot Using Amazon Lex Service
  1. Ask your bot a question defined in utterances, e.g., “what are your official working hours?” and it replies with the confirmation message that we provided in the intent.

    Creating A Chatbot Using Amazon Lex Service
  1. Once you say, yes, it navigates to fulfilling the intent, but since we did not provide any lambda it says it is ready for fulfillment.

    Creating A Chatbot Using Amazon Lex Service
  1. Again, ask the relevant question.

    Creating A Chatbot Using Amazon Lex Service
  1. Now type no and we’ll see that the bot responds with OK, Thanks!;i.e. the text we provided in the confirmation for no.

    Creating A Chatbot Using Amazon Lex Service

Conclusion

In this article, we learned what chatbots are, and how to set up the AWS account to get started and create and test a simple chatbot using Amazon Lex service. In the next article, we’ll discuss AWS Lambda functions, and further, will continue with slots, error handling, and deployment over Facebook Messenger.


Similar Articles