亚马逊Lex经常出现lambda函数错误

时间:2017-08-08 20:34:41

标签: amazon-web-services aws-lambda amazon-lex

我一直在努力为我正在制作的Lex聊天机器人制作一个lambda函数,但每当我的意图调用该函数时,它一直给我同样的错误,我厌倦了它。我正在使用node.js.它给我的错误信息是:

An error has occurred: Invalid Lambda Response: 
Received invalid response from Lambda: Can not construct instance of
IntentResponse: no String-argument constructor/factory method to
deserialize from String value ('this works') at
[Source: "this works"; line: 1, column: 1

无论我输入什么样的lambda函数,都会发生这种情况。有什么答案吗?

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您发回的所有内容都是字符串,而Lex希望以特定格式回复,例如。

"dialogAction": {
    "type": "Close",
    "fulfillmentState": "Fulfilled or Failed",
    "message": {
      "contentType": "PlainText or SSML",
      "content": "Message to convey to the user. For example, Thanks, your pizza has been ordered."
    },
   "responseCard": {
      "version": integer-value,
      "contentType": "application/vnd.amazonaws.card.generic",
      "genericAttachments": [
          {
             "title":"card-title",
             "subTitle":"card-sub-title",
             "imageUrl":"URL of the image to be shown",
             "attachmentLinkUrl":"URL of the attachment to be associated with the card",
             "buttons":[ 
                 {
                    "text":"button-text",
                    "value":"Value sent to server on button click"
                 }
              ]
           } 
       ] 
     }
  }

此代码可以使用:

function close(sessionAttributes, fulfillmentState, message, responseCard) {
    return {
        sessionAttributes,
        dialogAction: {
            type: 'Close',
            fulfillmentState,
            message,
            responseCard,
        },
    };
}

function dispatch(intentRequest, callback) {
    const outputSessionAttributes = intentRequest.sessionAttributes || {};
    callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText',
        content: 'Thank you and goodbye' }));
}

function loggingCallback(response, originalCallback) {
    originalCallback(null, response);
}

exports.handler = (event, context, callback) => {
    try {
        console.log("event: " + JSON.stringify(event));
        dispatch(event, (response) => loggingCallback(response, callback));
    } catch (err) {
        callback(err);
    }
};

它只是发回'#34;谢谢你,再见"在所需格式中,在这种情况下使用" dialogAction"类型"关闭" - 告知Lex不要期待用户的回复。

还有其他类型 - 这些以及更多内容都在Lex documentation中进行了解释。