Integrate and Customize the Business Messaging (BM) Widget Using JavaScript APIs

Integrating Custom Actions in the Business Messaging Widget

JavaScript API Overview

JavaScript APIs for business messaging are client-side APIs designed to manage contact information, events, and API calls related to the BM widget. You can use these APIs to pre-fill data, retrieve contact details, and customize the widget's appearance.

Using the JavaScript API

To use the necessary JavaScript API, you need to copy and paste the relevant code into the source code of your website within the <script> tags. Before executing the BM script, ensure that you have set the respective JavaScript API details in the code.

BM Widget Setup

To display the BM widget:

  1. Create a Channel in IM.
  2. Obtain the channel's Embedded Code Script.
  3. Inject the Embedded Code Script into your webpage.
  4. Initialize the ZOHOIM object before injecting the script:
    • window.ZOHOIM = window.ZOHOIM || {};
  5. Set the necessary properties for rendering the BM widget within the window.ZOHOIM variable.

BM Widget Script Sample

JavaScript
<script type="text/javascript" nonce="{place_your_nonce_value_here}"
src="https://im.zoho.com/api/v1/public/channel/435798000000140015/widget" defer>
var _d=document;_d.prefilledMessage=
</script>

Set API

Define the following properties in window.ZOHOIM before rendering the widget:

Prop NameTypeDescription
widgetContainerIdstringElement ID in which ZBM will be rendered
widgetLayoutobjectDescribes the options required for customising the ZBM Widget layout
widgetLayout.showWidgetbooleanWhether to show the BM widget
widgetLayout.needWidgetBubblebooleanWhether to render the BM widget with the Bubble Icon or not
widgetLayout.needCloseIconbooleanIn the ZBM Widget header, whether the close Icon is required or not
widgetProps.sessionVariableValuesarrayTo set a session variable for GC bot
**customStylesstringDescribes the options required for customizing the styles of ZBM Widget
customStyles.modestringIn which mode ZBM needs to be rendered. expected values : "light", "dark"
customStyles.themestringDescribes the theme of ZBM widget.   expected value : Hex Color Code
customStyles.actionThemestringDescribes the action theme of ZBM widget. expected value : Hex Color Code
customStyles.wallpaperobjectDescribes the options required for ZBM widget wallpaper.
customStyles.wallpaper.srcobjectDescribes the options required for ZBM widget wallpaper in different modes.
customStyles.wallpaper.src.lightstringWallpaper needs to be rendered in light mode. expected value : Image URL
customStyles.wallpaper.src.darkstringWallpaper needs to be rendered in dark mode. expected value : Image URL
poweredByTagobjectDescribes the options required for rendering the powered by tag in ZBM Widget footer.
poweredByTag.isCheckedbooleanWhether to display the powered by tag or not in the ZBM footer
customHandlersobjectDescribes the options required for the functionality of the ZBM Widget
customHandlers.closeButtonHandlerfunctionExecutes when the close icon is clicked
customHandlers.backButtonHandlerfunctionExecutes when the back icon is clicked
customHandlers.openArticlefunctionExecutes when the article message is clicked

Trigger API

The following methods allow communication between the BM widget and your application.

Method NameDescriptionArgumentsReturn Value
triggerEventtriggers a particular event that executes all the methods which are subscribed to the triggered eventZOHOIM.triggerEvent('eventName',...args)NIL
subscribeToEventA method can subscribe to a particular eventZOHOIM.subscribeToEvent( 'eventName', method)NIL

 Below is a sample code snippet illustrating how to render the BM widget. 


JavaScript
<html>
<head>
<title>Page Title</title>
<script>
window.ZOHOIM = window.ZOHOIM || {};
window.ZOHOIM.widgetContainerId = "test";
window.ZOHOIM.widgetLayout = { needWidgetWithBubble : false, needCloseIcon : true};
window.ZOHOIM.customStyles = '{ "mode": "light", "theme": "#71198c","actionTheme":"#1a7063", "wallpaper": {"src": {"light":"https://fastly.picsum.photos/id/703/536/354.jpg?hmac=1NZ7SzrTrnA-1O2S18kJC-IFIOZyYeHt8x98Iqdd5kM"}},"poweredByTag": { "isChecked": false } }';
customCloseButtonHandler = ()=>{alert('clicked the close button');}
customBackButtonHandler = ()=>{
if(a===1){
a=0;
window.ZOHOIM.triggerEvent('showCloseIcon',true);
}else{
a=1;
window.ZOHOIM.triggerEvent('showCloseIcon',false);
}
}
window.ZOHOIM.customHandlers = {
closeButtonHandler : customCloseButtonHandler,
backButtonHandler : customBackButtonHandler
}
</script>

<script type="text/javascript" nonce="{place_your_nonce_value_here}" src="https://implus.localzoho.com/api/v1/public/channel/c572beb392335194a3f8e528fb092408/widget" defer> var _d=document;_d.prefilledMessage= </script>
</head>

<body>
<div id="test" style="height: 500px; width: 400px; border: 1px solid black;"></div>
</body>
</html>

Language Support and Fallback Behavior

You can configure the languages that you want to display in the BM widget using widgetProps.languages.


JavaScript
<script type='text/javascript'>window.ZOHOIM=window.ZOHOIM||function(a,b){ZOHOIM[a]=b;};window.ZOHOIM.prefilledMessage="";</script>
<script>
window.ZOHOIM = window.ZOHOIM || {};
window.ZOHOIM.locale = 'en_US'; // locale is mandatory
window.ZOHOIM.widgetProps = { languages: ['en_US','fr_FR','ta_IN','ar_EG']}; // locale should be present in the langauges list
</script>


Replace the languages in ['en_US','fr_FR','ta_IN','ar_EG'] with the required language codes in the same format.

The languages configured in widgetProps.languages are displayed in the language dropdown. However, adding a language to this property does not mean that the language is supported by the BM widget.

The language selected in the dropdown, the language used to display the BM widget UI, and the language used by the GC bot depend on different language configurations.

Language dropdown: Determined by the languages configured in widgetProps.languages.

BM widget UI: Depends on whether an IM translation is available for the selected language.

Languages supported in IM

CodeLanguageCodeLanguage
en_USEnglish - USnb_NONorwegian Bokmål
ar_EGArabic - Egyptnl_NLDutch
da_DKDanishor_INOdia
de_DEGermanpl_PLPolish
es_ESSpanishpt_BRPortuguese - Brazil
fr_FRFrenchro_RORomanian
hi_INHindiru_RURussian
in_IDIndonesiansv_SESwedish
is_ISIcelandicta_INTamil
it_ITItaliante_INTelugu
iw_ILHebrewth_THThai
ja_JPJapanesetr_TRTurkish
kk_KZKazakhvi_VNVietnamese
ko_KRKoreanzh_CNChinese - Simplified
zh_TWChinese - Traditional

GC bot response: The language used by the GC bot depends on whether the selected language is supported by IM.

Refer to the  GC supported language documentation to view the complete list of languages supported by the GC bot.

What happens if an unsupported IM language is configured?

You can include a language in widgetProps.languages even when a corresponding IM translation is not available. The language will still appear in the BM language dropdown and can be selected.

However, when the language is selected:

  • The selected language continues to be displayed as the current language in the dropdown.
  • Since the corresponding IM translation is unavailable, the BM widget UI falls back to the default language, English (en_US).
  • The GC bot's response language depends separately on whether the selected language is supported by IM.

Important: A language appearing in the BM language dropdown only indicates that it has been configured in widgetProps.languages. It does not indicate that the language is supported by IM or the GC bot.

Get Contact Info API (Web)

You can provide custom contact information using widgetProps.

Prop NameTypeDescription
ContactInfoobjectDescribes the details of the contact

Sample

JavaScript
window.ZOHOIM.widgetProps = { 'contactInfo': {'name':'Manoj','phone': '+91203354', 'email': 'abc@gmail.com'}};

For Android, refer to this link: Setting the Contact Info for the Business Messaging SDK (Android)

For iOS, refer to this link: Setting the Contact Info for the Business Messaging SDK (iOS)

Authenticated Cases  API

We provide the following methods to handle authenticated flows with Business Messaging.

Method to set Csrfcookie:


JavaScript
 window.ZOHOIM.setCsrfParamName(csrfParamName);
window.ZOHOIM.setCsrfToken(csrfToken);

Method to set OAuth (JWT case):


JavaScript
 window.ZOHOIM.setAuthtoken(token);

Method to set Domain:


JavaScript
window.ZOHOIM.setCustomDomainUrl(DomainURL);

Method to add ZohoAccountId to BM:


JavaScript
window.ZOHOIM.setZaid(zaid);

Pre-Render Action API:

You can execute actions before rendering the widget.


JavaScript
window.ZOHOIM.beforeRenderCallback= () => {
//todo
}

BM Widget Control Changes

We've introduced a set of public methods that allow end users to control the Business Messaging (BM) widget directly from the page. These methods enable dynamic interaction with the widget interface.

Supported Events

Event NameDescription
openWidgetOpens the BM widget
closeWidgetCloses the BM widget
toggleWidgetToggles the widget between open and closed states
showBubbleDisplays the widget's bubble icon
hideBubbleHides the widget's bubble icon

Sample Implementation

The following code snippet is only a sample to demonstrate how you can use the new widget control methods.

JavaScript

<script>
window.ZOHOIM = window.ZOHOIM || {};
function closeWidget() {
const { triggerEvent } = window.ZOHOIM;
triggerEvent('closeWidget');
}
function openWidget() {
const { triggerEvent } = window.ZOHOIM;
triggerEvent('openWidget');
}
function toggleWidget() {
const { triggerEvent } = window.ZOHOIM;
triggerEvent('toggleWidget');
}
</script>

<button type="button" onclick="openWidget()">Open</button>
<button type="button" onclick="closeWidget()">Close</button>
<button type="button" onclick="toggleWidget()">Toggle</button>