Methods

This section describes the methods available on the WebChat SDK

The WebChat SDK provides the following methods that allow you to interact with your chat widget programmatically:

  • .init()

  • .destroy()

  • .refresh()

  • .restart()

  • .open()

  • .close()

  • .sendMessage()


Initialise

Initialise an instance of WebChat using your preferred settings and - optionally - a set of custom components and an existing user ID:

/**
* Initialise the chat widget with settings and custom components
* @function init
* @param {Object} [settings={}] - settings object (optional)
* @param {Array} [customComponents=[]] - array of custom components (optional)
* @param {?string} [userId] - uuid for the user (optional)
*/

.init(settings, customComponents, userId)

If you do not provide a settings object, WebChat will look for one on the window.openDialogSettings object. If that's not present either, initialisation will fail. At a bare minimum, the settings object MUST include the following:


Destroy

Destroy an existing WebChat widget:


Refresh

Refresh your WebChat widget with new settings. Does not change the conversation in any way. Calling refresh() will make a new call out to the /chatApi-config endpoint to ensure that any other interface settings affected by your changes are also updated. Your new settings will then be applied over those received from the server.


Restart

Restart your WebChat widget with new settings, a new starting intent, and/or a new user. Restart is distinct from Refresh because it affects the current conversational state.

The first parameter - clearUser: Boolean - determines whether to refresh the conversation from the beginning with the same user, or whether to clear all user data and start again from the very beginning. In both instances, the message list will be cleared ready for a new conversation to begin (note that this does not clear the user history, so if you retain the current user and they refresh the page then, depending on your settings, they may see the previous conversational history rendered)

If you provide an openIntent: string parameter, you can tell WebChat where to restart the conversation from. This success relies a little on your conversation design as this intent needs to be available at the point of request.

The parameter userId: 'string' is only applicable where clearUser === true and will allow you to restart the conversation with a given user.


Open

Opens the WebChat widget. The current open state of the widget is exposed on the instance and can be interrogated by calling webchat.openState


Close

Closes the WebChat widget. The current open state of the widget is exposed on the instance and can be interrogated by calling webchat.openState


Send Message

Send a message to chatApi through the WebChat widget. Note that the success of this functionality is very much dependent on your conversation design. The correct intent needs to be available to the conversation engine at the point of requesting!

Last updated