如何使用Amazon-Lex中的Image进行响应

时间:2018-02-17 12:56:12

标签: amazon-web-services aws-lambda amazon-lex

将图像显示为响应时出现错误。我收到了这个错误:

  

发生错误:无效的Lambda响应:收到无效   来自Lambda的回复:无法识别的字段“responseCard”(类   IntentResponse),在[来源:未标记为可忽略]   { “sessionAttributes”:{}, “dialogAction”:{ “类型”: “关闭”, “fulfillmentState”: “达到”, “消息”:{ “的contentType”: “纯文本”, “内容”:“

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

我应该如何在聊天框中显示图像?我在这做什么错?

1 个答案:

答案 0 :(得分:1)

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:contentType,genericAttachments,version
GenericAttachments format:attachmentLinkUrl,buttons,imageUrl,subtitle,title
Buttons format:文字,值
没有必需,但这里是所有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
             }
          ]
       } 
   ] 
}

注意:响应卡未出现在Lex Console Test Chatbot中,您必须将Lex bot连接到Facebook,Twilio-SMS或其他以测试响应卡。