1

我一直在尝试为我正在制作的 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 函数,都会发生这种情况。有什么答案吗?

4

1 回答 1

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);
    }
};

它只是以所需的格式发回“谢谢和再见”,在本例中使用“关闭”类型的“dialogAction”——它通知 Lex 不要期待用户的响应。

还有其他类型 - Lex文档中解释了这些类型以及更多类型。

于 2017-08-09T22:32:07.033 回答