2

I am using AWS Lex and AWS Lambda for creating a chatbot. The request and response format are as follows Event being passed to AWS Lambda

{
  "alternativeIntents": [
    {
      "intentName": "AMAZON.FallbackIntent",
      "nluIntentConfidence": null,
      "slots": {}
    }
  ],
  "botVersion": "$LATEST",
  "dialogState": "ConfirmIntent",
  "intentName": "OrderBeverage",
  "message": "you want to order 2 pints of beer",
  "messageFormat": "PlainText",
  "nluIntentConfidence": {
    "score": 0.92
  },
  "responseCard": null,
  "sentimentResponse": null,
  "sessionAttributes": {},
  "sessionId": "2021-05-10T09:13:06.841Z-bSWmdHVL",
  "slotToElicit": null,
  "slots": {
    "Drink": "beer",
    "Quantity": "2",
    "Unit": "pints"
  }
}

Response Format-

{
  "statusCode": 200,
  "dialogAction": {
    "type": "Close",
    "fulfillmentState": "Fulfilled",
    "message": {
      "contentType": "PlainText",
      "content": "Message to convey to the user. For example, Thanks, your pizza has been ordered."
    }
  }
}

AWS LAMBDA PYTHON IMPLEMENTATION-

    import json

def lambda_handler(event, context):
    # TODO implement
    slots= event["slots"];
    drink,qty,unit= slots["Drink"], slots["Quantity"], slots["Unit"]
    retStr= "your order of "+qty+" "+unit+ " of "+drink+ " is coming right up!";
    return {"dialogAction": {
    "type": "Close",
    "fulfillmentState": "Fulfilled",
    "message": {
      "contentType": "PlainText",
      "content": retStr
    },
  }
  }

The formats are in accordance with the documentation, however still getting error in processing lambda response. What is the issue?

Jonas
  • 121,568
  • 97
  • 310
  • 388
alphason
  • 41
  • 1
  • 9
  • Try to print the event that you are receiving – KnowledgeGainer May 10 '21 at 10:19
  • @KnowledgeGainer updated the description – alphason May 10 '21 at 10:22
  • What is the error that you are getting ? can you include the cloud watch log error – KnowledgeGainer May 10 '21 at 10:29
  • When i am manually passing the event from aws lambda i am getting the json reponse object however, the content in the message flag isnt showing up on the lex test bot and giving the error "An error has occurred: The server encountered an error processing the Lambda response" – alphason May 10 '21 at 10:39
  • try to use logger to log each and every value and check in cloud watch for better debugging – KnowledgeGainer May 10 '21 at 10:41
  • i am new to this, the invocation of lambda from lex is not showing up on cloud watch for some reason – alphason May 10 '21 at 10:46
  • If your lambda is using basicExecutionRole which was automatically created at the time of lambda creation, then logs will be created, if you are doing all this in a dev environment, then try to clear the logs of that lambda and start a trigger. – KnowledgeGainer May 10 '21 at 10:50
  • As per my understanding I am able to extract the slot values through the event parameter. I am also able to return a hardcoded string in response. However when I am using the received slots in the content string to be returned, lex s for somereason unable to parse it – alphason May 10 '21 at 10:57

1 Answers1

2

This error occurs when the execution of the Lambda function fails and throws an error back to Amazon Lex.

I have attempted to recreate your environment using the python code shared and the test input event.

The output format that you have specified in the original post is correct. Your problem appears to lie with the input test event. The input message that you are using differs from what Lex is actually sending to your Lambda function.

Try adding some additional debugging to your Lambda function to log the event that Lex passes into it and then use the logged event as your new test event.

Ensure that you have CloudWatch logging enabled for the Lambda function so that you can view the input message in the logs.

Here's how my Lambda function looks:

import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def dispatch(event):
    # TODO implement
    slots= event["slots"];
    drink,qty,unit= slots["Drink"], slots["Quantity"], slots["Unit"]
    retStr= "your order of "+qty+" "+unit+ " of "+drink+ " is coming right up!";
    return {"dialogAction": {
        "type": "Close",
        "fulfillmentState": "Fulfilled",
        "message": {
            "contentType": "PlainText",
            "content": retStr
        },
    }}

def lambda_handler(event, context):
    logger.debug('event={}'.format(event))
    response = dispatch(event)
    logger.debug(response)
    return response

Now if you test via the Lex console you will find your error in the CloudWatch logs.:

[ERROR] KeyError: 'slots' Traceback (most recent call last): File "/var/task/lambda_function.py", line 33, in lambda_handler response = dispatch(event) File "/var/task/lambda_function.py", line 19, in dispatch slots= event["slots"];

Using this error trace and the logged event, you should see that slots is nested within currentIntent.

You will need to update your code to extract the slot values from the correct place.

Trust this helps you.

Reegz
  • 511
  • 1
  • 6
  • 13