Integrate Amazon Lex Chatbot Into Website

Suraj Kumar
Dev Tutorials
Published in
7 min readAug 31, 2019

--

Integrate Amazon lex into your website

Amazon Lex is a service by AWS for building conversational interfaces into any application using voice and text. Lex has quickly become popular among the chatbots enthusiasts. Notably, popular Amazon products such as Alexa are powered by the same technology as that of Amazon Lex.

In this blog, I will help you to understand the basic concepts of Lex and explain how to create a bot and integrate it into your website.

Understanding Amazon Lex

To understand the Lex platform, let’s consider the below conversation between a flight booking chatbot and a user:

User: Book me a flight to New York from Boston.

Bot: Sure! Which date you want to fly?

User: I am planning to fly on 10th of October.

Bot: Ok. Do you want me to book a return ticket as well?

User: Yes.

Bot: Please help me with the date of your return.

User: It is 15th October.

Bot — Alright. Searching for a flight for 10th of October and return for 15th october. Here are the results.

Here, the bot collects the basic information from the user, processes it and displays the search results to the user. To create this conversation flow using Lex lets first discuss the terminology and concepts used in Lex console. You can create a free account in Amazon Lex to build your chatbot.

Intents

An intent represents an action that the user wants to perform. When an end-user interacts with the bot, the user’s query is matched to the best intent available in the bot. Every intent has a set of Sample utterance. You can use Sample utterance to match the user’s query with an intent. You can configure a bot to support multiple intents. These combined intents can handle a complete conversation.

Sample utterance

This is the collection of possible expressions what an end-user might say. These are basically phrases which mean the same as our defined intent. Let’s go back to our flight booking bot. There are multiple ways to ask a bot to book your flight:

Book me a flight to New York.

Find me a plane to New York.

I want to go on a trip to New York.

Hey, let’s go to New York this winters.

You can provide these expressions while configuring the intent. You can use these expressions to build a model to categorize the user’s queries.

When a user uses any of these sentences then this model identifies the intent. If multiple intents are matched the best match is triggered. Suppose, if none of the intents are matched you can set a message in the Error Handling section to handle such scenarios. If an intent is matched, Lex will check how to fulfill the intent.

Fulfilling the intent

Fulfillments are the responses which the bot sends when an intent is triggered. There are two ways to define fulfillment:

  1. Create a Lambda function to fulfill the intent: Amazon recommends to create a Lambda function which will be called when the intent is triggered. Lex will send all details (intent detail and slots) and the Lambda function will perform the action and decide which response to be sent to the user.
  2. Lex returns the information(intent detail and slots) back to the client application to do the necessary fulfillment.

Slots

Slots are the parameters which are defined as part of the intent configuration. The value of a slot is extracted dynamically at runtime from the user’s query. Slots contain structured data that can easily be used to perform some logic or generate responses.

Each slot has a type which dictates the type of value slot would contain. Lex provides some inbuilt intents and slot types to extract the basic information i.e city names, dates, some measurement units, etc.

For example, when the user says:

Book me a flight to New York.

You can configure a slot so that your bot can detect the parameter “DestinationCity” and populate it with the value “New York” or whichever city presents in the user’s query.

Now, let’s identify the other possible slots in the above conversation.

Here is how your Amazon Lex console looks with this configuration.

You can configure fulfillment after finishing the slot configuration.

Configuring Lambda functions as fulfillment

When a user provides all of the slot data required to fulfill the intent, your Lambda function will be invoked, if enabled. Lex sends data to the lambda function in a specific format mentioned here. The Lambda function thus performs the business logic.

In our flight booking scenario, our Lambda function calls the flight search APIs to get the result for the user’s query. Then it returns the result to Lex in the aforementioned format. Further, Lex forwards this response to the end-user.

This was all about learning the basic functioning of the Amazon Lex platform. Let’s now jump into testing your bot and integrating it in a website.

Build Test and Publish the bot

After finishing all the configurations, you need to build your bot before you start testing. Lex provides a test window where you can test your bot and see how the bot replies to user’s queries.

Please note that if you are changing any intent configuration you have to build it again to make the latest changes visible in the test window.

Publishing the bot creates a new version of it. It allows you to create a different version of your bots and you can control the version which your application use.

Integrating Amazon Lex bot into a website

Lex has inbuilt support to integrate bots with some platforms such as Facebook, Kik, Slack, etc. If you want to integrate your bot with your own website or mobile apps, you have two ways:

  1. Using AWS SDK: AWS SDK provides APIs to send queries to the bot. This requires a lot of programming knowledge, development and maintenance efforts.
  2. Using third party applications like Kommunicate. I personally recommend the Kommunicate because it provides a code-less integration with Amazon Lex. It also comes with some cool features such as bot to human handoff.

Integrate Lex using Kommunicate

Kommunicate is a bot+human hybrid customer support software which provides code-less integration with chatbot builder platforms such as Amazon Lex, Google Dialogflow, etc. Once integrated, your users can chat with your bot using a beautiful and customizable chat-widget.

Your Lex bot can be integrated into your website in a few simple steps:

Step 1: Create a free Kommunicate account

You can create a free account in Kommunicate. Head to the signup section to start.

Step 2: Connect your Amazon Lex bot

Post signup, navigate to the bot integration section and select the Amazon Lex platform. Kommunicate requires below detail in order to query your bot on your behalf.

You just need to fill a few details to connect your Lex bot. You can get these details in your AWS Management Console -> Security credentials section.

  1. Access key ID & Secret access key: Access key ID and secret access key are required to sign requests that are sent to your Lex bot. To get your access key sign into your AWS console as an IAM user having permissions to access Lex API. Locate to your user name in the upper-right section of the navigation bar. From the drop-down menu, select My Security Credentials. Then create an access key in Access keys for CLI, SDK, & API access section. You can find more detail in this blog.
  2. Bot name in Lex platform: This is the same name you have entered while creating the bot in Lex platform. If you are not aware of it, you can also get it from the bot list in Lex home page.
  3. Bot alias: Bot alias is a pointer to a specific version of the bot. The alias is exposed to client applications instead of the version. If you publish a new version of the bot and want kommunicate to connect to the new version, you can simply point the alias to the new version from Lex console without changing anything on Kommunicate Dashboard.
  4. Region: AWS region where your Lex service is running. You can find your region in the top-right corner following the user name in AWS console.

Once you have the above information follow below steps, click Save and Proceed.

Step 3: Give your bot an identity

You can give your bot a name and a profile picture. The name and the profile picture will be visible to your users while they interact with your bot. Give your bot a name. This name will be visible to your users who interact with your bot. Click Save and Proceed.

Step 4: Enable/Disable human handoff

Your bot is as smart as you can make it. But at times, it may fail to understand the questions of a user. In that case, you can trigger a chatbot to human handoff. This helps you make the overall user experience better and handle edge cases.

Choose whether to enable or disable this feature and click on Finish bot integration setup.

Step 5: Assign all the incoming conversations to your Lex bot

To let your user chat with the new bot, you need to assign all the conversation to the bot. Just after finishing the bot setup, click on Let this bot handle all the incoming conversations. Now, all new Conversation initiated after the integration will be assigned to this bot and your bot will start answering them.

You can also enable conversation assignment from Conversation Rules section.

Step 6: Install the Kommunicate on your website

The final step is to install the Kommunicate chat widget on your website so that your website user can chat with your bot. Copy the installation script form the Install section and paste it into your websites. Here are the detailed instructions to install the same.

Publish your website and your bot is ready to chat with your users. Hurray! That was easy, isn’t it?

--

--