sap.ui.define([
'jquery.sap.global',
'sap/ui/core/Fragment',
'sap/ui/core/mvc/Controller',
'sap/ui/model/json/JSONModel',
'sap/m/Popover',
'sap/m/Button',
"../controller/BaseController",
"sap/m/Dialog",
"sap/m/MessageToast",
"sap/ui/core/mvc/XMLView"
], 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");
},
onAfterRendering: function () {
this.getBotInstanceId(); //get user uuid of your bot account in recast.ai
this.getView().byId("chatBtn").attachPress(this.openBot.bind(this)); //attaching press to open chat window
},
getBotInstanceId: function () {
var that = this;
$.ajax({
type: "GET",
url: "https://" + "api.recast.ai/auth/v1/owners/<Your account user name is here> ",
headers: {
"Authorization": "Your Request Token Here" //request token
},
success: function (data) {
that.uuid = data.results.owner.id;
//MessageToast.show("Bot instance is successfully created");
//console.log("uuid" + that.uuid);
},
error: function (data) {
that.botError = data;
MessageToast.show("Bot instance failed to create");
}
});
}
createMessage: function (from, message, delay, tweeter_flag) {
var sSrc, sStyle;
var listItem;
if (from === "bot") {
sSrc = "https://cdn.recast.ai/webchat/bot.png";
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 = "https://cdn.recast.ai/webchat/user.png";
sStyle = "userStyle";
listItem = new sap.m.CustomListItem({
content: [
new sap.m.Text({
text: message
}),
new sap.m.Image({
src: sSrc,
height: "2rem"
})
]
});
}
listItem.addStyleClass(sStyle);
var oChatList = this.byId(sap.ui.core.Fragment.createId(this.createId("chatBotFragment"), "list1"));
oChatList.addItem(listItem);
var oScrollContainer = this.byId(sap.ui.core.Fragment.createId(this.createId("chatBotFragment"), "scrollBot"));
oScrollContainer.rerender(true);
oScrollContainer.scrollToElement(listItem);
},
parseText: function (event) {
var message;
if (event.sId === "change") {
try {
sap.ui.getCore().byId("sap-ui-invisible-interndashboard---app--chatBotFragment--statusbar").setVisible(true);
} catch (error) {
sap.ui.getCore().byId("interndashboard---app--chatBotFragment--statusbar").setVisible(true);
}
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);
myChat.setValue("");
this.createMessage("user", message, "0", "false");
this.respondTo(message);
}
}
},
respondTo: function (message) {
if (message.indexOf(" ") === -1)
msgLength = 1;
else
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;
$.ajax({
type: "POST",
data: JSON.stringify(_data),
url: "https://" + "api.recast.ai/build/v1/dialog", //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 {
sap.ui.getCore().byId("interndashboard---app--chatBotFragment--statusbar").setVisible(false);
} catch(error) {
sap.ui.getCore().byId("sap-ui-invisible-interndashboard---app--chatBotFragment--statusbar").setVisible(false);
}
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");
}
});
}
},
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!";
this.createMessage("bot",welcome,"0","false");
},
this.createWelcomeMessage()
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.getView().addDependent(this._oPopover);
this.typingStatus = typingStatus;
}
if (this._oPopover.isOpen()) {
this._oPopover.openBy(oEvent.getSource());
var oChatList = this.byId(sap.ui.core.Fragment.createId(this.createId("chatBotFragment"), "list1"));
this.chatScroll.rerender(true);
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);
this.oChatBot.attachBeforeOpen(this.onBeforeBotOpen.bind(this));
}
var oModel = new sap.ui.model.json.JSONModel({
data: {}
});
this.oChatBot.setModel(oModel);
this.oChatBot.openBy(this.byId('chatBtn')); //detailContent.getFooter().getContentRight()[1]);
this.createWelcomeMessage();
this.chatInput = this.byId(sap.ui.core.Fragment.createId(this.createId("chatBotFragment"), "chat"));
this.chatInput.atttachBrowserEvent("keyup", this.parseText.bind(this), false);
this.chatInput.addStyleClass("botInput");
},
<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">
<items>
<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">
<items>
<CustomListItem id="item1">
<content></content>
</CustomListItem>
</items>
</List>
</ScrollContainer>
</FlexBox>
<FlexBox width="100%" height="10px">
<items>
<Text id="statusbar" class="statusBar" text="Bot is typing..." visible="false"></Text>
</items>
</FlexBox>
<FlexBox width="100%" class="chat_input full" height="15%">
<items>
<Input width="260px" id="chat" placeholder="Your message..." change="parseText"/>
<Button type="Emphasized" press="handleSpeechToText" id="mic"
icon="https://www.google.com/intl/en/chrome/assets/common/images/content/mic.gif" text=""/>
</items>
</FlexBox>
</items>
</FlexBox>
</Popover>
</core:FragmentDefinition>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
25 | |
18 | |
16 | |
12 | |
10 | |
8 | |
8 | |
7 | |
7 | |
7 |