Building chatbot with amazon lex and lambda

“Amazon Lex is an AWS service for building conversational interfaces for any applications using voice and text.”

Amazon lex uses the same deep learning technology as Alexa. With Lex you can create your speech or text chatbots which can provide seamless natural language experience.

This bots can be integrated with other services provided by AWS like Lambda, MobileHub and CloudWatch etc. Lex allows you to easily publish the chatbots to mobile devices and to 3rd party messaging service like Slack, facebook messenger.

At the moment AWS Lex is only available in two regions US EAST (N. Virginia) and EU (Ireland). You can start with one of the sample chatbots (BookTrip, OrderFlowers, SceduleAppointments) provided by amazon or build your custom one from scratch.

Intents, Utterances, Slots, Prompts and Fulfillment are the basic components of amazon Lex. The picture below explains the basic components for hotel booking, one of the sample bots provided by amazon lex.

Here, my goal is to create a custom chatbot with the intent to register users for pre-defined event types.
You can create sample utterances, provide type of input that user should prompt to user to fulfil the intent. Here I am going to create a custom slot type from aws CLI.
"enumerationValues": [
{
"value": "AwesomeDay"
},
{
"value": "BuildersDay
}
],
"name": "EventTypesValue",
"description": "Types of Event to register”
}

Save above code as EventTypes.json

Now lets create this custom SlotType from the json using AWS CLI

Configure your AWS CLI with accesskey for your account. Use put-slot-type for creating custom slot type. Once deployed you can add it to the slots for your bot.

aws lex-models put-slot-type \
--region eu-west-1 \
--name EventTypesValue \
--cli-input-json file://EventTypes.json


Lets create the lambda function in the same region to support bot’s fulfilment. I have created custom lambda function in python as below:

import uuid
def lambda_handler(event, context):
id = registerUser(event)
message = 'Okay, I have registerd you. Your registration no is {}'.format(id)
state = 'Fulfilled'
response = {
'dialogAction': {
'type': 'Close',
'fulfillmentState': state,
'message': message
}
}
return response

Assign lambda function that we just created to fulfilment intent.



Build and test your chatbot.