If you are using Amazon Lex V2, refer to the Amazon Lex V2 guide instead.
If you are using Amazon Lex V1, we recommend upgrading your bots to Amazon Lex V2. We are no longer adding new features to V1 and strongly recommend using V2 for all new bots.
Step 5 (Optional): Review the Details of the Information Flow (Console)
This section explains the flow of information between the client and Amazon Lex for each user input, including the integration of the Lambda function.
Note
The section assumes that the client sends requests to
Amazon Lex using the PostText
runtime API and shows
request and response details accordingly. For an example of the
information flow between the client and Amazon Lex in which
client uses the PostContent
API, see Step 2a
(Optional): Review the Details of the Spoken Information
Flow (Console) .
For more information about the PostText
runtime API
and additional details on the requests and responses shown in the
following steps, see PostText.
-
User: I would like to order some flowers.
-
The client (console) sends the following PostText request to Amazon Lex:
POST /bot/
OrderFlowers
/alias/$LATEST
/user/ignw84y6seypre4xly5rimopuri2xwnd
/text "Content-Type":"application/json" "Content-Encoding":"amz-1.0" { "inputText": "I would like to order some flowers", "sessionAttributes": {} }Both the request URI and the body provide information to Amazon Lex:
-
Request URI – Provides bot name (
OrderFlowers
), bot alias ($LATEST
), and user name (a random string identifying the user). The trailingtext
indicates that it is aPostText
API request (and notPostContent
). -
Request body – Includes the user input (
inputText
) and emptysessionAttributes
. When the client makes the first request, there are no session attributes. The Lambda function initiates them later.
-
-
From the
inputText
, Amazon Lex detects the intent (OrderFlowers
). This intent is configured with a Lambda function as a code hook for user data initialization and validation. Therefore, Amazon Lex invokes that Lambda function by passing the following information as event data:{ "messageVersion": "1.0", "invocationSource": "DialogCodeHook", "userId": "ignw84y6seypre4xly5rimopuri2xwnd", "sessionAttributes": {}, "bot": { "name": "OrderFlowers", "alias": null, "version": "$LATEST" }, "outputDialogMode": "Text", "currentIntent": { "name": "OrderFlowers", "slots": { "PickupTime": null, "FlowerType": null, "PickupDate": null }, "confirmationStatus": "None" } }
For more information, see Input Event Format.
In addition to the information that the client sent, Amazon Lex also includes the following additional data:
-
messageVersion
– Currently Amazon Lex supports only the 1.0 version. -
invocationSource
– Indicates the purpose of Lambda function invocation. In this case, it is to perform user data initialization and validation. At this time, Amazon Lex knows that the user has not provided all the slot data to fulfill the intent. -
currentIntent
information with all of the slot values set to null.
-
-
At this time, all the slot values are null. There is nothing for the Lambda function to validate. The Lambda function returns the following response to Amazon Lex:
{ "sessionAttributes": {}, "dialogAction": { "type": "Delegate", "slots": { "PickupTime": null, "FlowerType": null, "PickupDate": null } } }
For information about the response format, see Response Format.
Note the following:
-
dialogAction.type
– By setting this value toDelegate
, Lambda function delegates the responsibility of deciding the next course of action to Amazon Lex.Note
If Lambda function detects anything in the user data validation, it instructs Amazon Lex what to do next, as shown in the next few steps.
-
-
According to the
dialogAction.type
, Amazon Lex decides the next course of action. Because none of the slots are filled, it decides to elicit the value for theFlowerType
slot. It selects one of the value elicitation prompts ("What type of flowers would you like to order?") for this slot and sends the following response back to the client:The client displays the message in the response.
-
-
User: roses
-
The client sends the following PostText request to Amazon Lex:
POST /bot/
OrderFlowers
/alias/$LATEST
/user/ignw84y6seypre4xly5rimopuri2xwnd
/text "Content-Type":"application/json" "Content-Encoding":"amz-1.0" { "inputText": "roses", "sessionAttributes": {} }In the request body, the
inputText
provides user input. ThesessionAttributes
remains empty. -
Amazon Lex first interprets the
inputText
in the context of the current intent. The service remembers that it had asked the specific user for information about theFlowerType
slot. It updates the slot value in the current intent and invokes the Lambda function with the following event data:{ "messageVersion": "1.0", "invocationSource": "DialogCodeHook", "userId": "ignw84y6seypre4xly5rimopuri2xwnd", "sessionAttributes": {}, "bot": { "name": "OrderFlowers", "alias": null, "version": "$LATEST" }, "outputDialogMode": "Text", "currentIntent": { "name": "OrderFlowers", "slots": { "PickupTime": null, "FlowerType": "roses", "PickupDate": null }, "confirmationStatus": "None" } }
Note the following:
-
invocationSource
– continues to beDialogCodeHook
(we are simply validating user data). -
currentIntent.slots
– Amazon Lex has updated theFlowerType
slot to roses.
-
-
According to the
invocationSource
value ofDialogCodeHook
, the Lambda function performs user data validation. It recognizesroses
as a valid slot value (and setsPrice
as a session attribute) and returns the following response to Amazon Lex.{ "sessionAttributes": { "Price": 25 }, "dialogAction": { "type": "Delegate", "slots": { "PickupTime": null, "FlowerType": "roses", "PickupDate": null } } }
Note the following:
-
sessionAttributes
– Lambda function has addedPrice
(of the roses) as a session attribute. -
dialogAction.type
– is set toDelegate
. The user data was valid so the Lambda function directs Amazon Lex to choose the next course of action.
-
-
According to the
dialogAction.type
, Amazon Lex chooses the next course of action. Amazon Lex knows it needs more slot data so it picks the next unfilled slot (PickupDate
) with the highest priority according to the intent configuration. Amazon Lex selects one of the value-elicitation prompt messages—"What day do you want the roses to be picked up?"—for this slot according to the intent configuration, and then sends the following response back to the client:The client simply displays the message in the response – "What day do you want the roses to be picked up?."
-
-
User: tomorrow
-
The client sends the following PostText request to Amazon Lex:
POST /bot/
OrderFlowers
/alias/$LATEST
/user/ignw84y6seypre4xly5rimopuri2xwnd
/text "Content-Type":"application/json" "Content-Encoding":"amz-1.0" { "inputText": "tomorrow", "sessionAttributes": { "Price": "25" } }In the request body,
inputText
provides user input and the client passes the session attributes back to the service. -
Amazon Lex remembers the context—that it was eliciting data for the
PickupDate
slot. In this context, it knows theinputText
value is for thePickupDate
slot. Amazon Lex then invokes the Lambda function by sending the following event:{ "messageVersion": "1.0", "invocationSource": "DialogCodeHook", "userId": "ignw84y6seypre4xly5rimopuri2xwnd", "sessionAttributes": { "Price": "25" }, "bot": { "name": "OrderFlowersCustomWithRespCard", "alias": null, "version": "$LATEST" }, "outputDialogMode": "Text", "currentIntent": { "name": "OrderFlowers", "slots": { "PickupTime": null, "FlowerType": "roses", "PickupDate": "2017-01-05" }, "confirmationStatus": "None" } }
Amazon Lex has updated the
currentIntent.slots
by setting thePickupDate
value. Also note that the service passes thesessionAttributes
as it is to the Lambda function. -
As per
invocationSource
value ofDialogCodeHook
, the Lambda function performs user data validation. It recognizesPickupDate
slot value is valid and returns the following response to Amazon Lex:{ "sessionAttributes": { "Price": 25 }, "dialogAction": { "type": "Delegate", "slots": { "PickupTime": null, "FlowerType": "roses", "PickupDate": "2017-01-05" } } }
Note the following:
-
sessionAttributes
– No change. -
dialogAction.type
– is set toDelegate
. The user data was valid, and the Lambda function directs Amazon Lex to choose the next course of action.
-
-
According to the
dialogAction.type
, Amazon Lex chooses the next course of action. Amazon Lex knows it needs more slot data so it picks the next unfilled slot (PickupTime
) with the highest priority according to the intent configuration. Amazon Lex selects one of the prompt messages ("Deliver the roses at what time on 2017-01-05?") for this slot according to the intent configuration and sends the following response back to the client:The client displays the message in the response – "Deliver the roses at what time on 2017-01-05?"
-
-
User: 4 pm
-
The client sends the following PostText request to Amazon Lex:
POST /bot/
OrderFlowers
/alias/$LATEST
/user/ignw84y6seypre4xly5rimopuri2xwnd
/text "Content-Type":"application/json" "Content-Encoding":"amz-1.0" { "inputText": "4 pm", "sessionAttributes": { "Price": "25" } }In the request body,
inputText
provides user input. The client passes thesessionAttributes
in the request. -
Amazon Lex understands context. It understands that it was eliciting data for the
PickupTime
slot. In this context, it knows that theinputText
value is for thePickupTime
slot. Amazon Lex then invokes the Lambda function by sending the following event:{ "messageVersion": "1.0", "invocationSource": "DialogCodeHook", "userId": "ignw84y6seypre4xly5rimopuri2xwnd", "sessionAttributes": { "Price": "25" }, "bot": { "name": "OrderFlowersCustomWithRespCard", "alias": null, "version": "$LATEST" }, "outputDialogMode": "Text", "currentIntent": { "name": "OrderFlowers", "slots": { "PickupTime": "16:00", "FlowerType": "roses", "PickupDate": "2017-01-05" }, "confirmationStatus": "None" } }
Amazon Lex has updated the
currentIntent.slots
by setting thePickupTime
value. -
According to the
invocationSource
value ofDialogCodeHook
, the Lambda function performs user data validation. It recognizesPickupDate
slot value is valid and returns the following response to Amazon Lex.{ "sessionAttributes": { "Price": 25 }, "dialogAction": { "type": "Delegate", "slots": { "PickupTime": "16:00", "FlowerType": "roses", "PickupDate": "2017-01-05" } } }
Note the following:
-
sessionAttributes
– No change in session attribute. -
dialogAction.type
– is set toDelegate
. The user data was valid so the Lambda function directs Amazon Lex to choose the next course of action.
-
-
At this time Amazon Lex knows it has all the slot data. This intent is configured with a confirmation prompt. Therefore, Amazon Lex sends the following response to the user asking for confirmation before fulfilling the intent:
The client simply displays the message in the response and waits for the user response.
-
-
User: Yes
-
The client sends the following PostText request to Amazon Lex:
POST /bot/
OrderFlowers
/alias/$LATEST
/user/ignw84y6seypre4xly5rimopuri2xwnd
/text "Content-Type":"application/json" "Content-Encoding":"amz-1.0" { "inputText": "yes", "sessionAttributes": { "Price": "25" } } -
Amazon Lex interprets the
inputText
in the context of confirming the current intent. Amazon Lex understands that the user wants to proceed with the order. This time Amazon Lex invokes the Lambda function to fulfill the intent by sending the following event, which sets theinvocationSource
toFulfillmentCodeHook
in the event it sends to the Lambda function. Amazon Lex also sets theconfirmationStatus
toConfirmed
.{ "messageVersion": "1.0", "invocationSource": "FulfillmentCodeHook", "userId": "ignw84y6seypre4xly5rimopuri2xwnd", "sessionAttributes": { "Price": "25" }, "bot": { "name": "OrderFlowersCustomWithRespCard", "alias": null, "version": "$LATEST" }, "outputDialogMode": "Text", "currentIntent": { "name": "OrderFlowers", "slots": { "PickupTime": "16:00", "FlowerType": "roses", "PickupDate": "2017-01-05" }, "confirmationStatus": "Confirmed" } }
Note the following:
-
invocationSource
– This time Amazon Lex set this value toFulfillmentCodeHook
, directing the Lambda function to fulfill the intent. -
confirmationStatus
– is set toConfirmed
.
-
-
This time, the Lambda function fulfills the
OrderFlowers
intent, and returns the following response:{ "sessionAttributes": { "Price": "25" }, "dialogAction": { "type": "Close", "fulfillmentState": "Fulfilled", "message": { "contentType": "PlainText", "content": "Thanks, your order for roses has been placed and will be ready for pickup by 16:00 on 2017-01-05" } } }
Note the following:
-
Sets the
dialogAction.type
– The Lambda function sets this value toClose
, directing Amazon Lex to not expect a user response. -
dialogAction.fulfillmentState
– is set to Fulfilled and includes an appropriatemessage
to convey to the user.
-
-
Amazon Lex reviews the
fulfillmentState
and sends the following response back to the client.Amazon Lex then returns the following to the client:
Note that:
-
dialogState
– Amazon Lex sets this value tofulfilled
. -
message
– is the same message that the Lambda function provided.
The client displays the message.
-
-
-
Now test the bot again. To establish a new (user) context, choose the Clear link in the test window. Now provide invalid slot data for the
OrderFlowers
intent. This time the Lambda function performs the data validation, resets invalid slot data value to null, and asks Amazon Lex to prompt the user for valid data. For example, try the following:-
Jasmine as the flower type (it is not one of the supported flower types).
-
Yesterday as the day when you want to pick up the flowers.
-
After placing your order, enter another flower type instead of replying "yes" to confirm the order. In response, the Lambda function updates the
Price
in the session attribute, keeping a running total of flower orders.
The Lambda function also performs the fulfillment activity.
-
Next Step
Step 6: Update the Intent Configuration to Add an Utterance (Console)