Livechat Widget API code must be inserted after the Livechat Widget installation script and wrapped as a callback of RocketChat();
function.
You can call multiple Livechat Widget APIs on the same page.
To set a custom field for a visitor, you can use the following code:
RocketChat(function() {this.setCustomField('fieldName1', 'Any value you want to store');this.setCustomField('fieldName2', 'A value set just once', false); // you can pass false as the third parameter to not overwrite an already set value});
To change the online color of the Livechat widget, use the following code:
RocketChat(function() {this.setTheme({color: '#04436A', // widget title background colorfontColor: '#FFFFFF', // widget title font coloriconColor: '#1d74f5', // widget icon colortitle: "Welcome to Rocket.Chat", // default widget title when the status of service is onlineofflineTitle: "Service is offline", // default widget title when the status of service is online});});
To automatically assign a Livechat widget to a specific department (for example, to use a unique Livechat widget on more than one website), use the following code:
RocketChat(function() {this.setDepartment('FILL HERE DEPARTMENT NAME - case sensitive');});
To set an external token for a visitor, you can use the following code:
RocketChat(function() {this.setGuestToken('FHwaLnp8fzjMupSAj');});
To set the visitor name field, you can use the following code:
RocketChat(function() {this.setGuestName('visitor name');});
To set the visitor email field, you can use the following code:
RocketChat(function() {this.setGuestEmail('sample@rocket.chat');});
To register the visitor without using the registration form, you can use the following code:
RocketChat(function() {this.registerGuest({token: 'FHwaLnp8fzjMupSAj', // The token field is not required. If it is not passed, a new token will be generatedname: 'visitor Name',email: 'sample@rocket.chat',department: 'my_department', // The department field is not required,customFields: [ // The customFields field is not required. If it is passed it needs to be an Array, where each item needs to be an object with key and value fields{key: 'my_custom_field_a', value: 'my_custom_field_a_value'},{key: 'my_custom_field_b', value: 'my_custom_field_b_value'}]});});
There are number of language options, which you can choose from to set language for your widget. To check supported languages refer here. To set language of widget use the following code.
RocketChat(function() {this.setLanguage('af');});
The widget allows setting a specific agent before the conversation starts, to do this follow these steps:
RocketChat(function() {this.setAgent({_id: 'h24yNtyoCmvp96wgt',username: 'rocket.chat',});});
The widget allows configuring all the settings in just one method, the following properties are acceptable:
RocketChat(function() {this.initialize({theme: {color: '#04436A',fontColor: '#FFFFFF',iconColor: '#1d74f5',title: "Welcome to Rocket.Chat",offlineTitle: "Service is offline",},department: 'sales',guestToken: 'FHwaLnp8fzjMupSAj',language: 'en',});});
You can either hide or show widget in your website. To hide widget use the following code:
RocketChat(function() {this.hideWidget();});
To show widget use the following code.
RocketChat(function() {this.showWidget();});
You can either open or close then widget in your website. To open widget(default state) use the following code:
RocketChat(function() {this.maximizeWidget();});
To close the widget use the following code.
RocketChat(function() {this.minimizeWidget();});
Fired when the chat widget is maximized.
RocketChat(function() {this.onChatMaximized(function() {// do whatever you wantconsole.log('chat widget maximized');});});
Fired when the chat widget is minimized.
RocketChat(function() {this.onChatMinimized(function() {// do whatever you wantconsole.log('chat widget minimized');});});
Fired when the chat is started (the first message was sent).
RocketChat(function() {this.onChatStarted(function() {// do whatever you wantconsole.log('chat started');});});
Fired when the chat is ended either by the agent or the visitor.
RocketChat(function() {this.onChatEnded(function() {// do whatever you wantconsole.log('chat ended');});});
Fired when the pre-chat form is submitted.
RocketChat(function() {this.onPrechatFormSubmit(function(data) {// data is an object containing the following fields: name, email and deparment (the department _id)​// do whatever you wantconsole.log('pre-chat form submitted');});});
Fired when the offline form is submitted.
RocketChat(function() {this.onOfflineFormSubmit(function(data) {// data is an object containing the following fields: name, email and message​// do whatever you wantconsole.log('offline form submitted');});});
Fired when widget is hidden.
RocketChat(function() {this.onWidgetHidden(function(data) {// do whatever you wantconsole.log('chat widget hidden');});});
Fired when an agent is assigned to the chat.
RocketChat(function() {this.onAssignAgent(function(data) {// data is an object containing the following fields: name, username and status​// do whatever you wantconsole.log('Agent assigned');});});
Fired when widget is shown.
RocketChat(function() {this.onWidgetShown(function(data) {// do whatever you wantconsole.log('chat widget shown');});});
Fired when the status of the current agent changes.
RocketChat(function() {this.onAgentStatusChange(function(data) {// data is an object containing the following fields: name, username and status​// do whatever you wantconsole.log('The status of the agent has changed');});});
Fired when a visitor tries to start a new conversation and the Livechat service is offline.
RocketChat(function() {this.onServiceOffline(function(data) {// do whatever you wantconsole.log('The Livechat service is offline');});});
Version | Description |
3.1.0 | Added |
3.0.0 | Added |
2.2.0 | Added |
1.3.0 | Added |
1.1.0 | Added |
1.0.0 | Added |
0.66.0 | Added |
0.53.0 | Added callback events and the ability to pass a flag to |
0.36.0 | Added |
0.26.0 | Added |