Search code examples
aws-lambdaamazon-lex

Rendering HTML in Amazon lex response from Lambda


I am new to Amazon Lex. I am trying to return a hyperlink as part of the "content" response from Lambda function to the Amazon Lex. Basically I have doing the following:

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

This returns the whole response as a string whereas I am expecting the html part to be rendered before displaying on the chatbot. I don't want to use a responseCard below my response. Is that possible to include hyperlink in the content returned? Thanks


Solution

  • PLEASE NOTE THAT THIS IS FOR RENDERING THE CONVERSATION ON A HTML PAGE

    I had this same issue when I was developing a HTML page to render the chat between an user and the chatbot. I managed to solve it using the following Javascript Function:

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

    For Reference, you can refer to the question I asked regarding this same issue: LexResponse output does not understand HTML data