How do I use an initialization/validation or fulfillment Lambda function to change the dialog flow of my Amazon Lex bot?

3 minute read
0

I want to use an initialization/validation or fulfillment AWS Lambda function to change the dialog flow of my Amazon Lex bot.

Resolution

Note: The example in this article uses version 2 of the Amazon Lex console. If you use version 1, then choose Switch to the new Lex v2 console in the navigation pane.

Lambda response syntax specifies the format that Amazon Lex expects the Lambda function response to be in. Make sure to include the required fields in the response.

Use ElicitSlot type to change the dialog flow

To use the ElicitSlot type to change dialog flow, pass the response from the Lambda code hook in the following format:

{
    "sessionState": {
        "dialogAction": {
            "slotToElicit": "<slot-name-to-elicit>",
            "type": "ElicitSlot"
        },
        "intent": {
            "name": "<intent-name-to-elicit>",
            "state": "InProgress"
        }
    }
}

After it returns the response, Amazon Lex elicits a slot called slot-name-to-elicit that belongs to the intent-name-to-elicit intent.

For example, you have an intent CreateUser with a slot that prompts for a username. Use a Lambda function to check if the username already exists. If the username exists, then elicit the username slot again for the same intent.

Use ElicitIntent type to change the dialog flow

To use the ElicitIntent type to change dialog flow, pass the response from the Lambda code hook in the following format:

{
        "sessionState": {
        "dialogAction": { 
            "type": "ElicitIntent"
         }
     },
     "messages": [{
         "contentType": "<content-type>",
         "content": "<message>"
     }]
 }

After Amazon Lex returns the response, the user sees the message that's specified in the message placeholder. The next user input is taken as an intent utterance and invokes the intent with the highest nluConfidence score.

For example, if your bot fulfills an intent for your user, then put the bot in a Listen state. The user can then provide another utterance to invoke another intent.

Use Delegate type to change the dialog flow

You can use Delegate type to change the start of an intent when you specify the new intent. To use the Delegate type, pass the response from the Lambda code hook in the following format:

{
    'sessionState': {
        'dialogAction': {
            'type': 'Delegate'
        },
        'intent': {
            'name': '<intent-name-to-elicit>',
            'state': 'InProgress'
        }
    }
}

For example, your bot has intents A, B, and C. If the user provides a response to a slot in intent A, then the Lambda function checks the user input. Then, the function invokes either intent B or C.

Related information

amazon-lex-v2-dialogation on the GitHub website

AWS OFFICIAL
AWS OFFICIALUpdated 2 months ago
2 Comments

How does on switch intents. this example needs to be flushed out with this scenario

replied 10 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 10 months ago