Conversational AI is all the rage these days and for the right reason. Making chatbots smart enough to understand user intent and return results or information to complement user intention is conversational AI. We can choose to dive deeper into the subject but that is not the goal of this post.

In this post, we will create a chatbot using AWS Lex bot and lambda.

Goal

We will build a bot where the user will ask for Bitcoin price on a given date, and the bot will return the closing price of the bitcoin for that day. Behind the scene, we will use a lambda function to fetch the bitcoin price for that day.

STEP 1: BUILD A BOT

Log into AWS Management Console and click Amazon Lex under Machine Learning.

Click create

Before we move further, let us try to understand some key concepts behind lex.

Intents: A single skill (user intention) of the bot. With intents, the bot understands the user’s intention. A bot can have multiple intentions. In this demo, there is only one.

Utterances: Sentences that invoke/trigger intents. So, this would be intent “{Date} bitcoin price”

Slots: Variables that you must supply in intent. If you take the above example, {Date} is the slot. Amazon provides a list of built-in slots.

Create your first intent and fill with the following intent and slots.

Once you are done click Build. Now try asking one of the sample intent:
“2020/3/11 bitcoin price”

STEP 2: FETCH BITCOIN PRICE WITH LAMBDA

Once lex understands user intents it translates input intents into JSON. This input JSON is consumed by the code performing the transaction (in this case, fetching bitcoin price). Lex expects an output JSON from the code performing transaction.

On your AWS Management Console click Lambda and create a function as below. In this example, we are using Python 3.6 as our runtime language.

Paste the following code in your lambda function. As you can see that we are using API from coinapi.io, make sure you replace YOUR-API-KEY with your key.

"""
 Lexbot Lambda handler.
 """
 from urllib.request import Request, urlopen
 import json
 def get_bitcoin_price(date):
     print('get_bitcoin_price, date = ' + str(date))
     request = Request('https://rest.coinapi.io/v1/ohlcv/BITSTAMP_SPOT_BTC_USD/latest?period_id=1DAY&limit=1&time_start={}'.format(date))
     request.add_header('X-CoinAPI-Key', 'YOUR-API-KEY')
     response = json.loads(urlopen(request).read())
     return response[0]['price_close']
 def lambda_handler(event, context):
     print('received request: ' + str(event))
     date_input = event['currentIntent']['slots']['date']
     btc_price = get_bitcoin_price(date_input)
     response = {
         "dialogAction": {
             "type": "Close",
             "fulfillmentState": "Fulfilled",
             "message": {
               "contentType": "SSML",
               "content": "Bitcoin was at ${price} on {date}.".format(price=btc_price, date=date_input)
             },
         }
     }
     print('result = ' + str(response))
     return response

STEP 3: CONNECT LAMBDA FUNCTION TO BOT

On Lex console add your lambda function into the Fulfillment section.

Save and build the bot.

Congratulations, you’ve successfully built a chatbot….smart one. Now keep playing with it and try to add more intents and prompts.