Build an Amazon Lex Bot with WhatsApp Integration

Muskan Jindal
5 min readNov 20, 2021

A chatbot is a service or tool that allows one to communicate via a chat interface. Chatbot understands what you are trying to imply and replies with a relevant message or directly completes the desired task for you.

In a project, we have created an Amazon Lex chatbot, named GiftCard that allows users to buy available gift cards and send them to their friends/family/colleagues via email or SMS. We have integrated this chatbot application with WhatsApp using Twilio.

Amazon Lex

Amazon Lex is a fully managed service for building conversational interfaces into any application using voice and text which provides the advanced deep learning functionalities of automatic speech recognition (ASR) for converting speech to text, and natural language understanding (NLU) to recognize the intent of the text. Amazon Lex enables you to quickly & easily build chatbots with highly engaging user experiences and lifelike conversational interactions. As a fully managed service, Amazon Lex scales automatically, so you don’t need to worry about managing infrastructure.

A bot performs automated tasks such as ordering a pizza, booking a hotel, ordering flowers, and so on. Amazon Lex bots can understand user input provided with text or speech and converse in natural language. You can create Lambda functions and add them as code hooks in your intent configuration to perform user data validation and fulfillment tasks.

AWS Services used in this project

  • Amazon Lex
  • Amazon Lambda
  • Amazon DynamoDB
  • Amazon Simple Notification Service (SNS)
  • Amazon Simple Email Service (SES)
  • Amazon CloudWatch

Building a Lex Bot

  • Intents are templates of user interaction sentences — requests, questions, answers to Alexa’s re-prompts. It represents an action that fulfills a user’s spoken request. Each intent invokes specific skill functionality.
  • Every intent has at least one utterance — a predefined word, phrase, or sentence. These are the words the user says to invoke the intent, to convey what they want to do, or to provide a response to a question.
  • Slot is an argument to an intent that gives Alexa more information about that request. For example, ‘Alexa, ask Pizza Hut to order Margherita’. In this statement, Margherita is the value of the pizza slot that refines the request.
  • Slot Type — Each slot has a type. There are built-in slot types that can be used or create a custom slot type if required. AMAZON.NUMBER built-in slot type can be used for order Id slot. While creating a custom slot type, a list of values for the slot has to be enumerated.
  • Prompt is the phrase used by Lex at runtime to get slot values for an intent. Each slot can have one or more prompts associated with it. Lex randomly picks up one of them.

Amazon DynamoDB is used to store the user, product, and order details. DynamoDB is a fully managed, Internet scalable, easily administrated, and cost-effective NoSQL database.

We have created three different tables namely — User, Product, and Order. Just as the snapshot below, all the available products are stored in the Product table along with all the required details.

GiftCard Lex bot consists of three different intents — UserInfoIntent, OrderDetailsIntent, and RecieverDetailsIntent.

UserInfoIntent

On starting the GiftCard Lex bot, UserInfoIntent gets triggered. It is used to get the details of the user/sender including its name, email, and phone number.

On fulfillment of the intent, a lambda function named GetUserDetails gets triggered and stores the details in the User table. Also, it fetches all the available gift cards from the Product table and sends them to the user, and asks the user which gift card they want to order.

OrderDetailsIntent

On knowing the available gift cards, when the user replies to the question asked of which gift card they want to order, the OrderDetailsIntent gets triggered. In this intent, we get the product user wants to buy/send, the ProductName, and Quantity.

On fulfillment of this intent, a lambda function named GetOrderDetails gets triggered. The lambda function here retrieves requested product details from the Product table like the name, price, description, steps to redeem, and terms and conditions.

ReceiverDetailsIntent

On confirmation of the product, the ReceiverDetailsIntent gets triggered and asks users for the details of the person they want to send the gift card to. The details include the name, email, and phone number of the receiver. Also, asks for the delivery mode (SMS, Email, or both) and a message to send along with the gift card.

On fulfillment of this intent, a lambda function named GetReceiverDetails gets triggered and stores the details in the Order table.

DynamoDB trigger to send Gift Card

Added a Dynamodb trigger, to execute a lambda function named SendConfirmation, whenever order details are stored in the Order table. This lambda function will send a confirmation email along with the payment link to the user/sender who ordered the gift card using Amazon SES.

Also, based on the delivery mode (SMS, Email, or both) selected by the sender, the gift card along with all the details including a unique gift card id and customized message will be sent to the receiver using Amazon SES and Amazon SNS.

Integrate Amazon Lex Bot Twilio SMS Channel with Twilio WhatsApp Sandbox

To integrate our Lex bot with WhatsApp, established a channel between Twilio and Amazon Lex, using the Twilio SMS channel. This channel generates an Endpoint URL, which is required to configure Twilio WhatsApp Sandbox.

In WhatsApp Sandbox, added the Endpoint URL in the “WHEN A MESSAGE COMES IN” textbox. And finally, test your chatbot using the number mentioned.

Tested the Integration

Tested the GiftCard Lex bot on WhatsApp

That's all for this project. Check the code here.

--

--