Web chat development overview
If you are comfortable with JavaScript code, you can customize and extend the web chat by using the web chat API. You can also use a WebView to embed the web chat in your mobile app.
For detailed reference information about the web chat API, see the web chat developer documentation.
To add the web chat widget to your website or a WebView in your mobile app, all you need to do is embed a generated script element in your HTML source (for more information about the embed script, see Embedding the web chat on your page). Within this embed script, you can use the web chat API to customize or extend the web chat.
The web chat API consists of several components:
- Configuration object: The embed script defines a configuration object named
watsonAssistantChatOptions
, which specifies configuration objects for the web chat widget. By editing the configuration object, you can customize the appearance and behavior of the web chat before it is rendered. For more information about the available configuration options, see Configuration options object in the web API reference. - Instance methods: The web chat instance methods provide low-level control of the web chat widget. You can use the instance methods to implement custom behavior such as changing how the web chat widget opens, showing and hiding content, and setting identity information. For more information about the available instance methods, see List of methods and properties in the web chat API reference.
- Events: The web chat event system makes it possible for your website to respond to web chat events (such as opening or closing the web chat window, sending or receiving messages). By subscribing to events, you can implement custom behavior or even intercept and modify message content. For more information about the event system, see Events in the web chat API reference.
If you want to use the web chat API to customize your web chat implementation, you don't have to start from scratch. Tutorials are available that show examples of common web chat customizations. For more information, see Web chat development tutorials.
Development tasks
You can use the web chat API to customize and extend the web chat in the following ways:
- Web chat style and content
- Opening, closing, and rendering the web chat window
- Customizing the conversation
- Managing data
- Security and administration
For more examples of web chat integrations, see here.
Web chat style and content
- Customizing the look of the web chat
-
If you want to customize the style and appearance of the web chat beyond the options that are available on the Style tab in the web chat settings, you can do so by choosing a different Carbon Design theme.
The supported themes are color themes that are defined by IBM Carbon Design. To set the Carbon theme, use the
carbonTheme
configuration option.You can also set individual variables within the theme to customize specific UI elements. For example, the text that is displayed in the chat window uses the fonts
IBMPlexSans, Arial, Helvetica, sans-serif
. If you want to use a different font, you can specify it by using theupdateCSSVariables()
instance method. - Customizing the home screen
-
The home screen greets the customer and optionally shows a list of suggested conversation starters. You can customize the style and content of the home screen:
-
To add elements to the home screen, you can define custom HTML using the
writeableElements.homeScreenAfterStartersElement
theming variable:instance.writeableElements.homeScreenAfterStartersElement.innerHTML = '<div class="MyCustomClass">This is my custom element</div>';
Example: For a working example that shows how to add custom elements to the home screen, see Home screen custom elements for watsonx Assistant web chat.
-
To change the home screen style, use CSS helper classes.
-
- Customizing strings
-
You can customize the strings that define the various labels and hardcoded phrases displayed by the web chat. To customize strings, use the
updateLanguagePack()
instance method to replace strings in the current language pack. For more information, see Languages. - Supporting global audiences
-
By default, the strings displayed by the web chat are in English. To change to a different language, use the
updateLanguagePack()
instance method to replace the current language pack with one of the available translated language packs. For more information, see Supporting global audiences in web chat.
Opening, closing, and rendering the web chat window
- Replacing the default launcher
-
To better integrate with your website, you might want to replace the built-in launcher icon with a different mechanism for opening the web chat. To hide the default launcher, set the
showLauncher
configuration option tofalse
. To open the web chat based on some other interaction, use theopenWindow
instance method.Tutorial: For a tutorial that shows you how to implement a custom launcher, see Using a custom launcher.
- Keeping the web chat always open
-
If you want to keep the web chat always open on your page, use the
openChatByDefault
configuration open to render the page with the chat window open, and thehideCloseButton
option to prevent customers from closing it. - Changing the size or position of the web chat
-
Your website design might require that you change where and how the web chat window renders on your website. For example, you might want it to appear in a different position on the page, at a different size, or nested within another element on the page.
To change the size of the web chat window, you can use the updateCSSVariables() instance method to modify the CSS styling.
If you need to change the position of the web chat window, or you need to change the size beyond the limits allowed in the CSS, you can use a custom DOM element to contain the web chat window. To do this, use the
element
configuration option.Tutorial: For a tutorial that shows how render the web chat in a custom element, see Tutorial: Customizing the size and position of the web chat.
- Adding the web chat to your mobile application
-
You can use a WebView with a JavaScript bridge to add the web chat to your mobile application. For more information, see Adding the web chat to your mobile application.
Customizing the conversation
- Intercepting and modifying messages
-
By subscribing to events, your code can intercept messages that are sent and received between the customer and the assistant, and even modify their content.
Subscribe to the
pre:receive
event if you want to intercept a customer's message before it is sent to the assistant. For example, you might want to remove personally identifiable information from the message text or add context variables to the message payload.Subscribe to the
pre:send
event if you want to intercept an incoming response from the assistant before it is shown to the customer. For example, you might want to add formatting, links, or other elements that are specific to the web chat.Tutorial: For a tutorial showing how to use the
pre:receive
event to intercept and modify incoming messages, see Tutorial: implementing custom option buttons in the web chat. - Rendering custom response types
-
If your assistant sends specialized responses using a custom (
user_defined
) response type, you can implement a custom view to display these responses in the web chat window. To do this, subscribe to thecustomResponse
event, which is fired each time auser_defined
response is received. In your event handler method, you can then read the contents of the response and render the output using your own elements.Tutorial: For a tutorial showing how to render a custom response type as a replacement for the default options response, see Tutorial: implementing custom option buttons in the web chat.
- Implementing a contact center integration
-
You can use one of the web chat starter kits to integrate with a contact center (service desk) platform other than the ones available in as built-in watsonx Assistant integrations. Fully functional reference implementations are available for several contact center platforms; in addition, you can use a starter kit to develop a custom integration with the platform of your choice.
For more information, see Adding contact center support.
Managing data
- Managing user identity information
-
The web chat associates a user ID with each message it sends to the assistant; this user ID is used for user-based billing and other purposes. You can either allow the web chat to generate an anonymous ID for each user, or you can control the user ID yourself.
Depending on whether you have enabled security for the web chat, you can use either the
updateUserID
instance method or theupdateIdentityToken
method to specify user identity information.For more information about how user identity information is specified and how it is used, see Managing user identity information in web chat.
Security and administration
- Securing the web chat
-
To secure the web chat, you can use JSON Web Token (JWT) to authenticate users and encrypt private data. For more information, see Securing the web chat.
- Controlling the web chat version
-
The web chat code hosted by IBM Cloud is regularly updated with improvements and new features. By default, the embed script automatically uses the latest version of the web chat. To avoid unexpected changes that might affect your website, you might want to control which version of the web chat your website uses, giving you an opportunity to test each new version before you deploy in in production., in order to avoid unexpected changes when a new version is released.
For more information about web chat versioning, see Controlling the web chat version.