Image

What will you learn?

What you need?

Introduction

Amazon Lex is a tool for building conversational interfaces on AWS. Lex is effectively the technology used by Alexa, Amazon’s voice-activated virtual assistant which lets people control things with voice commands such as playing music, setting alarm, ordering groceries, etc. It provides deep learning-powered natural-language understanding along with automatic speech recognition. Amazon now provides it as a service that allows developers to take advantage of the same features used by Amazon Alexa. So, now there is no need to spend time in setting up and managing the infrastructure for your bots.

The best thing about ChatBot:

“Amazon Lex allows you to easily publish your bot to chat services directly from the Amazon Lex console, reducing multi-platform development efforts. You can configure the bot through the Console to create a callback URL to facilitate the integration. Rich formatting capabilities on Amazon Lex enable an intuitive user experience. Integration with Kik simplifies deployment and management of the Amazon Lex bot on the messaging platform.”

How does amazon lex work?

Building a bot on Lex is done through the AWS console. Start by selecting “Lex” from the list and clicking on “Get Started.” This is also a good time to read the “Getting Started with Amazon Lex” guide.

Steps to first simple serverless chatBot experience with amazon lex and lambda function

Let’s implement ChatBot

1. First start with amazon lex

Go to Amazon Lex

Click on “Get Started with Amazon Lex”

Now give the credentials as IAM user or you can also sign-in through root account credentials. If don’t have amazon account no worry we can create it through sign-up which free of cost.

Click on “Create button” for creating new ChatBot.

Now you can choose to create your own experience, or start with a template, or sample from the menu. Here we are choosing “Custom bot”.

Create a custom bot by providing following information:

Bot Name: SpotChatBot

Output voice: None, this is only a test based application

Set Session Timeout: 5 min

Add Amazon Lex basic role to Bot app: Amazon will create it automatically.Find out more about Lex roles and permissions here.

Choose no for COPPA.

Click on Create button, which will redirect you to the editor page.

ARCHITECTING BOT CONVERSATIONS:

Intent: Intent represents a goal, needed to be achieved by the bot’s user. In our case, our goal is to provide services to customers.

Here we are creating 1 intent name as “Spot Service”.

Click on “create Intent”.

Under “Add Intent” click “Create Intent” and give the name of Intent.

Click on “Add” for adding Intent for our ChatBot.

Utterances: An utterance is a text phrase that invokes intent. If we have more than one intent, we need to provide different utterances for them. Amazon Lex builds a language model based on utterance phrases provided by us, which then invoke the required intent.

Provide some utterances to invoke the intent. An utterance can consist only of Unicode characters, spaces, and valid punctuation marks. Valid punctuation marks are periods for abbreviations, underscores, apostrophes, and hyphens. If there is a slot placeholder in your utterance ensure, that it’s in the {slotName} format and has spaces at both ends.

For our example, we need a single intent “SpotService”. Some sample utterances would be:

I want to know about jobs at Us location

Can you please provide service on Automation

Slots: Each slot is a piece of data that the user must supply in order to fulfill the intent. Slots are an input, a string, location etc. that are needed to reach the goal of the intent. Each slot has a name, slot type, a prompt, and is it required. The slot types are the valid values a user can respond with, which can be either custom defined or one of the Amazon pre-built types.

Map slots with their types and provide prompt questions that need to be asked to get valid value for the slot.

Note the sequence, Lex-bot will ask the questions according to priority.

Prompt: A prompt is a question that Lex uses to ask the user to supply some correct data (for a slot) that is needed to fulfill an intent. e.g. Lex will ask “How can I help you like, services, jobs or events?” to fill the slot.

Fulfillment: Fulfillment provides the business logic that is executed after getting all required slot values, need to achieve the goal. Amazon Lex supports the use of Lambda functions for fulfillment of business logic and for validations.

Now we have all necessary data gathered from the chatbot, it can just be passed over in lambda function, or the parameters can be returned to the client application that then calls a REST endpoint.

2. Start with amazon lambda function

Amazon Lex supports Lambda function to provide code hooks to the bot. These functions can serve multiple purposes such as improving the user interaction with the bot by using prior knowledge, validating the input data that bot received from the user and fulfilling the intent.

Steps for Creating Lambda Function:

Go to AWS Lambda and click on “Get started with AWS Lambda”.

Now give the credentials as Iam user or you can also sign-in through root account credentials. If don’t have amazon account no worry we can create it through sign-up which free of cost.

After navigating to AWS console, click on “Create Function” for creating new function.

Select blueprint as blank function and click next.

Give the details of as follows

Click on “create function”.

Now our console is ready for write the code.

FULFILLMENT CODE HOOK: This lambda function is invoked after receiving all slot data required to fulfill the intent

Now our console is ready for write the code.

exports.handler = (event, context, callback) => {

// when intent get fulfilled, inform lex to complete the state

let response = {sessionAttributes: event.sessionAttributes,

dialogAction: {

type: “Close”,

fulfillmentState: “Fulfilled”,

message: {

contentType: “PlainText”,

content: “Thanks for service request. Mail us at rakesh@nectarspot.com if any queries are there .”

}

}

}

callback(null, response);

};

Click on “Test” button for configuring the test event.

Write the test event and save it

We can configure our bot to invoke these lambda functions. We need to do this while configuring the intent as shown below:

Now click on “Build” for building the ChatBot.

Now ChatBot is ready for test

I hope you have understood the basic terminologies of Amazon Lex along with how to create a simple chat-bot using serverless (Amazon Lambda).

Receive latest marketing insights, data and inspiration

View Blogs ➞ Subscribe ➞

Start your Project Today

Get Started