Quick Start — Develop a Chat Bot with AWS Lex + Lambda: Part 1

Nitin Labhishetty
Chatbots Magazine
Published in
4 min readMar 4, 2018

--

Chatbots are the rage these days, and for good reason. Conversational interfaces are going to be kickass. It’s intuitive (and fun) to have a dialog with your computer. This is a guide to easily develop your own chat bot.

Contents

Part 1: Create chat bot in lex + lambda

Let’s make a bot that finds the price of Bitcoin in 3 simple steps:

Step 1: Create a custom lex bot

We’ll start off by creating a custom bot.

Fig. 1: Create a custom lex bot

Step 2: Building the language model

Here’s where we give our bot the ability to understand conversations. Some terms used here:

  1. Intent: Intent is a skill the bot has. Our bot has one skill for now i.e. FindBitcoinPrice.
  2. Utterances: Sentences to invoke an intent. For find bitcoin price intent, an example utterance would be “what is the price of bitcoin on {Date}?”
  3. Slots: Values user must supply to an intent. In this example, it is the “{Date}” on which the user wants bitcoin’s value. Amazon provides a lot of built-in slot types, we can use `AMAZON.DATE` for this slot.

Let’s fill these up and build our bot.

Fig. 2: Lex bot model

Build will take a couple of seconds. You can now test your bot by asking it a question in Test Chatbot section.

Try asking “what’s the price of bitcoin on first feb?”.

Step 3: Adding the logic

Now our bot is able to understand questions and get inputs, we need to add “the logic to perform the skill” i.e. getting the dollar value of bitcoin. We’ll be adding this logic through AWS Lambda.

AWS Lambda lets us run code without having to create & host a server. Read more about “serverless” computing.

Workflow with lambda:

Fig. 3: Workflow with lambda

Once lex understands user’s sentence, it produces an input JSON. This input JSON goes to the code performing logic. Lex expects an output JSON in return. Both the input and output JSON have specific formats described in detail here. First let’s create a lambda to add our logic.

Step 3.a Create a Python 3.6 lambda

On your AWS console, create a lambda with python 3.6 runtime. Choose a role that has access to Cloudwatch logs (useful for lambda function logs.)

Fig. 4: Create Python 3.6 lambda function

Step 3.b Write python code for processing request

This is the input JSON we get from lex on asking “what is the price of bitcoin on first feb?”:

{
'messageVersion':'1.0',
'invocationSource':'FulfillmentCodeHook',
'userId':'zbf4r3orv6z2df6ryhcay9ulu97pd4uj',
'sessionAttributes':{},
'requestAttributes':None,
'bot':{
'name':'CryptoBot',
'alias':'$LATEST',
'version':'$LATEST'
},
'outputDialogMode':'Text',
'currentIntent':{
'name':'FindBitcoinPrice',
'slots':{
'Date':'2019-02-01'
}
,
'slotDetails':{
'Date':{
'resolutions':[],
'originalValue':'first feb'
}
},
'confirmationStatus':'None'
},
'inputTranscript':"what's the price of bitcoin on first feb?"
}

Lot of fields there, I’ve highlighted the ones useful for us. We’ll use CoinAPI to get the price of Bitcoin. Add this python code to the lambda:

Python code of crypto lex bot lambda.

Change the API key in the above code and save the lambda. You can use the above input JSON as a test event to the lambda.

Step 3.c Connect lambda to lex bot

On the lex console, add the above lambda in Fulfillment section.

Fig. 5: Connecting lambda to lex bot

Save and build the bot. Try it out by typing your question in the test bot section.

Congratulations on developing your own conversational bot! Tinker with it further, try adding new skills, integrate it with slack or do your own thing.

The second part of this article will talk about the conversational model of lex with advanced topics like multi-turn dialogs, handling sessions and so on. Use the Lex developer guide for any reference or to explore further. Cheers!

Clap to recommend this article to others so that small business can grow!

--

--

Googler, Ex-CodeNation. I love developing great products, exploring new tech, blues guitar and adventure sports.