In this module you will build on the Lex chatbot you started in module 1 and add abilities for it to subscribe users to additional services your company offers -- in this case, international plans you can add on to your existing phone plan for travel.
You have already learned to define intents, collect user inputs by leveraging slots in the last module. In this one we will go into more advanced topics such as session management and input validation.
Now the bot is capable of informing your customer the options they can add to the account, the next step is allow the user to choose a plan and add it to their account. This serves an example for another common use case for chatbots: transactional requests such as updating account information, fulfill orders, etc.
We can create a separate intent to apply the chosen plan into the user's account. This bring up the question of maintaining session context. During the same session, if the user asks to subscribe to a particular plan after hearing the list of options for a given country, the bot should maintain context and know which country is of interest when applying the plan.
You can achieve this by using Session Attributes. Read here if you want more details of how session attributes work. You can set session attributes in the Lambda function that fulfills the bot logic.
In the ListInternationalPlans
intent we built in the last module, the Lambda function is setting the country
session attribute as part of the response, so subsequent conversation will have that context:
You can also take a look at the Lambda source code to see how it's setting the session attributes (look for the listPlanIntent
function).
This type of transactional requests also require your bot to be able to identify and authenticate the user before fulfilling their request.
The way you identify users can vary depend on where and how your Lex bot is deployed.
If your Amazon Lex bot is deployed as a Facebook Messenger or Slack bot, users are already logged in into Facebook/Slack respectively, and these channels will pass the user ID on these platforms into your bot. This means you need to build a backend that can correlate the Facebook/Slack user to your company's user management system.
If your Lex bot is bulit as part of your mobile/web app, then you can rely on the normal authentication methods of your app for users to log in.
This workshop shows an example if your Lex bot is deployed with either Amazon Connect or Twilio SMS. In these two scenario, because the user is interacting with your bot through a phone, you can use the phone number as one of the factors in a Multi-factor Authentication flow. In this example, we will ask for a four-digit PIN for the account associated with the phone number.
Let's start by defining the conversational interface of adding an international travel plan to the user's account.
-
Start by creating a new intent in the
InternationalPlan
bot you already created. Name it theApplyTravelPlan
intent -
Create a slot
Country
for the country the travel plan applies to.Expand for detailed instruction
- Name it
Country
- Use built-in type
AMAZON.Country
- For prompt, use
Which country are you going to?
- Name it
-
Create a new custom slot for plan name with 2 supported values:
basic
andpremium
and add it to the intent as slotplanName
.Expand for detailed instruction
-
On the left side bar, locate the Slot Types tab and click (+)
-
Use
TravelPlan
for slot name -
For description, use something like
name of international phone plans
. -
For Slot Resolution, because in this example our international phone plans only have two possible names, we will pick Restrict to Slot values and Synonyms (The "Expand Values" option is when the values are more open ended, e.g. book titles, company names, etc. )
-
For acceptable values, put an entry for
basic
and anotherpremium
-
Click Add Slot to intent
-
Name the slot
planName
and useWhich plan would you like to apply to your account?
for slot prompt
-
-
Add additional slots to collect information on the plan start date (
startDate
) and duration (numOfWeeks
)Expand for detailed instruction
- For start date:
- Name it
startDate
- Use built-in type
AMAZON.DATE
- For prompt, use
When would you like {planName} plan to start?
- Name it
- For duration:
- Name it
numOfWeeks
- Use built-in type
AMAZON.NUBMER
- For prompt, use
How many weeks will you need?
- Name it
- For start date:
-
Confirm that all 4 slots are marked Required
-
Add sample utterances (remember to add them one at a time):
{planName} Yes. {planName} plan apply {planName} plan to my account apply {planName} plan apply international plan to my account apply travel plan to my account I want to add travel plans to my account
-
For a request that has impact on the customer's account, you can leverage the Confirmation Prompt feature to confirm choices users have made before fulfilling the intent.
Expand for detailed instruction
-
Expand the Confirmation Prompt section and click the Confirmation prompt check mark
-
Fill in for Confirm
You want to use the {planName} plan in {Country} for {numOfWeeks} weeks starting {startDate}, is that right?
-
For Cancel (if the user says "no"), fill in
Ok. {planName} plan will not be added. How else can I help you?
-
-
Build and test the conversation flow
In the last module, we used AWS Lambda to fulfill the user's intent. Another powerful integration is using Lambda functions to validate user inputs. When you enable this feature, Lex invokes the specified Lambda function on each user input (utterance) after Amazon Lex recognizes the intent.
Here's how we can use this for our ApplyTravelPlan
intent:
-
Once Lex recognizes the intent to subscribe to a plan, the Lambda function can check if the user has been authenticated (through session attributes). If not, force the user to verify their identity before proceeding.
-
Validate that the
startDate
is in the future. -
Validate that the number of weeks is between 0 and 52.
-
Validate there's a corresponding plan for user's specified country.
Now, configure the lex-workshop-LexBotHandler
function for input validation for this intent.
Expand for detailed instruction
-
Under Lambda initialization and validation setting for the
ApplyTravelPlan
intent, pick thelex-workshop-LexBotHandler
Lambda function -
Click Save Intent to save the intent configuration
-
Build the bot and test it again. Note that the Lambda validation is kicking in to force the user to verify their identity before proceeding:
As discussed in the Identifying and authenticating the user section, if your user is interacting with your bot through a phone, the bot can look up their phone number and verify their identity by challenging them with one or more security questions. In this workshop we will use a four-digit PIN.
To handle the identity verification flow, create a new intent with a slot for user pin.
Expand for detailed instruction
-
Create a new intent
VerifyIdentity
-
Create a
pin
slot with a built-in slot typeAMAZON.FOUR_DIGIT_NUMBER
with a promptWhat's your pin code?
-
Be sure to mark it Required
-
Add sample utterances for the intent (one at a time):
{pin} my pin is {pin} verify my identity
-
Configure
lex-workshop-LexBotHandler
Lambda function for intent fulfillment: -
Save the intent
-
Build the bot
Note for testing the bot in Lex Console:
When the lex bot is integrated with phone call (Amazon Connect) or SMS text (Twilio), the user pin verification logic in the Lambda function uses the phone number of the user to look up the user and verify the corresponding pin.
In the bot testing window of the Lex Console, the userId is randomly generated by the Console and there's no "phone number" we can use to look up the user. To test the conversation flow during development, use the fake pin
1234
Now we are ready to fulfill the user's request to add plans to their account! Configure the fulfillment for the ApplyTravelPlan
intent to use the same Lambda function lex-workshop-LexBotHandler
Expand for detailed instruction
-
Go to the
ApplyTravelPlan
intent -
Under Fulfillment, select the
lex-workshop-LexBotHandler
Lambda function (you may need to grant permissions as before) -
We can add a response for when the Lambda function successfully adds the plan to the user's account:
- Response message:
{planName} plan in {Country} has been added to your account. Can I help you with anything else today?
- Wait for user reply: checked
- If user says "no" message:
Thank you. Have a nice day.
- (For spontaneity, you can add multiple messages and Lex will choose one at runtime.)
- Response message:
-
Save the intent
-
Build the bot
Now we have configured 3 intents:
ListInternationalPlans
- for users to inquire travel options they can add to phone planApplyTravelPlan
- for applying a user's selected travel plan to their accountVerifyIdentity
- for verifying user's identity
We can now test how these intents can work together to provide a streamlined customer experience:
Note that by leveraging session attributes, the Lex bot is able to transition between different intents and "remember" what information the user has already provided. In the above example, after user authentication, the bot is able to pick up where it left off and continue with fulfilling the intent to add the plan to the user's account.
For detailed explanation on how this works, expand here
If we take a look at the Inspect Response tab at this point, notice the below:
- After verifying the user, the bot remembers the user was in progress to add a plan, and set the
intentName
toApplyTravelPlan
(see yellow highlight) - After verifying the user, the bot sets the
sessionAttributes
to mark the logged in user (see red highlight) - After verifying the user, the bot identifies inputs that the user has provided in previous intents (e.g.
Country
andplanName
) and prompts the user to collect information that hasn't been provided (see green highlight)
See below for an example of a full conversation flow:
You can also verify the user's plan selections are being persisted in DynamoDB by checking the DynamoDB table.
Expand for detailed instruction
-
Go to the DynamoDB console
-
Select the table name starting with
lex-workshop-UserTravelPlansDDBTable
-
Verify you see the test user's applied plans and other attributes:
An alias is a pointer to a specific version of an Amazon Lex bot. Use an alias to allow client applications to point to a tested version of the bot while you iterate on bot design (e.g. you might have different aliases for prod
, staging
and dev
to represent different stages).
We will publish the bot with a dev
alias from the Lex console.
Expand for detailed instruction
Once you have a working Lex chatbot, you can choose to complete one or more of the following modules to integrate your Lex chatbot to different channels to interface with your customer: