First Simple Serverless ChatBot Experience with Amazon Lex and Lambda function

What will 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.

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:

    1. Bot Name: SpotChatBot

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

    3. Set Session Timeout: 5 min

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

    5. Choose no for COPPA.

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

  • ARCHITECTING BOT CONVERSATIONS:

    1)   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.

  • 2) 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
  • 3)  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.

  • 4)  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.

  • 5)  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

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).

Looking for a Top Rated* Marketing Company?

Contact us at +1 630 999 SPOT (7768) or submit a business inquiry online.