-1

''' Trying to get response back as name of the intent from lambda for Amazon Lex v2. It can be string or any response back in simple program.

I have referred the V2 Lex documentation but I can come-up with below code which shows error after several attempts. https://docs.aws.amazon.com/lexv2/latest/dg/lambda.html

error : "Invalid Lambda Response: Received error response from Lambda: Unhandled"

'''

def lambda_handler(event, context):
  entity = event["currentIntent"]["slots"]["Nm"].title()
  intent = event["currentIntent"]["name"]


  response = {
    'sessionState': {
        'dialogAction': {
            'type': 'Close'
        },
        'state': 'Fulfilled'
    },
    'messages': [
          'contentType': 'PlainText',
          'content': "The intent you are in now is "+intent+"!"
        ],
    
    }    

  return response
rakesh N
  • 1
  • 1

2 Answers2

1

The 'messages' field is an array of objects, not an array of strings. It should be declared as follows:

'messages': [
          {
              'contentType': 'PlainText',
              'content': "The intent you are in now is "+intent+"!"
            }
        ]

Reference:

Amazon Lex - Lambda Response format

OARP
  • 3,429
  • 1
  • 11
  • 20
  • Hello, @OmarRosadio Thanks for your time and effort. Yes, I have tried your suggestion, but the same error is still there. Am I missing another thing? – rakesh N Dec 30 '21 at 04:33
  • Please update the question with the new code and an sample request – OARP Dec 30 '21 at 06:19
0

I faced the same issue. The solution that worked for me is like below

var response = {};
    response.messages = [
        message
    ];
    response.sessionState = {
        sessionAttributes:sessionAttributes,
        intent : {
            name : intentRequest.interpretations[0].intent.name,
            state : 'Fulfilled'
        },
        dialogAction: {   
            type: "Close",   
            fulfillmentState: "Fulfilled"
        }
    };

Refer to lex v2 developer guide page 69 Response format https://docs.aws.amazon.com/lexv2/latest/dg/lex2.0.pdf

betelgeuse
  • 1,136
  • 3
  • 13
  • 25
fyhao
  • 1