Chat widget
Last updated
Was this helpful?
Last updated
Was this helpful?
One of the most common ways of making a bot available to your clients is through a chat widget. In this technical guide, we will guide you through the process of integrating the bot in your website.
To create a chat widget for your bot, navigate to the Channels tab and then click on the pencil icon next to Web.
Click on "+ Create" to start building your first custom chat widget.
On the Chat Widget page you can customize a number of key chat widget components:
Config tab
Title of the bot
Template
Upload custom CSS
Title font & font size
Paragraph font & font size
White listing of the domains you want the bot to be activated on (i.e. your own website domain and any other domains you use for testing)
Images (title, bot, user, send icon, SDK button - which is the image that's shown when the bot is closed)
Colors tab
Colors of all the key components of the chatbot
Translations tab
Translations for the default chat widget messages such as "Write reply..."
To make sure your bot can only be activated on your website, all Chatlayer.ai chat widgets must be whitelisted before use. To do this, go to the config tab of the Chat Widget builder and add your domains in regex format.
To embed the bot into your website, start by clicking the Embed button in the top right of the Custom Chat Widget page.
There are two ways of integrating the Chatlayer.ai Chat Widget onto your website: you can use an iframe or the SDK.
You can switch between SDK and iframe using the Type switcher above your widget:
The Chatlayer chat widget SDK is a layer on top of the iframe, which includes some other functionalities such as a button that is shown before the chat window is opened, an option to close the chatbot.
If you want to get the chat widget onto your website with minimal custom development it's best to use the SDK. The only thing you need to do is include a HTML script tag as described below.
If you want more control over other elements, such as the chat button, it's best to use the iframe.
You can load the Chatlayer.ai chat widget by using the script tag below. Calling the chatlayer
function will render the button and your chat widget to the page.
Remove .staging
from the URL if you want to integrate a production bot. You can add parameters to the chatlayer
function to include additional functionality.
In this case, the bot will be opened in English, and when a user clicks the SDK button, it will be opened with the Grow animation.
Property name
Type
Remark
withCloseButton
boolean
The close button allows customers to close the bot using an "X" icon in the top right of the chat widget
autoOpen
boolean
noButton
boolean
session
object
Add data to the session which can be used to guide the flow.
sessionId
string
A session ID can be used to continue the conversation after the user has left the page. Make sure it's 20 characters or longer.
language
string
button
HTMLElement
wrapper
HTMLElement
If you want to pass data from your website to the chatbot, you can add these to the chatlayer
function. All data will be put onto the root.session
variable in the Chatlayer.ai session. This data can then be used to customize the chatbot flow based on website data.
Passing the variables firstName
and lastName
to the SDK allows you to set the corresponding internal Chatlayer session variables. These specific variables decide how the user will show up in the Chatlayer conversations table.
For a lot of use cases, it's required to know who is talking to the bot. One way to do this is by sending a login ID, gathered on the website where your user is logged in, and pass that on to the bot, when the SDK is initialized:
If you import the chat widget with our SDK, you can decide when the widget opens or closes. An example:
Make sure that the SDK script is loaded before running these functions.
Embedding the chat widget in an iframe is the most straight forward embedding option. You determine how and where you place the element and you can style it as you like.
Remove .staging
from the URL if you want to integrate a production bot. You can change ?language=en
to any language the bot supports.
If you want to recognize a returning bot user, you can send a unique sender ID for each person opening the bot, which can then be used to open the same conversation when the page is reloaded.
Make sure that your sender ID is 20 characters or longer.
The same goal can also be achieved by changing the iframe url:
The example above, would result in a session containing:
By adding the properties underneath to your CSS, you can resize the chat widget window. You can use these parameters to make the chat widget responsive for mobile devices.
There are two main options for securing the chat widget. You can either verify the payload by using JWT, making sure it hasn't been tampered with, or you can encrypt the session created by the chat widget by using AES-256 data protection.
When you turn on the AES-256 data protection, you can only pass an encrypted token as the chat session. This token should be generated in your own back-end. The code to generate the token looks like this (using node.js):
How you pass this token to the client will depend on how you are generating your html, but this server-generated token should be what is passed to the chatlayer function:const chat = chatlayer({token: serverGeneratedToken});
We've created an example of how you can initialize and destroy the Chatlayer.ai chat widget through your own Javascript code.
JSON builder plugin configuration:
As you can see from above example, you can initialize the widget, open/close it as well as destroy it from your own code. The example shows how you can use the JSON Builder plugin to trigger a destroy event for the chat widget.
An example of this regex format for : ^https:\/\/www\.chatlayer\.ai(\/|$)
This login ID can then be reused to perform calls with the .
A user's session can be updated at any time through the .
View a live version of this code .