The Ops Community ⚙️


Posted on • Originally published at

Making a WhatsApp Crypto Chatbot with AWS Lex, CDK, CoinMarketCap, and Twilio

What is a Chatbot?

A chatbot is a software or computer program that simulates human conversation or "chatter" through text or voice interactions.

Amazon Lex is a fully managed artificial intelligence (AI) service with advanced natural language models to design, build, test, and deploy conversational interfaces in applications.

This article aims to create an automated agent that will retrieve information about the current prices and trends of the crypto market.


  • AWS account
  • NPM / CDK / Python
  • Coinmarketcap API key
  • Twilio account (optional)


Let's create an account in CoinMarketCap to get our API Key. Feel free to choose the plan that adjusts to your use case, but the basic plan is good enough for this example.
With our API Key, create a Parameter Store to store this value.

/dev/crypto-bot/COIN_MARKETCAP_API_KEY : <<your-api-key>>
Enter fullscreen mode Exit fullscreen mode

For this example, I will use a clear string for the API Key value, but it is recommended to secure the string with an encrypted AWS KMS key.
Parameter store
Now lets begin creating our project with cdk, after we configure our aws credentials, run:

cdk init sample-app --language python
Enter fullscreen mode Exit fullscreen mode

In our requirements.txt file lets add the following dependencies:

Enter fullscreen mode Exit fullscreen mode

python-coinmarketcap is a dependency that wraps Coinmarketcap API very nicely; feel free to use this dependency or to use a direct implementation which you can find here.
boto3 is a dependency that acts as the AWS SDK for Python; this will be used to access the Parameter Store previously created.

In our file you can change the name of the project and the name of the stack, for this example I will name it cbot.
import aws_cdk as cdk

from cbot.cbot_stack import CbotStack

app = cdk.App()
CbotStack(app, "cbot")

Enter fullscreen mode Exit fullscreen mode

Create a folder with the name of your project (cbot), and create or rename the name of the stack file ( In this stack, we will create a lambda, give that lambda access to our Parameter Store, and create a layer to add our dependencies.

Create a folder named lambda, and create a In this lambda, we will use the CoinMarketCap API, which will interact with our slot intent.
def search_crypto(intent_request):
    session_attributes = get_session_attributes(intent_request)
    slots = get_slots(intent_request)
    crypto = get_slot(intent_request, "crypto")
    api_key = ssm.get_parameter(Name="/dev/crypto-bot/COIN_MARKETCAP_API_KEY")
    cmc = coinmarketcapapi.CoinMarketCapAPI(api_key["Parameter"]["Value"])
    r = cmc.cryptocurrency_info(symbol=crypto.upper())
    text = repr([crypto]["description"])
    message = {"contentType": "PlainText", "content": text}
    fulfillment_state = "Fulfilled"
    return close(intent_request, session_attributes, fulfillment_state, message)
Enter fullscreen mode Exit fullscreen mode

Our project should look a bit like this:

Project structure

Run the cdk bootstrap command to prepare our account to handle cdk deployments.

cdk bootstrap
Enter fullscreen mode Exit fullscreen mode

Now we are ready to deploy our lambda into our account

cdk deploy
Enter fullscreen mode Exit fullscreen mode

Let's start with the final step, create a bot using the AWS Lex service

  • Creation method > Create a blank bot
  • Bot configuration > Name your bot
  • IAM permissions > Create a role with basic Amazon Lex permissions
  • Children’s Online Privacy Protection Act (COPPA) > No
  • Language > English (US)

Create a Welcome Intent; let's add some examples that our bot will learn and detect our intent

Sample Utterance 1
Add our bot response to the fulfillment section
Fulfilment 1
Test our bot, if we configure our intents properly it should response with our fulfillment text
Test Bot 1
Configure the text of the fallback intent, if something goes wrong this intent should catch this error and show a generic error message
Save the intent, build and test. If you write something that we didn't add to the utterance examples, it should trigger our fallback intent.
Test Fallback
Create another intent named CryptoSearch; let's add some utterance examples to detect if a user wants to search about crypto
Crypto Intent
Add a slot. In this slot, we will capture the crypto symbol we would like to search with our CoinMarketCap API; you can define a specific type of data, but for simplicity, I will use the integrated type AMAZON.FirstName to capture the value inputed by the user. (Be sure to name it identical to our code in our lambda.)
Under Deployment in Aliases, let's add our lambda, be sure to create a production alias when you move your bot to production. For this example, we will use the default alias


Lambda Function
In the fulfillment section of our CryptoSearch intent, be sure to use our lambda function
Save the intent, build and test our bot. Now we can retrieve information about any crypto that CoinMarketCap supports! We can even try fancy things like plotting, creating trends, or showing candle graphs.
Now let's integrate our bot into social media. In this example, I will use Twillio to publish our bot into WhatsApp, create an account in Twilio and copy the SID and Authentication values.
In AWS, lex select Channels, select Twilio and create the channel integration.
Copy the callback URL generated in our integration.
In the Twilio, dashboard, select message and set up a Twilio Sandbox for Whatsapp; here, we will copy our callback URL, which will connect the messages sent from WhatsApp to our bot in AWS Lex.
And.. that's it! Now our bot works with WhatsApp and Lex.

You can find the complete code here


Top comments (0)