• 沒有找到結果。

Overview of the Lambda Function Blueprint (lex- (lex-make-appointment-python)

在文檔中 Amazon Lex (頁 180-184)

The Lambda function blueprint (lex-make-appointment-python) is a code hook for bots that you create using the ScheduleAppointment bot blueprint.

This Lambda function blueprint code can perform both initialization/validation and fulfillment tasks.

• The Lambda function code showcases a dynamic conversation that is based on example availability for a dentist appointment (in real applications, you might use a calendar). For the day or date that the user specifies, the code is configured as follows:

• If there are no appointments available, the Lambda function returns a response directing Amazon Lex to prompt the user for another day or date (by setting the dialogAction type to ElicitSlot). For more information, see Response Format (p. 144).

• If there is only one appointment available on the specified day or date, the Lambda function suggests the available time in the response and directs Amazon Lex to obtain user confirmation by setting the dialogAction in the response to ConfirmIntent. This illustrates how you can improve the user experience by proactively suggesting the available time for an appointment.

• If there are multiple appointments available, the Lambda function returns a list of available times in the response to Amazon Lex. Amazon Lex returns a response to the client with the message from the Lambda function.

• As the fulfillment code hook, the Lambda function returns a summary message indicating that an appointment is scheduled (that is, the intent is fulfilled).

NoteIn this example, we show how to use response cards. The Lambda function constructs and returns a response card to Amazon Lex. The response card lists available days and times as buttons to choose from. When testing the bot using the client provided by the Amazon Lex console, you cannot see the response card. To see it, you must integrate the bot with a messaging platform, such as Facebook Messenger. For instructions, see Integrating an Amazon Lex Bot with Facebook Messenger (p. 152). For more information about response cards, see Managing Messages (p. 10).

When Amazon Lex invokes the Lambda function, it passes event data as input. One of the event fields is invocationSource, which the Lambda function uses to choose between an input validation and fulfillment activity. For more information, see Input Event Format (p. 139).

Next Step

Step 1: Create an Amazon Lex Bot (p. 174)

Step 1: Create an Amazon Lex Bot

In this section, you create an Amazon Lex bot using the ScheduleAppointment blueprint, which is provided in the Amazon Lex console.

1. Sign in to the AWS Management Console and open the Amazon Lex console at https://

console.aws.amazon.com/lex/.

2. On the Bots page, choose Create.

3. On the Create your Lex bot page, do the following:

• Choose the ScheduleAppointment blueprint.

• Leave the default bot name (ScheduleAppointment).

4. Choose Create.

This step saves and builds the bot. The console sends the following requests to Amazon Lex during the build process:

• Create a new version of the slot types (from the $LATEST version). For information about slot types defined in this bot blueprint, see Overview of the Bot Blueprint (ScheduleAppointment) (p. 173).

• Create a version of the MakeAppointment intent (from the $LATEST version). In some cases, the console sends a request for the update API operation before creating a new version.

• Update the $LATEST version of the bot.

At this time, Amazon Lex builds a machine learning model for the bot. When you test the bot in the console, the console uses the runtime API to send user input back to Amazon Lex. Amazon Lex then uses the machine learning model to interpret the user input.

5. The console shows the ScheduleAppointment bot. On the Editor tab, review the preconfigured intent (MakeAppointment) details.

6. Test the bot in the test window. Use the following screen shot to engage in a test conversation with your bot:

Note the following:

• From the initial user input ("Book an appointment"), the bot infers the intent (MakeAppointment).

• The bot then uses the configured prompts to get slot data from the user.

• The bot blueprint has the MakeAppointment intent configured with the following confirmation prompt:

{Time} is available, should I go ahead and book your appointment?

After the user provides all of the slot data, Amazon Lex returns a response to the client with a confirmation prompt as the message. The client displays the message for the user:

16:00 is available, should I go ahead and book your appointment?

Notice that the bot accepts any appointment date and time values because you don't have any code to initialize or validate the user data. In the next section, you add a Lambda function to do this.

Next Step

Step 2: Create a Lambda Function (p. 176)

Step 2: Create a Lambda Function

In this section, you create a Lambda function using a blueprint (lex-make-appointment-python) that is provided in the Lambda console. You also test the Lambda function by invoking it using sample Amazon Lex event data that is provided by the console.

1. Sign in to the AWS Management Console and open the AWS Lambda console at https://

console.aws.amazon.com/lambda/.

2. Choose Create a Lambda function.

3. For Select blueprint, type lex to find the blueprint, and then choose the lex-make-appointment-python blueprint.

4. Configure the Lambda function as follows.

• Type the Lambda function name (MakeAppointmentCodeHook).

• For the role, choose Create a new role from template(s), and then type a role name.

• Leave other default values.

5. Choose Create Function.

6. If you are using a locale other than English (US) (en-US), update the intent names as described in Updating a Blueprint for a Specific Locale (p. 149).

7. Test the Lambda function.

a. Choose Actions, and then chooseConfigure test event.

b. From the Sample event template list, choose Lex-Make Appointment (preview). This sample event uses the Amazon Lex request/response model, with values set to match a request from your Amazon Lex bot. For information about the Amazon Lex request/response model, see Using Lambda Functions (p. 139).

c. Choose Save and test.

d. Verify that the Lambda function ran successfully. The response in this case matches the Amazon Lex response model.

Next Step

Step 3: Update the Intent: Configure a Code Hook (p. 177)

Step 3: Update the Intent: Configure a Code Hook

In this section, you update the configuration of the MakeAppointment intent to use the Lambda function as a code hook for the validation and fulfillment activities.

1. In the Amazon Lex console, select the ScheduleAppointment bot. The console shows the MakeAppointment intent. Modify the intent configuration as follows.

NoteYou can update only the $LATEST versions of any of the Amazon Lex resources, including the intents. Make sure that the intent version is set to $LATEST. You have not published a version of your bot yet, so it should still be the $LATEST version in the console.

a. In the Options section, choose Initialization and validation code hook, and then choose the Lambda function from the list.

b. In the Fulfillment section, choose AWS Lambda function, and then choose the Lambda function from the list.

c. Choose Goodbye message, and type a message.

2. Choose Save, and then choose Build.

3. Test the bot.

Next Step

Step 4: Deploy the Bot on the Facebook Messenger Platform (p. 178)

Step 4: Deploy the Bot on the Facebook Messenger Platform

In the preceding section, you tested the ScheduleAppointment bot using the client in the Amazon Lex console. Currently, the Amazon Lex console does not support response cards. To test the dynamically generated response cards that the bot supports, deploy the bot on the Facebook Messenger platform and test it.

For instructions, see Integrating an Amazon Lex Bot with Facebook Messenger (p. 152).

Next Step

在文檔中 Amazon Lex (頁 180-184)