亚马逊Lex重复出现的lambda函数错误

亚马逊Lex重复出现的lambda函数错误

问题描述:

我一直在试图为我正在制作的Lex聊天机器人做一个lambda函数,但是每当我的意图调用函数时,它都会给我同样的错误,我厌倦了它。我正在使用node.js.它给我的错误信息是:亚马逊Lex重复出现的lambda函数错误

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函数,都会发生这种情况。任何答案?

+0

你能显示你的节点代码吗? – AndyOS

+0

exports.handler =(event,context,callback)=> {TODO implements callback(null,“this works”); }; – Gabe

+0

对不起,它看起来很奇怪,我不能让markdown正常工作 – Gabe

发生这种情况是因为您发送的所有内容都是字符串,而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 documentation解释。

+0

我对AWS Lambda非常陌生,所以我并不真正了解如何从我的javascript中获取JSON代码。 – Gabe

+1

我建议你通过他们提供的教程,他们相当详细 - 并向您展示如何使用示例函数构建所需的JSON响应,例如我的答案中包含的“close”函数。对于那个你需要提供的是“内容” - 一个字符串。 – AndyOS