qpwoeiruty
qpwoeiruty

Reputation: 11

AWS Lex(v2) and Lambda

I am currently working on an AWS Lex chatbot which will integrate with Lambda. The chatbot's goal is to provide the distance between two cities and return an amount. Here is an example of a chat: Chat example

However, I keep getting "Intent is fulfilled".

Here is a copy of the Lambda code:

import json
import boto3
from boto3.dynamodb.conditions import Key

def lambda_handler(event, context):

    destination = event["sessionState"]["intent"]["slots"]["Destination"]["value"]["interpretedValue"]
    source = event["sessionState"]["intent"]["slots"]["Source"]["value"]["interpretedValue"]
    distance = get_distance(source, destination)

    return {
    "sessionState": {
        "dialogAction": {
            "type": "Close"
        },
        "intent": {
            "name": "DistanceIntent"
            "state": "Fulfilled"
        },
        "messages": [
            {
                "contentType": "PlainText",
                "content": f"{distance}"
            }
        ]
    }
}

Could you please explain what is wrong?

Upvotes: 0

Views: 613

Answers (2)

fxfxfx
fxfxfx

Reputation: 1

For Lex v2 (current since 2022):

"messages" should not be nested inside sessionState. Only dialogAction and intent go inside sessionState.

return {
"sessionState": {
    "dialogAction": {
        "type": "Close"
    },
    "intent": {
        "name": "DistanceIntent"
        "state": "Fulfilled"
    },
},
"messages": [
        {
            "contentType": "PlainText",
            "content": f"{distance}"
        }
    ]
}

documentation: https://docs.aws.amazon.com/lexv2/latest/dg/lambda-response-format.html

dialogAction.type must be "Close" and intent must be provided. If you use type "Delegate" your custom message will be ignored.

Upvotes: 0

Reegz
Reegz

Reputation: 579

It would appear that you have yet linked your intent to the Lambda function. There are two steps that are necessary.

  1. Link the Lambda function to the bot
  • To do this, click on the "Test" bot button
  • Click the settings wheel at the top of the test chat window
  • In the settings window that slides out, choose a Lambda function to associate with your bot
  • Save the changes
  1. Configure the intent to use the Lambda for fulfillment
  • Open your intent and scroll down to the "Fulfillment" tab
  • Click on "Advanced Options" button
  • In the window that opens, look for the "Fulfillment Lambda code hook" section
  • Check the "Use a Lambda function for fulfillment" box
  • Save your changes

This will now ensure that Lex defers all fulfillment to the Lambda function.

An important difference between v1 and v2 of Lex is that you can only configure 1 Lambda function for the entire bot in Lex v2. You will need to consider using a router function for more flexibility. Take a look at the following guide: https://docs.aws.amazon.com/lexv2/latest/dg/lambda-attach.html

Upvotes: 0

Related Questions