Showing posts with label lex chat showing html tags in website. Show all posts
Showing posts with label lex chat showing html tags in website. Show all posts

Monday, October 19, 2020

Lex chatbot is not rendering html on web page

If you have integrated Lex chatbot in your website and it is showing html as a response message, it means your javascript is not able to render the html on the web page.

You need to handle this issue at the client side as it is correct method to send html as a response message from Lex.

If you are using default javascript code which you found in AWS blog for your Lex chatbot integration, you need to modify it little.

Find the following code in javascript function showResponse(lexResponse)

and replace following piece of code

	    if (lexResponse.message) {
	responsePara.appendChild(document.createTextNode(lexResponse.message));
	responsePara.appendChild(document.createElement('br'));
    }

with

    	if (lexResponse.message) {

        var message = lexResponse.message.replace(/"/g, '\'');
        responsePara.innerHTML = message;
        responsePara.appendChild(document.createElement('br'));
    }

Now you can add html markups in the message section of Lex chatbot so it will return this message when the 'Utterance' will be matched and It will render the html on the web page.