• 沒有找到結果。

User: Types Book an appointment

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

Details of Information Flow

1. User: Types Book an appointment

The section assumes client sends requests to Amazon Lex using the PostText runtime API and shows request/response details accordingly. For more information about the PostText runtime API, see PostText (p. 444).

NoteFor an example of information flow between client and Amazon Lex in which client uses the PostContent API, see Step 2a (Optional): Review the Details of the Spoken Information Flow (Console) (p. 75).

 

• Data flow between Amazon Lex and the Lamba function. For more information, see Lambda Function Input Event and Response Format (p. 139).

Note

The example assumes that you are using the Facebook Messenger client, which does not pass session attributes in the request to Amazon Lex. Accordingly, the example requests shown in this section show empty sessionAttributes. If you test the bot using the client provided in the Amazon Lex console, the client includes the session attributes.

This section describes what happens after each user input.

1. User: Types Book an appointment.

a. The client (console) sends the following PostContent (p. 434) request to Amazon Lex:

POST /bot/ScheduleAppointment/alias/$LATEST/user/bijt6rovckwecnzesbthrr1d7lv3ja3n/

text

"Content-Type":"application/json"

"Content-Encoding":"amz-1.0"

{ "inputText":"book appointment", "sessionAttributes":{}

}

Both the request URI and the body provide information to Amazon Lex:

• Request URI – Provides the bot name (ScheduleAppointment), the bot alias ($LATEST), and the user name ID. The trailing text indicates that it is a PostText (not PostContent) API request.

• Request body – Includes the user input (inputText) and empty sessionAttributes.

b. From the inputText, Amazon Lex detects the intent (MakeAppointment). The service invokes the Lambda function, which is configured as a code hook, to perform initialization and validation by passing the following event. For details, see Input Event Format (p. 139).

{ "confirmationStatus": "None"

}, "bot": {

"alias": null, "version": "$LATEST",

"name": "ScheduleAppointment"

},

"userId": "bijt6rovckwecnzesbthrr1d7lv3ja3n", "invocationSource": "DialogCodeHook",

"outputDialogMode": "Text", "messageVersion": "1.0", "sessionAttributes": {}

}

In addition to the information sent by the client, Amazon Lex also includes the following data:

• currentIntent – Provides current intent information.

• invocationSource – Indicates the purpose of the Lambda function invocation. In this case, the purpose is to perform user data initialization and validation. (Amazon Lex knows that the user has not provided all of the slot data to fulfill the intent yet.)

• messageVersion – Currently Amazon Lex supports only the 1.0 version.

c. At this time, all of the slot values are null (there is nothing to validate). The Lambda function returns the following response to Amazon Lex, directing the service to elicit information for the AppointmentType slot. For information about the response format, see Response Format (p. 144).

{ "dialogAction": {

"slotToElicit": "AppointmentType", "intentName": "MakeAppointment", "responseCard": {

"text": "root canal (60 min)", "value": "root canal"

}, {

"text": "whitening (30 min)", "value": "whitening"

} ],

"subTitle": "What type of appointment would you like to schedule?",

"title": "Specify Appointment Type"

} ],

"version": 1,

"contentType": "application/vnd.amazonaws.card.generic"

},

"slots": {

"AppointmentType": null, "Date": null,

"Time": null },

"type": "ElicitSlot", "message": {

"content": "What type of appointment would you like to schedule?", "contentType": "PlainText"

} },

"sessionAttributes": {}

}

The response includes the dialogAction and sessionAttributes fields. Among other things, the dialogAction field returns the following fields:

• type – By setting this field to ElicitSlot, the Lambda function directs Amazon Lex to elicit the value for the slot specified in the slotToElicit field. The Lambda function also provides a message to convey to the user.

• responseCard – Identifies a list of possible values for the AppointmentType slot. A client that supports response cards (for example, the Facebook Messenger) displays a response card to allow the user to choose an appointment type, as follows:

d. As indicated by the dialogAction.type in the response from the Lambda function, Amazon Lex sends the following response back to the client:

The client reads the response, and then displays the message: "What type of appointment would you like to schedule?" and the response card (if the client supports response cards).

2. User: Depending on the client, the user has two options:

• If the response card is shown, choose root canal (60 min) or type root canal.

• If the client does not support response cards, type root canal.

a. The client sends the following PostText request to Amazon Lex (line breaks have been added for readability):

POST /bot/BookTrip/alias/$LATEST/user/bijt6rovckwecnzesbthrr1d7lv3ja3n/text

"Content-Type":"application/json"

"Content-Encoding":"amz-1.0"

{ "inputText": "root canal", "sessionAttributes": {}

}

b. Amazon Lex invokes the Lambda function for user data validation by sending the following event as a parameter:

{

"currentIntent": { "slots": {

"AppointmentType": "root canal", "Date": null,

"Time": null },

"name": "MakeAppointment", "confirmationStatus": "None"

}, "bot": {

"alias": null, "version": "$LATEST",

"name": "ScheduleAppointment"

},

"userId": "bijt6rovckwecnzesbthrr1d7lv3ja3n", "invocationSource": "DialogCodeHook",

"outputDialogMode": "Text", "messageVersion": "1.0",

"sessionAttributes": {}

}

In the event data, note the following:

• invocationSource continues to be DialogCodeHook. In this step, we are just validating user data.

• Amazon Lex sets the AppointmentType field in the currentIntent.slots slot to root canal.

• Amazon Lex simply passes the sessionAttributes field between the client and the Lambda function.

c. The Lambda function validates the user input and returns the following response to Amazon Lex, directing the service to elicit a value for the appointment date.

{ "dialogAction": {

"slotToElicit": "Date",

"intentName": "MakeAppointment", "responseCard": {

"contentType": "application/vnd.amazonaws.card.generic"

},

"sessionAttributes": {}

}

Again, the response includes the dialogAction and sessionAttributes fields. Among other things, the dialogAction field returns the following fields:

• type – By setting this field to ElicitSlot, the Lambda function directs Amazon Lex to elicit the value for the slot specified in the slotToElicit field. The Lambda function also provides a message to convey to the user.

• responseCard – Identifies a list of possible values for the Date slot. A client that supports response cards (for example, Facebook Messenger) displays a response card that allows the user to choose an appointment date:

Although the Lambda function returned five dates, the client (Facebook Messenger) has a limit of three buttons for a response card. Therefore, you see only the first three values in the screen shot.

These dates are hard coded in the Lambda function. In a production application, you might use a calendar to get available dates in real time. Because the dates are dynamic, you must generate the response card dynamically in the Lambda function.

d. Amazon Lex notices the dialogAction.type and returns a response to the client that includes information from the Lambda function's response.

The client displays the message: When would you like to schedule your root canal? and the response card (if the client supports response cards).

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