• 沒有找到結果。

User: Types Thursday

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

Details of Information Flow

3. User: Types Thursday

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

3. User: Types Thursday.

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": "Thursday", "sessionAttributes": {}

}

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

}, "bot": {

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

"name": "ScheduleAppointment"

},

"userId": "u3fpr9gghj02zts7y5tpq5mm4din2xqy", "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 the user data.

• Amazon Lex sets the Date field in the currentIntent.slots slot to 2017-02-16.

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

c. The Lambda function validates the user input. This time the Lambda function determines that there are no appointments available on the specified date. It returns the following response to Amazon Lex, directing the service to again elicit a value for the appointment date.

{ "dialogAction": {

"slotToElicit": "Date",

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

{

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

}, another day which works for you?",

"contentType": "PlainText"

} },

"sessionAttributes": {

"bookingMap": "{\"2017-02-16\": []}"

}}

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

• dialogAction field:

• type – The Lambda function sets this value to ElicitSlot and resets the slotToElicit field to Date. The Lambda function also provides an appropriate message to convey to the user.

• responseCard – Returns a list of values for the Date slot.

• sessionAttributes - This time the Lambda function includes the bookingMap session attribute. Its value is the requested date of the appointment and available appointments (an empty object indicates that no appointments are available).

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: We do not have any availability on that date, is there another day which works for you? and the response card (if the client supports response cards).

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

• If the response card is shown, choose 2-15 (Wed) or type Wednesday.

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

a. The client sends the following PostText request to Amazon Lex:

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

"Content-Type":"application/json"

"Content-Encoding":"amz-1.0"

{ "inputText": "Wednesday", "sessionAttributes": { }

}

NoteThe Facebook Messenger client does not set any session attributes. If you want to maintain session states between requests, you must do so in the Lambda function. In a real application, you might need to maintain these session attributes in a backend database.

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": "2017-02-15",

"Time": null },

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

},

"bot": {

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

"name": "ScheduleAppointment"

},

"userId": "u3fpr9gghj02zts7y5tpq5mm4din2xqy", "invocationSource": "DialogCodeHook",

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

}

Amazon Lex updated currentIntent.slots by setting the Date slot to 2017-02-15.

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

{ "dialogAction": { only availability, does that work for you?",

"contentType": "PlainText"

},

"type": "ConfirmIntent",

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

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

} },

"sessionAttributes": {

"bookingMap": "{\"2017-02-15\": [\"10:00\", \"16:00\", \"16:30\"]}"

} }

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

• dialogAction field:

• type – The Lambda function sets this value to ConfirmIntent, directing Amazon Lex to obtain user confirmation of the appointment time suggested in the message.

• responseCard – Returns a list of yes/no values for the user to choose from. If the client supports response cards, it displays the response card, as shown in the following example:

• sessionAttributes - The Lambda function sets the bookingMap session attribute with its value set to the appointment date and available appointments on that date. In this example, these are 30-minute appointments. For a root canal that requires one hour, only 4 p.m. can be booked.

d. As indicated in the dialogAction.type in the Lambda function's response, Amazon Lex returns the following response to the client:

The client displays the message: What time on 2017-02-15 works for you? 4:00 p.m. is our only availability, does that work for you?

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