首页 > 解决方案 > 在来自 Lambda 的 Amazon lex 响应中呈现 HTML

问题描述

我是 Amazon Lex 的新手。我正在尝试将超链接作为从 Lambda 函数到 Amazon Lex 的“内容”响应的一部分返回。基本上我做了以下事情:

    var message = {
        'contentType': 'PlainText', 
        'content': 'We offer x,y,z. For more information, visit our <a href="www.xyz.com">website</a>'
    }

这会将整个响应作为字符串返回,而我希望在聊天机器人上显示之前呈现 html 部分。我不想在我的回复下方使用 responseCard。是否可以在返回的内容中包含超链接?谢谢

标签: aws-lambdaamazon-lex

解决方案


请注意,这是为了在 HTML 页面上呈现对话

当我开发一个 HTML 页面来呈现用户和聊天机器人之间的聊天时,我遇到了同样的问题。我设法使用以下Javascript函数解决了它:

function showResponse(lexResponse) {

    var conversationDiv = document.getElementById('conversation');
    var responsePara = document.createElement("P");
    responsePara.className = 'lexResponse';
    if (lexResponse.message) {
        var message = lexResponse.message.replace(/"/g, '\'');
        responsePara.innerHTML = message;               
        responsePara.appendChild(document.createElement('br'));
    }           
    conversationDiv.appendChild(responsePara);
    conversationDiv.scrollTop = conversationDiv.scrollHeight;
}

作为参考,您可以参考我就同一问题提出的问题:LexResponse output does not understand HTML data


推荐阅读