2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
|
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| ( ) |( (_) ) |
|
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
| | | ( )_) | |
|
|
|
|
| (_) \___/' |
|
|
|
|
| |
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
| Licensed under the AGPL-3.0. |
|
|
|
|
| |
|
|
|
|
| According to our dual licensing model, this program can be used either |
|
|
|
|
| under the terms of the GNU Affero General Public License, version 3, |
|
|
|
|
| or under a proprietary license. |
|
|
|
|
| |
|
|
|
|
| The texts of the GNU Affero General Public License with an additional |
|
|
|
|
| permission and of our proprietary license can be found at and |
|
|
|
|
| in the LICENSE file you have received along with this program. |
|
|
|
|
| |
|
|
|
|
| This program is distributed in the hope that it will be useful, |
|
|
|
|
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
|
|
|
| "General Bots" is a registered trademark of Pragmatismo.io. |
|
|
|
|
| The licensing of the program under the AGPLv3 does not imply a |
|
|
|
|
| trademark license. Therefore any rights, title and interest in |
|
|
|
|
| our trademarks remain entirely with us. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
import GBMarkdownPlayer from "./players/GBMarkdownPlayer.js";
|
|
|
|
import GBImagePlayer from "./players/GBImagePlayer.js";
|
|
|
|
import GBVideoPlayer from "./players/GBVideoPlayer.js";
|
2018-09-03 13:43:09 -03:00
|
|
|
import GBLoginPlayer from "./players/GBLoginPlayer.js";
|
2018-04-21 02:59:30 -03:00
|
|
|
import GBBulletPlayer from "./players/GBBulletPlayer.js";
|
|
|
|
import SidebarMenu from "./components/SidebarMenu.js";
|
|
|
|
import GBCss from "./components/GBCss.js";
|
|
|
|
import { DirectLine } from "botframework-directlinejs";
|
|
|
|
import { ConnectionStatus } from "botframework-directlinejs";
|
2018-06-01 16:11:52 -03:00
|
|
|
import { SpeechRecognizer } from "botframework-webchat/CognitiveServices";
|
|
|
|
import { SpeechSynthesizer } from "botframework-webchat/CognitiveServices";
|
|
|
|
import { SynthesisGender } from "botframework-webchat/CognitiveServices";
|
2018-04-21 02:59:30 -03:00
|
|
|
import { Chat } from "botframework-webchat";
|
|
|
|
import GBPowerBIPlayer from "./players/GBPowerBIPlayer.js";
|
2018-09-16 17:00:17 -03:00
|
|
|
import { UserAgentApplication } from "msal";
|
2018-04-21 02:59:30 -03:00
|
|
|
|
|
|
|
class GBUIApp extends React.Component {
|
2018-09-16 18:17:28 -03:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.state = {
|
2018-10-11 10:53:22 -03:00
|
|
|
line: null,
|
2018-09-16 18:17:28 -03:00
|
|
|
instance: null,
|
|
|
|
token: null,
|
|
|
|
instanceClient: null
|
|
|
|
};
|
2018-09-20 12:35:47 -03:00
|
|
|
window.user = this.getUser()
|
2018-09-16 17:00:17 -03:00
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
sendToken(token) {
|
2018-09-20 12:35:47 -03:00
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
setTimeout(() => {
|
2018-10-11 10:53:22 -03:00
|
|
|
window.line
|
2018-09-16 18:17:28 -03:00
|
|
|
.postActivity({
|
|
|
|
type: "event",
|
|
|
|
name: "updateToken",
|
|
|
|
data: token,
|
|
|
|
locale: "en-us",
|
|
|
|
textFormat: "plain",
|
|
|
|
timestamp: new Date().toISOString(),
|
2018-09-20 12:35:47 -03:00
|
|
|
from: this.getUser()
|
2018-09-16 18:17:28 -03:00
|
|
|
})
|
|
|
|
.subscribe(() => {
|
|
|
|
window.userAgentApplication.logout();
|
|
|
|
console.log("updateToken done")
|
|
|
|
});
|
|
|
|
}, 400);
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
send(command) {
|
2018-10-11 10:53:22 -03:00
|
|
|
window.line
|
2018-09-16 18:17:28 -03:00
|
|
|
.postActivity({
|
|
|
|
type: "event",
|
|
|
|
name: command,
|
|
|
|
locale: "en-us",
|
|
|
|
textFormat: "plain",
|
|
|
|
timestamp: new Date().toISOString(),
|
2018-09-20 12:35:47 -03:00
|
|
|
from: this.getUser()
|
2018-09-16 18:17:28 -03:00
|
|
|
})
|
|
|
|
.subscribe(console.log("EVENT SENT TO Guaribas."));
|
|
|
|
}
|
2018-09-20 12:35:47 -03:00
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
getUser() {
|
|
|
|
return { id: "webUser@gb", name: "You" };
|
2018-09-16 17:00:17 -03:00
|
|
|
}
|
2018-06-01 16:11:52 -03:00
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
postEvent(name, value) {
|
2018-10-11 10:53:22 -03:00
|
|
|
window.line.postActivity({
|
2018-09-16 18:17:28 -03:00
|
|
|
type: "event",
|
|
|
|
value: value,
|
|
|
|
from: this.getUser(),
|
|
|
|
name: name
|
|
|
|
});
|
|
|
|
}
|
2018-06-01 16:11:52 -03:00
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
postMessage(value) {
|
2018-10-11 10:53:22 -03:00
|
|
|
window.line.postActivity({
|
2018-09-16 18:17:28 -03:00
|
|
|
type: "message",
|
|
|
|
text: value,
|
|
|
|
from: this.getUser()
|
|
|
|
});
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
configureChat() {
|
|
|
|
var botId = window.location.href.split("/")[3];
|
|
|
|
if (botId.indexOf('#') != -1) {
|
|
|
|
botId = botId.split("#")[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!botId || botId == "") {
|
|
|
|
botId = "[default]";
|
|
|
|
}
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
fetch("/instances/" + botId)
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(
|
|
|
|
result => {
|
|
|
|
this.setState({ instanceClient: result });
|
|
|
|
this.setupBotConnection();
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.setState({
|
|
|
|
isLoaded: false,
|
|
|
|
err: error
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-06-01 16:11:52 -03:00
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
authenticate() {
|
|
|
|
let _this_ = this;
|
|
|
|
let authority =
|
|
|
|
"https://login.microsoftonline.com/" +
|
|
|
|
this.state.instanceClient.authenticatorTenant;
|
|
|
|
|
|
|
|
let graphScopes = ["Directory.AccessAsUser.All"];
|
|
|
|
|
|
|
|
let userAgentApplication = new UserAgentApplication(
|
2018-09-24 11:04:36 -03:00
|
|
|
this.state.instanceClient.authenticatorClientId,
|
2018-09-16 18:17:28 -03:00
|
|
|
authority,
|
|
|
|
function(errorDesc, token, error, tokenType) {
|
|
|
|
if (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
window.userAgentApplication = userAgentApplication;
|
|
|
|
|
|
|
|
if (!userAgentApplication.isCallback(window.location.hash) && window.parent === window && !window.opener) {
|
|
|
|
var user = userAgentApplication.getUser();
|
|
|
|
if (user) {
|
|
|
|
userAgentApplication.acquireTokenSilent(graphScopes).then(function(accessToken) {
|
|
|
|
_this_.sendToken(accessToken);
|
|
|
|
}, function(error) {
|
|
|
|
console.log(error);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-16 17:00:17 -03:00
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
setupBotConnection() {
|
|
|
|
let _this_ = this;
|
|
|
|
window["botchatDebug"] = true;
|
2018-06-01 16:11:52 -03:00
|
|
|
|
2018-10-11 10:53:22 -03:00
|
|
|
const line = new DirectLine({
|
2018-09-16 18:17:28 -03:00
|
|
|
secret: this.state.instanceClient.secret
|
2018-06-01 16:11:52 -03:00
|
|
|
});
|
|
|
|
|
2018-10-11 10:53:22 -03:00
|
|
|
line.connectionStatus$.subscribe(connectionStatus => {
|
2018-09-16 18:17:28 -03:00
|
|
|
if (connectionStatus === ConnectionStatus.Online) {
|
2018-10-11 10:53:22 -03:00
|
|
|
_this_.setState({ line: line });
|
|
|
|
line.postActivity({
|
2018-09-16 18:17:28 -03:00
|
|
|
type: "event",
|
|
|
|
value: "startGB",
|
|
|
|
from: this.getUser(),
|
|
|
|
name: "startGB"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-06-01 16:11:52 -03:00
|
|
|
|
2018-10-11 10:53:22 -03:00
|
|
|
window.line = line;
|
2018-09-16 18:17:28 -03:00
|
|
|
this.postEvent("startGB", true);
|
|
|
|
|
2018-10-11 10:53:22 -03:00
|
|
|
line.activity$
|
2018-09-16 18:17:28 -03:00
|
|
|
.filter(
|
|
|
|
activity =>
|
|
|
|
activity.type === "event" && activity.name === "loadInstance"
|
|
|
|
)
|
|
|
|
.subscribe(activity => {
|
|
|
|
_this_.setState({ instance: activity.value });
|
|
|
|
_this_.authenticate()
|
|
|
|
});
|
|
|
|
|
2018-10-11 10:53:22 -03:00
|
|
|
line.activity$
|
2018-09-16 18:17:28 -03:00
|
|
|
.filter(activity => activity.type === "event" && activity.name === "stop")
|
|
|
|
.subscribe(activity => {
|
|
|
|
if (_this_.player) {
|
|
|
|
_this_.player.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-11 10:53:22 -03:00
|
|
|
line.activity$
|
2018-09-16 18:17:28 -03:00
|
|
|
.filter(activity => activity.type === "event" && activity.name === "play")
|
|
|
|
.subscribe(activity => {
|
|
|
|
_this_.setState({ playerType: activity.value.playerType });
|
|
|
|
_this_.player.play(activity.value.data);
|
|
|
|
});
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
componentDidMount() {
|
|
|
|
this.configureChat();
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2018-09-16 18:17:28 -03:00
|
|
|
render() {
|
|
|
|
|
|
|
|
|
|
|
|
let playerComponent = "";
|
|
|
|
|
|
|
|
if (this.state.playerType) {
|
|
|
|
switch (this.state.playerType) {
|
|
|
|
case "markdown":
|
|
|
|
playerComponent = (
|
|
|
|
<GBMarkdownPlayer
|
|
|
|
app={this}
|
|
|
|
ref={player => {
|
|
|
|
this.player = player;
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "bullet":
|
|
|
|
playerComponent = (
|
|
|
|
<GBBulletPlayer
|
|
|
|
app={this}
|
|
|
|
ref={player => {
|
|
|
|
this.player = player;
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "video":
|
|
|
|
playerComponent = (
|
|
|
|
<GBVideoPlayer
|
|
|
|
app={this}
|
|
|
|
ref={player => {
|
|
|
|
this.player = player;
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "image":
|
|
|
|
playerComponent = (
|
|
|
|
<GBImagePlayer
|
|
|
|
app={this}
|
|
|
|
ref={player => {
|
|
|
|
this.player = player;
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "pbi":
|
|
|
|
playerComponent = (
|
|
|
|
<GBPowerBIPlayer
|
|
|
|
app={this}
|
|
|
|
ref={player => {
|
|
|
|
this.player = player;
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "login":
|
|
|
|
playerComponent = (
|
|
|
|
<GBLoginPlayer
|
|
|
|
app={this}
|
|
|
|
ref={player => {
|
|
|
|
this.player = player;
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log(
|
|
|
|
"GBERROR: Unknow player type specified on message from server."
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let speechOptions;
|
|
|
|
let chat = <div />;
|
|
|
|
let gbCss = <div />;
|
|
|
|
|
|
|
|
|
|
|
|
let sideBar = (
|
|
|
|
<div className="sidebar">
|
|
|
|
<SidebarMenu chat={this.chat} instance={this.state.instance} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2018-10-11 10:53:22 -03:00
|
|
|
if (this.state.line && this.state.instance) {
|
2018-09-16 18:17:28 -03:00
|
|
|
let token = this.state.instanceClient.speechToken;
|
|
|
|
gbCss = <GBCss instance={this.state.instance} />;
|
|
|
|
|
|
|
|
function getToken() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve(token);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
speechOptions = {
|
|
|
|
speechRecognizer: new SpeechRecognizer({
|
|
|
|
locale: "pt-br",
|
|
|
|
fetchCallback: (authFetchEventId) => getToken(),
|
|
|
|
fetchOnExpiryCallback: (authFetchEventId) => getToken()
|
|
|
|
}),
|
|
|
|
speechSynthesizer: new SpeechSynthesizer({
|
|
|
|
fetchCallback: (authFetchEventId) => getToken(),
|
|
|
|
fetchOnExpiryCallback: (authFetchEventId) => getToken(),
|
|
|
|
gender: SynthesisGender.Male,
|
|
|
|
voiceName: 'Microsoft Server Speech Text to Speech Voice (pt-BR, Daniel, Apollo)'
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
chat = (
|
|
|
|
<Chat
|
|
|
|
ref={chat => {
|
|
|
|
this.chat = chat;
|
|
|
|
}}
|
|
|
|
locale={'pt-br'}
|
2018-10-11 10:53:22 -03:00
|
|
|
line={this.state.line}
|
2018-09-16 18:17:28 -03:00
|
|
|
user={this.getUser()}
|
|
|
|
bot={{ id: "bot@gb", name: "Bot" }}
|
|
|
|
speechOptions={speechOptions}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.state.instance) {
|
|
|
|
sideBar = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{gbCss}
|
|
|
|
{sideBar}
|
|
|
|
<div className="player">{playerComponent}</div>
|
|
|
|
{chat}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default GBUIApp;
|