简体   繁体   中英

Amazon Lex V2 Lambda python code to get intent name or any response back from lambda is not working

''' 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM