2017-08-08 65 views
0

我一直在試圖爲我正在製作的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

回答

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 documentation解釋。

+0

我對AWS Lambda非常陌生,所以我並不真正瞭解如何從我的javascript中獲取JSON代碼。 – Gabe

+1

我建議你通過他們提供的教程,他們相當詳細 - 並向您展示如何使用示例函數構建所需的JSON響應,例如我的答案中包含的「close」函數。對於那個你需要提供的是「內容」 - 一個字符串。 – AndyOS