🚨Web V1 [deprecated soon]
From 31st October 2024, all Chatlayer customers using the Web channel will need to switch to the new web widget, Web V2. This means that Web V1 will be removed from our codebase. Learn everything about moving from V1 to V2.
Attention:
This article refers to our legacy web widget, which will be deprecated on 01/10/2024. On 01/04/2024, we're discontinuing support for Web chat widget V1. All newly created web widgets should follow the Web V2 documentation.
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.
Creating your web chat widget
To create a chat widget for your bot, navigate to the Channels tab and then click on the pencil icon next to Web.
Click "+ Create" to start building your first custom chat widget.
You can create multiple chat widgets for one bot, remove them, view them, and copy them using the buttons in the chat widget table.
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 (more information about custom CSS here)
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..."
Whitelisting
To make sure your bot can only be activated on your website, all your chat widgets must be whitelisted before you can use them. To do this, go to the config tab of the Chat Widget builder and add a regular expression that matches the domain names that you wish to whitelist.
A simple example of this regex format for https://www.chatlayer.ai: ^https:\/\/www\.chatlayer\.ai(\/|$)
Whitelisting regexes can also be used for more complex, multi-domain configurations. Take the following set of domains as an example:^https:\/\/www\.(subdomain1|subdomain2)\.(rootdomain1|rootdomain2)\.com(\/|$)
will whitelist the domains:
https://www.subdomain1.rootdomain1.com
https://www.subdomain1.rootdomain2.com
https://www.subdomain2.rootdomain1.com
https://www.subdomain2.rootdomain2.com
You can separate the alternatives by using a |
character as follows:
A shorter regular expression which will match the same domains is:
An easy way to validate your regular expression is by using online tools like https://regex101.com/
Whitelisting with an iframe
Some browsers have a default referrerpolicy
of strict-origin-when-cross-origin
. This will enforce the browser to only include the origin of your webpage when it requests the content of an iframe.
If your iframe widget is hosted at https://example.com/chat_widget.html
, requests will only contain the https://example.com/
portion of that path. This could cause issues if you use a whitelisting regex that matches the full path of your webpage, for example:
If you want to use the full path of your webpage in the whitelisting configuration of your widget, you can override the referrerpolicy
of the iframe that contains the link to your widget to be no-referrer-when-downgrade
More information about referrer policies can be found here.
Embedding the web widget on your website
To embed the bot on 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 Chat Widget on your website: you can use either iframe or SDK.
You can switch between SDK and iframe using the Type switcher above your widget:
Should I use SDK or iframe?
Our chat widget SDK is a layer on top of an iframe, which includes some other functionalities like a button that is shown before the chat window opens, with the 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 an 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 iframe.
SDK
You can load the chat widget by using the script tag below. Calling the chatlayer
function will show the button and your chat widget on the page.
Remove .staging
from the URL if you want to integrate a LIVE bot. You can add parameters to the chatlayer
function to include additional functionality.
In this case, your bot will be opened in English, and when a user clicks the SDK button, it will be opened with the Grow animation.
SDK Options
Transferring a variable from your website, through the SDK to the chatbot
If you want to transfer data from your website to the chatbot, you can add that data to the chatlayer
function. All data will be put onto the session
variable in the Chatlayer.ai session. This data can then be used to customize the chatbot flow, based on actual website data.
Transferring 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.
User authentication with the chat widget SDK
In a lot of cases it's required to know who is talking to the bot. One way to find out is by sending a login ID, detected on the website your user is logged in, and passing it on to the bot, when the SDK is initialized:
This login ID can later be reused to perform calls with the API plugin.
Chat widget button behaviour
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.
Iframe
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 ?lang=en
to any language the bot supports.
Defining the sender ID
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.
Transferring a variable from your website, through the iframe to the chatbot
A user's session can be updated at any time through the Send messages through a Webhook channel API call.
The same goal can also be achieved by changing the iframe url:
The example above, would result in a session containing:
Size and position of iframe
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.
Encryption
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});
Live example
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:
View a live version of this code here.
As you can see from the example above, you can initialize the widget, open/close it, as well as destroy it from your own code. The example shows you how to use the JSON Builder plugin to trigger a destroy event for the chat widget.
Last updated