Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
Showing results for 
Search instead for 
Did you mean: 
This is the first part of a 2-part tutorial.

Very Short Summary

This is a post to show steps of building a custom user interface for a chatbot in UI5. The chatbot can be added to an existing UI5 app that is already deployed on SAP Cloud Platform. SAP Conversational AI API is used to process the user input, detect the intention and give a response based on the skills we create. Finally, a fragment view is used to display the chat between user and chatbot. Features such as Speech-To-Text are also included to enhance user experience.

Target Audience

You want to create your own UI for your chatbot.

You have a UI5 web application.

You want to be able to add new non-native features that don’t currently exist on the SAP Conversational AI platform.

You don’t want to use the default web chat UI given by the SAP. (This webchat UI can be found here at this view in your SAP Conversational AI platform account.


About this post

I wanted to add voice control to my chatbot that I was trying to integrate into an existing SAP Cloud Platform application. Voice control is not a native feature in SAP Conversational AI at the time of writing this article. I needed to create my own user interface for the chatbot so that I could add a mic button to activate speech-to-text feature.


I came across the following two blogs:

First one is about speech recognition via Google DialogFlow Natural Process Engine.

Second one is about ChatBot Integration to a Fiori App. Credits go to creators of those blog posts Manisha Madhwani and Rishikesh Aarsh for the idea as they inspired me.

I found tutorials for chatbot integration and some other blog posts for SAP Conversational AI and then some others on how to add custom features, but there wasn’t one place that helped me. I had to gather information from several different resources. This post is not a silver-bullet for custom chatbot UIs or non-native chatbot features but a comprehensive entry point for anyone who wants to take start developing a chatbot UI with non-native features such voice activation.



You are familiar with API usage and Ajax calls.

You have an account in SAP Conversational AI.

You are using Google Chrome Web Browser (Speech Recognition works only on Chrome)

What you will learn:

Part 1 of this post

  • Getting an SAP Conversational AI developer token

  • Making fetch requests to an SAP Conversational AI endpoint

  • Creating a Welcome Message

  • Creating a Pop over fragment in your Fiori App so that users can click to open the bot

Part 2 of this post

  • Adding custom CSS to modify the interface of the chatbot container

  • Creating a basic skill in SAP Conversational AI

  • Implementing a button to the pop over in order to activate your microphone and speech-to-text conversion.

  • Adding a Fallback skill to the chatbot so that bot says “I don’t get it” when the input is unclear to the bot

  • Adding a “Bot is typing status” to the pop over


What is a Chatbot and why you would need it

A chatbot can speed up user interaction, increase retention rate for a website and enhance the user experience. It can increase the efficiency of navigation for users and help them find them what they are looking for in the website.


What is SAP Conversational AI?

SAP Conversational AI is bought by SAP and got integrated into our system. It was formerly known as Currently, SAP Employees can create an account for free at the time of writing this article and start using their API by visiting here

How to follow steps in this post?

I will give snippets of functions. In your UI5 application, you can add these functions to your root component without affecting the existing functionality in your app. In your app, your root component might look like this based on whether you are extending a controller or creating your controller from scratch.
], function (jQuery, Fragment, Controller, JSONModel, Popover, Button, BaseController, Dialog, MessageToast, XMLView) {
"use strict";

var CController = BaseController.extend("mydomain.myappname.mycomponentname.", {
model: new sap.ui.model.json.JSONModel(),
data: {},
onInit: function () {
var that = this;
var oStatus = sap.ui.getCore().byId("statusbar");


Learning #1 Getting an SAP Conversational AI developer token

We need to include the developer and request tokens to our GET request code. The tokens can be located in the SAP Conversational AI platform as follows:



Learning #2 Making fetch requests to an SAP Conversational AI endpoint

We will use our developer tokens that we got from step 1 to make requests to the SAP Conversational AI API. In this step, we must initialize the instance of the bot after the view rendering is complete.
onAfterRendering: function () {
this.getBotInstanceId(); //get user uuid of your bot account in
this.getView().byId("chatBtn").attachPress(this.openBot.bind(this)); //attaching press to open chat window

getBotInstanceId: function () {
var that = this;
type: "GET",
url: "https://" + "<Your account user name is here> ",
headers: {
"Authorization": "Your Request Token Here" //request token
success: function (data) {
that.uuid =;
//"Bot instance is successfully created");
//console.log("uuid" + that.uuid);
error: function (data) {
that.botError = data;"Bot instance failed to create");



I do have a component name as “mydomain.myappname.mycomponentname” in my BaseController.extend(). When you are using these code snippets, please make sure that your app name replaces this placeholder.

Learning # 3: Creating a Welcome Message when the pop over opens

We need to have a function to parse the user input, create a message element, respond to the user with the message element, and display message on our UI.

First, we add createMessage function.
	createMessage: function (from, message, delay, tweeter_flag) {
var sSrc, sStyle;
var listItem;
if (from === "bot") {

sSrc = "";
sStyle = "botStyle";

listItem = new sap.m.CustomListItem({
content: [
new sap.m.Image({
src: sSrc,
height: "2rem"

new sap.m.Text({
text: message


} else if (from === "user") {
sSrc = "";
sStyle = "userStyle";
listItem = new sap.m.CustomListItem({
content: [
new sap.m.Text({
text: message
new sap.m.Image({
src: sSrc,
height: "2rem"

var oChatList = this.byId(sap.ui.core.Fragment.createId(this.createId("chatBotFragment"), "list1"));

var oScrollContainer = this.byId(sap.ui.core.Fragment.createId(this.createId("chatBotFragment"), "scrollBot"));


There is a tweeter_flag that builds a query string to get tweets from Twitter API. Tweeter feature is beyond the scope of this post and can be considered as a placeholder right now.
	parseText: function (event) {
var message;
if (event.sId === "change") {
try {

} catch (error) {


message = event.mParameters.newValue.trim();

if (message !== "") {
var content = this.getView();
var inputId = sap.ui.core.Fragment.createId(this.createId("chatBotFragment"), "chat");
var myChat = content.byId(inputId);
this.createMessage("user", message, "0", "false");


respondTo: function (message) {
if (message.indexOf(" ") === -1)
msgLength = 1;
msgLength = message.split(" ").length;

// maximum response length is 2 more words than the incoming message
responseLength = Math.ceil(Math.random() * (msgLength + 2));

// longer sentences should get a comma
if (responseLength > 😎
comma = Math.ceil(responseLength / 2);

// simulated delayed response
delay = Math.ceil(Math.random() * (responseLength + 1) * 1000) + 2500;
if (msgLength > 0) { //if user has inputted message then
var _data = {
"message": {
"type": "text",
"content": message
"conversation_id": "test-1533969037613",
"log_level": "info"
var that = this;
type: "POST",
data: JSON.stringify(_data),
url: "https://" + "", //bot connector callback url you will find under settings>options
contentType: "application/json",
path: "/build/v1/dialog",
scheme: "https",
headers: {
"Authorization": "Token xxxxxxxxxxxxxxxxxxxxx", //developer token
"x-uuid": that.uuid
success: function (data) {

that.pqaBotConversation = data;
var user_input = data.results.logs.input;

try {
} catch(error) {

var tweeter_flag;
// Remove Bot is typing.
if (data.results.conversation.skill=="fallback") {
that.createMessage("bot", fallback, "0", "false");
else if (data.results.messages[0].content != "") {
tweeter_flag = "false";
that.createMessage("bot", data.results.messages[0].content, delay, tweeter_flag);
} else {
that.createMessage("bot", "I dont know what to do. I dont understand it", delay, tweeter_flag);
error: function (data) {
that.botError = data;
var fallback = "I don't understand it."
that.createMessage("bot", fallback, "0", "false");


Once we are done with parsing and responding, we create a function for welcome message generation for the user.
createWelcomeMessage: function () {
var welcome = "Hi there! My name is Minion. I can help you search for tweets. Just use # symbol for what you want to search. I can also navigate to different view. You can say go to my tasks. Or you can simply ask me jokes!";

Once we implement the previous functions, sending a welcome message is achieved by calling the createMessage function with a welcome message inside the openBot function. Make sure that you call the following line in the openBot function

This will send a welcome message, whenever your chat bot is open.

Learning #4: Creating a Pop over fragment in your Fiori App so that users can click to open the bot

So far we have implemented functions to handle messaging, but we never created the UI for the user to see a UI container for our chat. Now, we are creating a view fragment to achieve a user container.

We are creating a Popover fragment view placed inside the view folder in our UI5 web app. Its name should be Popover.fragment.xml

Here is the function definition that would create an instance of the fragment view along with a function named openBot.  openBot function has a line "this.oChatBot.openBy(this.byId('chatBtn'))". This will open our fragment view by the button with ID chatBtn. This means that you have to add a button to your existing application, and make sure that the button has the ID "chatBtn".
handleChatButton: function (oEvent) {
//create popover
if (!this._oPopover) {
var typingStatus = {
value: "Bot is typing..."
this._oPopover = sap.ui.xmlfragment(this.getView().getId(), "interndashboard.view.Popover", this);
// this._oPopover.setModel(this.getView().getModel());
this.typingStatus = typingStatus;
if (this._oPopover.isOpen()) {
var oChatList = this.byId(sap.ui.core.Fragment.createId(this.createId("chatBotFragment"), "list1"));
if (oChatList.getMaxItemsCount() > 0) {
this.chatScroll.scrollToElement(oChatList.getItems()[oChatList.getMaxItemsCount() - 1]);


openBot: function (oEvent) {
if (!this.oChatBot) {
this.oChatBot = sap.ui.xmlfragment(this.createId("chatBotFragment"), "interndashboard.view.Popover", this);
var oModel = new sap.ui.model.json.JSONModel({
data: {}
this.oChatBot.openBy(this.byId('chatBtn')); //detailContent.getFooter().getContentRight()[1]);
this.chatInput = this.byId(sap.ui.core.Fragment.createId(this.createId("chatBotFragment"), "chat"));
this.chatInput.atttachBrowserEvent("keyup", this.parseText.bind(this), false);

Here is the XML fragment view of what we are going to open.
<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core" id="chatbotFragment">
<Popover showArrow="true" title="Minion ChatBot" placement="Auto" horizontalScrolling="true" id="botPopover">
<FlexBox xmlns:mvc="sap.ui.core.mvc" xmlns:layout="sap.ui.layout" xmlns:microchart="sap.suite.ui.microchart" xmlns="sap.m" width="300px"
class="container" height="400px" justifyContent="Center" wrap="Wrap" alignItems="End">
<FlexBox width="300px" height="80%" alignItems="End">
<ScrollContainer id="scrollBot" height="100%" width="300px" horizontal="false" vertical="true" focusable="true">
<List id="list1" width="300px" enableBusyIndicator="false" showSeparator="None">
<CustomListItem id="item1">
<FlexBox width="100%" height="10px">
<Text id="statusbar" class="statusBar" text="Bot is typing..." visible="false"></Text>
<FlexBox width="100%" class="chat_input full" height="15%">
<Input width="260px" id="chat" placeholder="Your message..." change="parseText"/>
<Button type="Emphasized" press="handleSpeechToText" id="mic"
icon="" text=""/>

Our pop over fragment view has to be placed inside the view folder in our UI5 app.


For the remaining steps, please make sure that you check part 2 of this blog series. Happy coding!

To see my other projects, you can check my personal website, GitHub page and Linkedin Page: