In your SAPUI5 view controller, in the onAfterRendering method, add the script tag for adding your Web Client to a web page. The script is available in the Connect tab when developing your chatbot.
javascript[4-11]
onAfterRendering: function () {
// Set up chatbot
// this.renderCAIChatBot()
var s = document.createElement("script");
s.setAttribute("src", "https://cdn.cai.tools.sap/webclient/bootstrap.js");
s.setAttribute("id", "cai-webclient-custom");
s.setAttribute("data-expander-preferences",data_expander_preferences);
s.setAttribute("data-channel-id",data_channel_id);
s.setAttribute("data-token",data_token);
s.setAttribute("data-expander-type","CAI");
document.body.appendChild(s);
},
For the specific IDs and tokens for your chatbot, place a new file webclient.js inside the controller folder (since you may want to change the chatbot from time to time) and add just the following few lines. Make sure to enter values for your chatbot.
The theme selector will be empty because you did not create a data model with all the themes. You will do this in the next step.
Step 3Add data model for themes
+
Put the following in the onInit method. This will add the theme options to the `` element.
JavaScript
varthemes={"themes":[{"name":"SAP Belize","theme":"sap_belize"},{"name":"SAP Belize High Contrast Black","theme":"sap_belize_hcb"},{"name":"SAP Belize High Contrast White","theme":"sap_belize_hcw"},{"name":"SAP Quartz","theme":"sap_fiori_3"},{"name":"SAP Quartz Dark","theme":"sap_fiori_3_dark"},{"name":"SAP Quartz High Contrast White","theme":"sap_fiori_3_hcw"},{"name":"SAP Quartz High Contrast Black","theme":"sap_fiori_3_hcb"},{"name":"SAP Horizon","theme":"sap_horizon"},{"name":"SAP Horizon Experimental","theme":"sap_horizon_exp"}]};// set explored app's demo model on this sample
varoModel=newJSONModel(themes);this.getView().setModel(oModel,"themes");
You can refresh the app and now there will be theme options.
Theme options
Step 4Add event handlers
+
In the view controller, add the following methods:
JavaScript
// Open Web Client
onPressOpen:function(evt){window.sap.cai.webclient.show();},// Close Web Client
onPressClose:function(evt){window.sap.cai.webclient.hide();},// Toggle Web Client, open or close
onPressToggle:function(evt){window.sap.cai.webclient.toggle();},// Send a message to the chatbot, as if it came from the user
onPressMessage:function(evt){varoView=this.getView(),myinput=oView.byId("messageText");window.sap.cai.webclient.sendMessage(myinput.getValue());},// Change the theme of the chatbot
onThemeChange:function(oEvent){varoView=this.getView(),mytheme=oView.byId("themeSelect");window.sap.cai.webclient.setTheme(mytheme.getSelectedKey());},
Step 5Try the web client APIs
+
Try out the Web Client methods.
Click Open to open the chatbot, Close to close the chatbot, and Toggle to do either.
Toggle
Enter hi in the text field, and then click Send.
Send message
From the dropdown box, select Quartz Dark, and notice the chatbot theme changes.
Change theme
Step 6Set up Web Client bridge
+
Create a file called webclientbridge.js and add it to your controller folder.
In the following steps, you will add Web client listeners inside the webclientBridge object for various tasks.
Step 7Change memory
+
Add the following method to the webclientBridge object:
JavaScript
// Called on every utterance
getMemory:()=>{letmemory;memory={'mynumber':200}return{memory,merge:true}},
Update the chatbot to display the memory. In this chatbot, typing “my number” displays the memory variable mynumber, which is set to 0 in the Initialize skill.
On subsequent calls, the memory is updated from this Web Client Bridge function. The merge value indicates whether to wipe out the current memory or to just update the values being returned.
Update memory
Step 8Capture messages
+
Add the following method to the webclientBridge object:
JavaScript
// called on every message
onMessage:(payload)=>{vardisplayMessagepayload.messages.forEach(element=>{displayMessage=''if(element.participant.isBot){displayMessage="THE BOT SAID:\n\n";}else{displayMessage="THE USER SAID:\n\n"}displayMessage+=element.attachment.content.text});alert(displayMessage)},
The above code will now display an alert for each message, indicating if it was sent by the user or the chatbot, and the text of the message.
Capture message
You can inspect the payload object in the method to see what other information is provided.
Step 9Capture data messages
+
Capturing messages lets the chatbot communicate with the web client. But what if the chatbot wants to communicate but not display any message to the user.
In such cases, the chatbot can send a “client data” message, which sends a JSON payload that is not displayed in the chat (the chatbot can send additional messages that are displayed).
In your chatbot, go to the Build > Greetings skill, and open the Action tab.
At the end of the greetings action group, add a new message of type Client Data.
Client data message
Add one key-value pair, with the key emoji and the value any emoji you want.
Client data emoji
Replace the onMessage function to the one below.
JavaScript
// called on every message
onMessage:(payload)=>{payload.messages.map(message=>{if(message.attachment.type=='client_data'){message.attachment.content.elements.map(pair=>{if(pair.key=='emoji'){alert("Here is a special gift for you:\n\n"+pair.value)}})}});},
The above code will now display an alert to the user with the emoji the chatbot sent – without displaying any extra message to the user in the chatbot (it will still send the greeting message).
Capture message
See at the end of some use cases for this feature.
Step 10Set client info and preferences
+
The following two methods are called at the start of the conversation, and used to set the theme (including some texts) and the client information, which is accessible to the chatbot.
Add the following method to the webclientBridge object:
JavaScript
// Called once when the WebClient is loaded, and sets client info
getClientInfo:(defaults)=>{return{language:'en',forceLanguage:false,timezone:'America/Los_Angeles'}},
If Initialize skill sets the current timezone based on the client_info object, you can set up a skill to display the time and the time zone used.
Here, we ask for the time at 15:05 in Jerusalem, but because we set the client info, it is displayed as 6:04 in the Los Angeles time zone.
Client info
Add the following method to the webclientBridge object:
JavaScript
// Called once when the WebClient is loaded, and sets CSS preferences
getChannelPreferences:()=>{return{// all preferences to be overwritten dynamically
accentColor:'blue',botMessageBackgroundColor:'#808000',botMessageColor:'green',complementaryColor:'#808000',backgroundColor:'olive',headerTitle:'Community Chatbot Avatar',userInputPlaceholder:"Let's talk",botPicture:'https://avatars.githubusercontent.com/in/155597?s=88&v=4',}}
The preferences let you set some of the color settings, as well as texts and icons.
The texts and icons are always used, but the CSS colors are only used if you have set the Custom Style SHeet setting in the Web Client channel.
Preferencesย
Step 112 use cases
+
A use case originally created for the Web Chat was to allow a user to type into the chatbot whether to move or zoom a Google map embedded in the web app. The chatbot interprets what the user wants and sends the results as a message, and the web page intercepts that message and calls the appropriate Google API to adjust the map.
Another use case was with the recent SAP Community Coding Challenge. A user indicates they want to download their Avatar into the SAPUI5 Image Editor, gives a community ID, and web app intercepts the message and loads the avatar.
Share feedback on this tutorial or join the conversation in SAP Community.
Submit detailed feedbackDiscuss in Community
Steps
Step 1 of 11
1. Add Web Client script2. Add footer toolbar3. Add data model for themes4. Add event handlers5. Try the web client APIs6. Set up Web Client bridge7. Change memory8. Capture messages9. Capture data messages10. Set client info and preferences11. 2 use cases
Joule
AI Notice
Joule is an AI assistant. Generative AI may produce inaccurate, incomplete, or biased information. Always verify important details before acting on them.
Conversations are sent to SAP-hosted large language models for processing. Do not include personal data, credentials, or confidential information in your messages.
Joule's responses are based on the SAP tutorial catalog and may not reflect the latest product changes. For authoritative guidance, consult the linked tutorials and official SAP documentation.