发生这种情况是因为您发回的只是一个字符串,而 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文档中解释了这些类型以及更多类型。