Skip to content

Latest commit

 

History

History
 
 

02_LexBotSubscribeService

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Module 2: Handle customer requests to subscribe to services

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.

Maintaining conversation session context

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:

session attributes in the list intent

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).

Identifying and authenticating the user

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.

Implementation Instructions

2A: Add intent to apply international plans to the user's account

Let's start by defining the conversational interface of adding an international travel plan to the user's account.

Conversational interface

  1. Start by creating a new intent in the InternationalPlan bot you already created. Name it the ApplyTravelPlan intent

  2. 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?
  3. Create a new custom slot for plan name with 2 supported values: basic and premium and add it to the intent as slot planName.

    Expand for detailed instruction

    1. On the left side bar, locate the Slot Types tab and click (+)

    2. Use TravelPlan for slot name

    3. For description, use something like name of international phone plans.

    4. 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. )

    5. For acceptable values, put an entry for basic and another premium

      create new slot screenshot
    6. Click Add Slot to intent

    7. Name the slot planName and use Which plan would you like to apply to your account? for slot prompt

      create new slot screenshot
  4. Add additional slots to collect information on the plan start date (startDate) and duration (numOfWeeks)

    Expand for detailed instruction

    1. For start date:
      • Name it startDate
      • Use built-in type AMAZON.DATE
      • For prompt, use When would you like {planName} plan to start?
    2. For duration:
      • Name it numOfWeeks
      • Use built-in type AMAZON.NUBMER
      • For prompt, use How many weeks will you need?
  5. Confirm that all 4 slots are marked Required

    create new slot screenshot
  6. 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
    
  7. 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

    1. Expand the Confirmation Prompt section and click the Confirmation prompt check mark

    2. Fill in for Confirm

      You want to use the {planName} plan in {Country} for {numOfWeeks} weeks starting {startDate}, is that right?
      
    3. For Cancel (if the user says "no"), fill in

      Ok. {planName} plan will not be added. How else can I help you?
      
      configure validation Lambda screenshot
  8. Build and test the conversation flow

    Expand for detailed instruction

    1. Click Save Intent to save the intent configuration

    2. Click Build at the top right of the page to build the bot

    3. Once the build complete, use the Test Bot window to test the conversation flow for this intent

      test bot screenshot

Validate input with AWS Lambda

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

  1. Under Lambda initialization and validation setting for the ApplyTravelPlan intent, pick the lex-workshop-LexBotHandler Lambda function

    configure validation Lambda screenshot
  2. Click Save Intent to save the intent configuration

  3. 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:

    test bot that enforces authentication

2B: Add intent to verify user identity

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

  1. Create a new intent VerifyIdentity

  2. Create a pin slot with a built-in slot type AMAZON.FOUR_DIGIT_NUMBER with a prompt What's your pin code?

    configure the pin slot
  3. Be sure to mark it Required

  4. Add sample utterances for the intent (one at a time):

    {pin}
    my pin is {pin}
    verify my identity
    
  5. Configure lex-workshop-LexBotHandler Lambda function for intent fulfillment:

    configure the pin slot
  6. Save the intent

  7. 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

2C: Configure fulfillment for adding international plans

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

  1. Go to the ApplyTravelPlan intent

  2. Under Fulfillment, select the lex-workshop-LexBotHandler Lambda function (you may need to grant permissions as before)

  3. 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.)
    configure the pin slot
  4. Save the intent

  5. Build the bot

2D: Test full flow

Now we have configured 3 intents:

  • ListInternationalPlans - for users to inquire travel options they can add to phone plan
  • ApplyTravelPlan - for applying a user's selected travel plan to their account
  • VerifyIdentity - for verifying user's identity

We can now test how these intents can work together to provide a streamlined customer experience:

configure the pin slot

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 to ApplyTravelPlan (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 and planName) and prompts the user to collect information that hasn't been provided (see green highlight)
configure the pin slot

See below for an example of a full conversation flow:

configure the pin slot

You can also verify the user's plan selections are being persisted in DynamoDB by checking the DynamoDB table.

Expand for detailed instruction

  1. Go to the DynamoDB console

  2. Select the table name starting with lex-workshop-UserTravelPlansDDBTable

    configure the pin slot
  3. Verify you see the test user's applied plans and other attributes:

    configure the pin slot

2E: Publish the bot

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

  1. Click Publish button (upper right side on the console)

  2. Use dev for alias

  3. Click Publish

    configure the pin slot

Next step

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: