🆕Web V2 methods and options
The article below will deep dive in technical possibilities of our new Web widget (V2).
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.
All methods
Method | Description | Arguments | Read more on |
---|---|---|---|
| Initializes the widget using the specified options. This method returns a promise like object. | (options: Options) | |
| Opens the widget. |
|
|
| Closes the widget. |
|
|
| If widget is closed, it opens it and vice-versa. |
|
|
| Destroys the widget | ||
| Shows whether the widget is open. |
|
|
| To federate the customer identity with the ones of your website or application. | (externalId: string, jwt: string) | |
| Closes the session and a new conversation will start from scratch. | (externalId: string, jwt: string) | |
| Subscribe to predefined and custom events throughout the user conversation with your bot. | (event, (arg1, arg2) => ...) | |
| Stop tracking or unsubscribe from receiving the events set in |
|
|
| Set the current language. Only languages on the list of languages supported by your bot will be accepted. |
| |
| The current language of the widget. |
| |
| Sets extra data in the conversation session and returns a promise. |
| |
| Retrieves session data from |
|
|
| Sends a message on the user’s behalf, if they click a button on the page, for example. | (text) |
|
| Creates a new event that will be tracked. | e.g: Chatlayer.track ("view_product”, product_id: "SKU123"})
|
|
| Track events occuring on your site or app. | (eventName: string, eventAttributes: Record<string,any>) | |
| This method can be used if you want the widget embedded on a page. |
|
|
| Shows the typing indicator | (sender) | |
| Hides the active typing indicator | ||
| Starts a new empty conversation while maintaining the customer information | ||
| Allows you to set a custom delegate for the agent avatar and name displayed |
|
Initialize the Web widget with options
'Chatlayer.init(options: Options)'
This Initialize the Web widget using the specified options. This method returns a promise-like object.
Note that except for the on
and off
methods, the rest of the documented methods below needs to be called after init
is completed.
Options
The Options
can be:
Option | Is optional | Dafault value | Type | |
---|---|---|---|---|
channelId | No | - |
| The ID of the Chatlayer channel |
title | Yes | - |
| The title to show in the header widget |
subtitle | No | - |
| An optional subtitle to show below the title |
position | No |
|
| The position of the widget. Possible values are |
buttonWidth | No | 58 |
| The width of the widget button |
buttonHeight | No | 58 |
| The height of the widget button |
Yes | - |
| Override the colors of the widget | |
headerIconUrl | Yes | - |
| URL of a public image to be shown in the header |
region | Yes | eu-west1-gcp |
| The region of your Chatlayer account |
Yes | - |
| Override the text of the different widget UI elements | |
enableTextInput | No | true |
| Set it to false to hide the text input, allowing users to interact only via buttons and quick replies |
enableAudioInput | No | true |
| Set it to false to hide the audio recording button, preventing the users from sending audio messages via the widget |
enableFileUpload | No | true |
| Set it to false to hidde the file upload button, preventing the users sending files via the widget. File upload request will still work |
enableSoundNotifications | No | true |
| Control whether the user will receive sound notifications every time a new message arrives. It can be changed by the user as well from the settings |
No | - |
| Information collection using a form before the chat starts | |
delegate | No | - |
| Set a delegate for the conversation. Check Setting a delegatefor more details |
customColors
The customColors
can be:
Option | Optional | Default Value | Description |
brandColor | Yes | 007171 | This color will be used in the messenger header and the launcher. It must be a 3 or 6-character hexadecimal color. |
conversationColor | Yes | 007171 | This color will be used for customer messages, quick replies and actions in the footer. |
actionColor | Yes | 007171 | This color will be used for call-to-actions inside your messages. Must be a 3 or 6-character hexadecimal color. |
customText
customText
can be:
Option | Optional | Default Value | Description |
inputPlaceholder | Yes | Send a message | Placeholder text shown in the message input field |
botDisplayName | Yes | Bot | This display name of the bot. Use an empty string to hide it. |
agentDisplayName | Yes | Agent | This display name of the agent. Use an empty string to hide it. |
To provide different values for different languages, add a subfield with the language code.
prechatForm
The prechatForm
is one of the options that we have in the Web widget to configure a form with a set of input fields before beginning the chat.
Internally, it consists of an array of Element Definitions. Each element can have following type
, label
, placeholder
and variant
.
Name | Type | Description | Applies to |
type | String | The type of element | One of |
label | String | The text that goes on top of the input field |
|
placeholder | String | The sample text in the field |
|
variant | String | The element variant | One of |
Below, an example of a preview of how PrechatForm
looks in the widget. User can customise the fields before starting the chat.
Other methods
User authentication with externalId
When the widget initializes, an anonymous customer is automatically created. This customer can then send messages to the bot.
In some scenarios, you would like to federate the customer identity with the ones of your website or application. This is done using the externalId
. This field identifies the user on your system. It can be an email address, a username, a uuid or any other user id on your database. Most importantly it should be unique and always reference a entity from your system.
In order to proof the authenticity of the user, we require a signed JWT token that validates your ownership of the widget. Otherwise anyone can impersonate your users.
method | necessary info |
| (externalId: string, jwt: string) |
| (externalId: string, jwt: string) |
Delete the chat history
Our new widget, by default, stores all conversations between the bot and the customer. This ensures an experience akin to other messaging platforms. When users contact the bot through the widget and reopen it, they can view all past conversations by default.
If you want to circumvent this or want customers to be able to refresh the conversation, you could use our chatlayer.login
or chatlayer.logout
commands to allow you to do so.
Create a refresh button
The script below will allow you to add a button to your widget that when being clicked will clear the conversation. The button will have the id: RESTART_CONVERSATION
and upon being clicked will logout the user and log them back in immediately.
Then in the snippet you placed in the bottom add:
Events send by Chatlayer
Chatlayer.on(event, (arg1, arg2) => ...)
Chatlayer.on(event, (arg1, arg2) => ...)
event | description | arg1 |
| The widget was opened | - |
| The widget was closed | - |
| When a custom action button was clicked | - |
| A message was received | - |
| A message was send by the user | - |
| An event was received |
|
| The widget was initialized | - |
| The widget was destroyed | - |
EventPayload
Field | Type | Description |
generic_event |
| A generic event |
agent_joined_event |
| When an agent joins the conversation |
agent_left_event |
| When an agent leaves the conversation |
GenericEvent
Field | Type | Description |
payload | JSON | Arbitrary data set to the event. A valid JSON object. |
Note that you can use the JSON builder to send generic events from your user's session to the site you've hosted the widget on.
AgentJoinedEvent
Field | Type | Description |
agent |
| Represents an agent that is involved in a conversation. |
Agent
Field | Type | Description |
display_name | string | Agent's display name |
picture_url | string | The Agent's picture url. |
type |
| Agent's classification can be UNKNOWN_AGENT_TYPE, HUMAN or BOT. |
Use Chatlayer.off(event, (arg1, arg2) => ...)
to unsubscribe from these events.
Change the language
Chatlayer.setLanguage(language)
Chatlayer.setLanguage(language)
Sets the current language. Only languages on the list of languages supported by your bot will be accepted.
Chatlayer.language
Chatlayer.language
Is the current language on the widget.
Note that you can also change the bot's language using the iframe in the Installation tab.
Set the session data
If you want to transfer data from your website to the chatbot, you can add that data to the chatlayer
function after initializing the widget. All data will be put in the Chatlayer session. This data can then be used to customize the chatbot flow, based on actual website data.
Chatlayer.setSessionData(SessionData)
Chatlayer.setSessionData(SessionData)
Sets extra data in the conversation session and returns a promise
Example
The example above shows how you can set the variable Product
in the Chatlayer session. It will get the value iPhone XS
in this case.
Setting a delegate
The widget allows you to set a delegate to receive callbacks when important changes happen in the conversation. To set a delegate, pass the delegate parameter in to init options, or use the setDelegate method. The delegate object may optionally contain beforeDisplay or beforeSend functions.
Passing delegate as part of init options is the preferred method. The setDelegate method can be used to change or remove delegate behaviors after a conversation has been initialized.
DelegateConfig
DelegateConfig is an object containing the 2 fields below:
beforeDisplay
The beforeDisplay
delegate allows a message to be hidden or modified before it is displayed in the conversation. This delegate should return a falsy value such as null to hide the message. It can also return a modified message object in order to change what the user will see rendered in their conversation history. Note that this change affects the client side rendering only; the server side copy of this message can not be modified by this delegate.
beforeSend
The beforeSend
delegate method allows you to modify properties of a message before sending it to Chatlayer. The modified message must be returned for it to take effect.
A common usage of this method is to add message metadata.
Note that when a file or an image is uploaded, only the message metadata may be updated. Other message properties such as type or text won't be considered.
💬 Give feedback
Please let us know what you think of the new Web widget here.
Last updated