How to register external events in Chatbot

Sometimes you might want to change the way the chatbot sends requests to the server and modify the response from the server to make the user experience better. Such changes are denoted as external events and this document gives an overview of how to register these external events in the chatbot.

The external events can only be used with Chatbot version V2.

For example,

  1. Modify the input text (to adjust for the accents) before sending the request to the server,

  2. Change the response of the chatbot based on user input, and so on.

The external site can register the below events:

Event

Description

Event

Description

afterInit

This event will get triggered after the chatbot is loaded.

beforeSentRequest

This event will get triggered before the chatbot user’s request is passed to the server.

afterResponse

This event will get triggered after the chatbot receives the response from the server. (before displaying the response to the user)

onDockCollapse

This event will get triggered as soon as the bot is docked/collapsed. (only for dockfloat theme)

onDockOpen

This event will get triggered as soon as the bot is undocked/opened. (only for dockfloat theme)

Instructions

To register external events in the chatbot, you have to add the below code in the script tag as described in the document.

if (OrbitaChatBotPlugin) { OrbitaChatBotPlugin.registerEvents('afterInit', function () { } ); OrbitaChatBotPlugin.registerEvents('beforeSentRequest', function (data) { // your business logic return data; } ); OrbitaChatBotPlugin.registerEvents('afterResponse', function (data) { // your business logic // eg: data.orbitaPayload.payload.multiagent.chat.chatText = data.orbitaPayload.payload.multiagent.chat.chatText+ 'Hook custom apppend'; return data; } ); OrbitaChatBotPlugin.registerEvents('onDockCollapse', function () { // Function call on an event } ); OrbitaChatBotPlugin.registerEvents('onDockOpen', function () { // Function call on an event } ); OrbitaChatBotPlugin.initPlugin(option); }

The structure of the data object should not be modified.

A sample of the entire <script> tag along with the external events code is given below.

<script>var currentAppUrl = 'http://localhost:3030';//window.location.protocol + '//' + window.location.hostname + ':' + window.location.port; var option = { domId: 'chatWindow', launchWord: 'open', name: 'Orbita', height: '520',// *Required property width: '520', settings: { "header": { // "logoUrl": "/chatbot/v2/assets/branding-mark.svg", "subHeaderText": "I am your virtual health assistant", "speakerOnIconUrl": "/chatbot/v2/assets/volume-on.svg", "speakerMuteIconUrl": "/chatbot/v2/assets/volume-muted.svg", "closeIconUrl": "/chatbot/v2/assets/x-header.svg" }, "banner": { "title": "", "message": "" }, "profile": { "defaultAvatorUrl": "" }, "footer": { "menuIconUrl": "/chatbot/v2/assets/plus-button.svg", "menuIconDiabledUrl": "/chatbot/v2/assets/plus-btn-disabled.svg", "micIconUrl": "/chatbot/v2/assets/mic.svg", "micOnIconUrl": "/chatbot/v2/assets/mic-on.svg", "sendButtonIconUrl": "/chatbot/v2/assets/button-circular-01.svg", "sendButtonDisabledIconUrl": "/chatbot/v2/assets/send-circular-01-disabled.svg" }, "botAvatorUrl": "/chatbot/v2/assets/orbita-icon.svg", "animationAvatorImageUrl": "/chatbot/v2/assets/avatar_loading.gif", "animationImageUrl": "/chatbot/v2/assets/msg_loading.gif", "popover": { "text": "Hi there do you want help parent ?", "waitTime": 3000, "documentTitle": "New Message!", "titleChangeTime": 2000 } }, serverUrl: currentAppUrl + '/oeapi/bot/web', oathuserverUrl: currentAppUrl, clientId: '', clientSecret: '', enableLogin: true, theme: 'dock-float', // dockDefaultAction: 'open' }; if (OrbitaChatBotPlugin) { OrbitaChatBotPlugin.registerEvents('afterInit', function () { } ); OrbitaChatBotPlugin.registerEvents('beforeSentRequest', function (data) { // your business logic return data; } ); OrbitaChatBotPlugin.registerEvents('afterResponse', function (data) { // your business logic // eg: data.orbitaPayload.payload.multiagent.chat.chatText = data.orbitaPayload.payload.multiagent.chat.chatText+ 'Hook custom apppend'; return data; } ); OrbitaChatBotPlugin.registerEvents('onDockCollapse', function () { // Function call on an event } ); OrbitaChatBotPlugin.registerEvents('onDockOpen', function () { // Function call on an event } ); OrbitaChatBotPlugin.initPlugin(option); }</script>