This page contains a reference for all methods available when injecting Webchat.

open

The window.botpress.open method opens the Webchat window:
window.botpress.open();
It becomes available after Webchat has been initialized.

Parameters

The open method takes no parameters.

Returns

void

close

The window.botpress.close method closes the Webchat window:
window.botpress.close();
It becomes available after Webchat has been initialized.

Parameters

The close method takes no parameters.

Returns

void

toggle

The window.botpress.toggle method toggles the Webchat window open/closed depending on its current state:
window.botpress.toggle();
It becomes available after Webchat has been initialized.

Parameters

The toggle method takes no parameters.

Returns

void

updateUser

The asynchronous window.botpress.updateUser method updates the data associated with the current Webchat user:
await window.botpress.updateUser({
  name: "<string>",
  pictureUrl: "<string>",
  data: {},
  userKey: "<string>"
});
It becomes available after Webchat has been initialized.

Parameters

user
User
The user attributes to update.
By default, the user object contains the following properties:
user
User
These are generated by the server and can’t be updated or deleted.

Returns

Promise<void>
A Promise that resolves void when the user data has been successfully updated.

getUser

The asynchronous window.botpress.getUser method gets the data associated with the current Webchat user:
await window.botpress.getUser();
It becomes available after Webchat has been initialized.

Parameters

The getUser method takes no parameters.

Returns

Promise<User>
A Promise that resolves when the user information has been retrieved.

config

The window.botpress.config method updates Webchat’s configuration and the current user’s data.
window.botpress.config({
  configuration: {},
  user: {}
});
It becomes available after Webchat has been initialized.

Parameters

object
required
An object containing the Webchat configuration and user data to update.

Returns

void

restartConversation

The asynchronous window.botpress.restartConversation method ends the current conversation and starts a new one:
await window.botpress.restartConversation();
It becomes available after Webchat is ready to receive messages.

Parameters

The restartConversation method takes no parameters.

Returns

Promise<void>
A Promise that resolves to void when a new conversation has been successfully created.

sendMessage

The asynchronous window.botpress.sendMessage method sends a new message to your bot on behalf of the current user:
await window.botpress.sendMessage("<string>");
It becomes available after Webchat is ready to receive messages.

Parameters

message
string
required
The message to send to the bot.

Returns

Promise<void>
A Promise that resolves to void when the message has successfully been sent.

sendEvent

The asynchronous window.botpress.sendEvent method sends a new event to your bot:
await window.botpress.sendEvent({});
It becomes available after Webchat is ready to receive messages.

Parameters

event
object
required
An object containing custom data to send to the bot. Can contain any key-value pairs.

Returns

Promise<void>
A Promise that resolves to void when the event has successfully been sent.

setUnreadMessageCount

The window.botpress.setUnreadMessageCount method sets the number of unread messages to display on the Floating Action Button (FAB):
window.botpress.setUnreadMessageCount(123);
It becomes available after Webchat has been initialized.

Parameters

count
number
required
The number of unread messages to display on the FAB.

Returns

void

on

The window.botpress.on method sets up an event listener for a given Webchat event:
window.botpress.on('<string>', (event) => {});

Parameters

type
string
required
The event type to listen for. Can be any of the following event types:
EventDescription
conversationFires when a new conversation starts
messageFires when a message is sent
errorFires when an error occurs
customEventFires for custom events
webchat:initializedFires when Webchat has finished loading and is ready to be opened
webchat:readyFires when Webchat has been opened for the first time and is ready to receive messages
webchat:openedFires when Webchat is opened
webchat:closedFires when Webchat is closed
*Fires for any event (receives all events)
handler
function
required
Callback function that gets called when the event occurs. The function receives event data specific to each event type as its parameter.

Returns

function
An unsubscribe function that can be called to remove the event listener.