如何在 Amazon-Lex 中使用图像进行响应
How to respond with Image in Amazon-Lex
我在显示图像作为响应时遇到错误。我收到此错误:
An error has occurred: Invalid Lambda Response: Received invalid
response from Lambda: Unrecognized field "responseCard" (class
IntentResponse), not marked as ignorable at [Source:
{"sessionAttributes":{},"dialogAction":{"type":"Close","fulfillmentState":"Fulfilled","message":{"contentType":"PlainText","content":"
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
},
responseCard: {
version: '2',
contentType: "application/vnd.amazonaws.card.generic",
genericAttachments: [
{
imageUrl:"URL of the image to be shown",
}
]
}
};
}
exports.handler = (event, context, callback) => {
console.log( "EVENT= "+JSON.stringify(event) );
const intentName = event.currentIntent.name;
var sessionAttributes = event.sessionAttributes;
var responseMsg = "";
if (intentName == "greetings") {
var message = {
'contentType': 'PlainText',
'content': 'Hi! How can I help you?'
}
responseMsg = close( sessionAttributes, 'Fulfilled', message );
}
else {
console.log( "ERROR unhandled intent named= "+intentName );
responseMsg = close( sessionAttributes, 'Fulfilled', {"contentType":"PlainText", "content":"Sorry, I can't help with that yet."});
}
console.log( "RESPONSE= "+JSON.stringify(responseMsg) );
callback(null,responseMsg);
}
我应该如何在聊天框上显示图像?我在这里犯了什么错误?
responseCard
需要在 dialogAction
内。
尝试:
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
"dialogAction": {
"type": "Close",
fulfillmentState,
message,
"responseCard": {
"version": "2",
"contentType": "application/vnd.amazonaws.card.generic",
"genericAttachments": [
{
"imageUrl":"http://...",
}
]
}
}
};
}
为了安全起见,我还给不是变量的键加了引号。
有关响应卡的更多信息:
ResponseCard format:内容类型、通用附件、版本
GenericAttachments format:附件链接网址、按钮、图片网址、副标题、标题
Buttons format:文本,值
None 是必需的,但这里是所有 responseCard 属性的示例:
"responseCard": {
"version": integer-value, //change to integer
"contentType": "application/vnd.amazonaws.card.generic", //don't change
"genericAttachments": [
{
"title":"card-title", //change to any string
"subTitle":"card-sub-title", //change to any string
"imageUrl":"http://...", //change to full url
"attachmentLinkUrl":"http://...", //change to full url
"buttons":[
{
"text":"button-text", //change to any string
"value":"Value sent to server on button click" //change to any string
}
]
}
]
}
我在显示图像作为响应时遇到错误。我收到此错误:
An error has occurred: Invalid Lambda Response: Received invalid response from Lambda: Unrecognized field "responseCard" (class IntentResponse), not marked as ignorable at [Source: {"sessionAttributes":{},"dialogAction":{"type":"Close","fulfillmentState":"Fulfilled","message":{"contentType":"PlainText","content":"
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
},
responseCard: {
version: '2',
contentType: "application/vnd.amazonaws.card.generic",
genericAttachments: [
{
imageUrl:"URL of the image to be shown",
}
]
}
};
}
exports.handler = (event, context, callback) => {
console.log( "EVENT= "+JSON.stringify(event) );
const intentName = event.currentIntent.name;
var sessionAttributes = event.sessionAttributes;
var responseMsg = "";
if (intentName == "greetings") {
var message = {
'contentType': 'PlainText',
'content': 'Hi! How can I help you?'
}
responseMsg = close( sessionAttributes, 'Fulfilled', message );
}
else {
console.log( "ERROR unhandled intent named= "+intentName );
responseMsg = close( sessionAttributes, 'Fulfilled', {"contentType":"PlainText", "content":"Sorry, I can't help with that yet."});
}
console.log( "RESPONSE= "+JSON.stringify(responseMsg) );
callback(null,responseMsg);
}
我应该如何在聊天框上显示图像?我在这里犯了什么错误?
responseCard
需要在 dialogAction
内。
尝试:
function close(sessionAttributes, fulfillmentState, message) {
return {
sessionAttributes,
"dialogAction": {
"type": "Close",
fulfillmentState,
message,
"responseCard": {
"version": "2",
"contentType": "application/vnd.amazonaws.card.generic",
"genericAttachments": [
{
"imageUrl":"http://...",
}
]
}
}
};
}
为了安全起见,我还给不是变量的键加了引号。
有关响应卡的更多信息:
ResponseCard format:内容类型、通用附件、版本
GenericAttachments format:附件链接网址、按钮、图片网址、副标题、标题
Buttons format:文本,值
None 是必需的,但这里是所有 responseCard 属性的示例:
"responseCard": {
"version": integer-value, //change to integer
"contentType": "application/vnd.amazonaws.card.generic", //don't change
"genericAttachments": [
{
"title":"card-title", //change to any string
"subTitle":"card-sub-title", //change to any string
"imageUrl":"http://...", //change to full url
"attachmentLinkUrl":"http://...", //change to full url
"buttons":[
{
"text":"button-text", //change to any string
"value":"Value sent to server on button click" //change to any string
}
]
}
]
}