AWS Lex:发生错误:无效的 Lambda 响应

AWS Lex : An error has occurred: Invalid Lambda Response

我正在尝试将使用 Amazon Lex 收集的值保存到 DynamoDB 中。 我制作(或复制和修改)了一个 Lambda 函数,这显然不起作用,但我真的不明白为什么。 这是我从 Amazon Lex 得到的错误:"An error has occurred: Invalid Lambda Response: Received error response from Lambda: Handled"

其他一切正常,我解决了权限问题,并遇到了其他错误。

当我收到该错误时,对话框处于状态:

{
   "dialogState": "ElicitSlot",
   "intentName": "MCQ_A",
   "message": "What is a black and white horse",
   "messageFormat": "PlainText",
   "responseCard": null,
   "sessionAttributes": {},
   "slotToElicit": "QB",
   "slots": {
       "QA": "4",
       "QB": null,
       "Session": "444"
   }
}

// 这是我的 Lambda 函数。它是用 javascript 写的,这是我刚刚发现的,我绝对不喜欢它 ^^'

console.log('Loading event');
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var tableName = "MCQ";

// Close dialog with the customer, reporting fulfillmentState of Failed or Fulfilled
function close(session_attributes, m) {
    let callback_obj = {
        sessionAttributes: session_attributes,
        dialogAction: {
            type: 'Close',
            fulfillmentState: "Fulfilled",
            message: m,
        }
    }
    return callback_obj;
}

function storeRegistration(intent, callback) {

    let userInfo = {};

    // store every slot we received as part of registration
    Object.keys(intent.slots).forEach((item) => {
        console.log(item)
        userInfo[item] = {"S": intent.slots[item]};
    });

    dynamodb.putItem({
        "TableName": tableName,
        "Item" : userInfo
    }, function(err, data) {
        if (err) {
            console.log('Failure storing user info');
            console.log(err);
            callback(close(intent.sessionAttributes, {contentType: 'PlainText', content: "I am sorry, but something went wrong. Please try again."}));
        } else {
            console.log("Successfully Stored UserInfo");
            callback(close(intent.sessionAttributes, {contentType: 'PlainText', content: "Thank you for attending the MCQ."}));
        }
    });
}

// --------------- Main handler -----------------------

// Route the incoming request based on intent.
// The JSON body of the request is provided in the event slot.
exports.handler = (event, context, callback) => {
    console.log(event);
    try {
        storeRegistration(event, (response) => {callback(null, response)});
    } catch (err) {
        callback(err);
    }
};

你知道发生了什么事吗? :) 非常感谢您!!

  1. 插槽 QB 可能会在插槽定义区域中被检查为 required。即使您通过实现意图来关闭对话框,lex 也会遵循其优先级,以便它引出插槽。
  2. 从代码端而不是 lex 控制台处理 elicitSLot 条件。

非常感谢您的回答。 QB确实设置为'required'。 所以我在主要部分添加了一些行来处理这种情况:

if (event.dialogState == "ElicitSlot")
        callback(null, {
            "dialogAction": {
                "type": "ElicitSlot",
                "intentName": event.intentName,
                "slots": event.slots,
                "slotToElicit" : event.slotToElicit
        }})

但是我得到了同样的错误。在我的绝望中,我也尝试了 'Delegate' 响应,但也出现了同样的错误。 我有doc,所以我想我的回复写得很好。 尽管如此,我还是不明白为什么我的聊天机器人 returns 输出带有空槽,因为我实际上是在对话框中填充槽 ...

编辑:在使用 AWS 的蓝图 "order flowers" 和相应的 lambda 函数进行了几次测试后,一切正常,直到我添加一行用于存储到 Dynamodb 中。所以一定是我的错误。

我找到了解决方案:

我放弃了我的功能,并采用了 AWS 的蓝图,名为 lex-order-flowers-python。它与 Lex 蓝图 OrderFlowers 开箱即用。我只需要让它适应我的工作,并添加 DynamoDB 部分。

确保您的项目格式正确,并指定了值类型('S' 为字符串)

put_item(
    TableName = 'myTableName',
    Item = {
        'key1': {'S': 'value1'}
        'key2': {'S': 'value2'}
    })

所以我做了这个:

userInfo = {}
for key, value in intent_request['currentIntent']["slots"].items() :
    userInfo[key] = {'S': value}

logger.debug(userInfo)

dynamodb = boto3.client('dynamodb')
dynamodb.put_item(TableName='MCQ', Item=userInfo)