From 3118b455433cd876e106e3e28f849f04662e5e5f Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Tue, 28 Aug 2018 17:50:19 -0300 Subject: [PATCH] Migration to V4 is in progress. The Bot now can be loaded again. --- .../services/GBConversationalService.ts | 2 +- deploy/core.gbapp/services/GBMinService.ts | 34 ++- deploy/default.gbui/public/index.html | 32 ++- deploy/default.gbui/src/GBUIApp.js | 14 +- deploy/kb.gbapp/dialogs/AskDialog.ts | 246 +++++++++--------- tsconfig.json | 2 + 6 files changed, 171 insertions(+), 159 deletions(-) diff --git a/deploy/core.gbapp/services/GBConversationalService.ts b/deploy/core.gbapp/services/GBConversationalService.ts index 6c7b2b19..79fba1b9 100644 --- a/deploy/core.gbapp/services/GBConversationalService.ts +++ b/deploy/core.gbapp/services/GBConversationalService.ts @@ -58,7 +58,7 @@ export class GBConversationalService implements IGBConversationalService { msg.value = value; msg.type = "event"; msg.name = name; - // TODO: dc.context.sendActivity(msg); + dc.context.sendActivity(msg); } async runNLP( diff --git a/deploy/core.gbapp/services/GBMinService.ts b/deploy/core.gbapp/services/GBMinService.ts index f60f90de..8d4da7b5 100644 --- a/deploy/core.gbapp/services/GBMinService.ts +++ b/deploy/core.gbapp/services/GBMinService.ts @@ -134,9 +134,11 @@ export class GBMinService { (instance: IGBInstance, err) => { if (instance) { + // TODO: Make dynamic: https://CHANGE.api.cognitive.microsoft.com/sts/v1.0 + let options = { url: - "https://api.cognitive.microsoft.com/sts/v1.0/issueToken", + "https://westus.api.cognitive.microsoft.com/sts/v1.0/issueToken", method: "POST", headers: { "Ocp-Apim-Subscription-Key": instance.speechKey @@ -155,6 +157,10 @@ export class GBMinService { conversationId: responseObject.conversationId }) ); + }).catch((reason) => { + let error = `Error loading Speech Service: ${reason}.`; + res.send(error); + logger.error(error); }); } else { let error = `Instance not found: ${botId}.`; @@ -223,6 +229,7 @@ export class GBMinService { logger.trace( `GeneralBots(${instance.engineName}) listening on: ${url}.` ); + server.post(`/api/messages/${instance.botId}`, (req, res) => { adapter.processActivity(req, res, async (context) => { @@ -230,6 +237,18 @@ export class GBMinService { const state = conversationState.get(context); const dc = min.dialogs.createContext(context, state); + const user = min.userState.get(dc.context); + if (!user.loaded) { + min.conversationalService.sendEvent( + dc, + "loadInstance", + min.instance // TODO: Send just necessary values. + ); + + user.loaded = true; + user.subjects = []; + } + if (context.activity.type === "conversationUpdate" && context.activity.membersAdded.length > 0) { @@ -242,7 +261,7 @@ export class GBMinService { if (!context.responded) { await dc.begin('/'); - }else if (context.activity.name === "whoAmI") { + } else if (context.activity.name === "whoAmI") { dc.begin("/whoAmI"); } else if (context.activity.name === "showSubjects") { dc.begin("/menu"); @@ -265,17 +284,6 @@ export class GBMinService { await dc.continue(); } - const user = min.userState.get(dc.context); - if (!user.loaded) { - // min.conversationalService.sendEvent( - // dc, - // "loadInstance", - // min.instance // TODO: Send a new thiner object. - // ); - - user.loaded = true; - user.subjects = []; - } appPackages.forEach(e => { e.onNewSession(min, dc); diff --git a/deploy/default.gbui/public/index.html b/deploy/default.gbui/public/index.html index 6d13aa1b..bdcdb980 100644 --- a/deploy/default.gbui/public/index.html +++ b/deploy/default.gbui/public/index.html @@ -34,27 +34,25 @@ - - - - - - - - General Bots Community Edition | pragmatismo.io + + + + + + + + General Bots Community Edition | pragmatismo.io - + -
+
- \ No newline at end of file diff --git a/deploy/default.gbui/src/GBUIApp.js b/deploy/default.gbui/src/GBUIApp.js index b12c5264..a51a0cb0 100644 --- a/deploy/default.gbui/src/GBUIApp.js +++ b/deploy/default.gbui/src/GBUIApp.js @@ -145,7 +145,7 @@ class GBUIApp extends React.Component { .subscribe(activity => { _this_.setState({ instance: activity.value }); }); - + botConnection.activity$ .filter(activity => activity.type === "event" && activity.name === "stop") .subscribe(activity => { @@ -167,7 +167,7 @@ class GBUIApp extends React.Component { } render() { - let chat =
; + let playerComponent = ""; @@ -234,16 +234,19 @@ class GBUIApp extends React.Component { } let speechOptions; + let chat =
; + let gbCss =
; + let sideBar = (
); - - + if (this.state.botConnection && this.state.instance) { let token = this.state.instanceClient.speechToken; + gbCss = ; function getToken() { return new Promise((resolve, reject) => { @@ -251,7 +254,6 @@ class GBUIApp extends React.Component { }); } - speechOptions = { speechRecognizer: new SpeechRecognizer({ locale: "pt-br", @@ -288,7 +290,7 @@ class GBUIApp extends React.Component { return (
- + {gbCss} {sideBar}
{playerComponent}
{chat} diff --git a/deploy/kb.gbapp/dialogs/AskDialog.ts b/deploy/kb.gbapp/dialogs/AskDialog.ts index bc0554c4..f4c1cfc7 100644 --- a/deploy/kb.gbapp/dialogs/AskDialog.ts +++ b/deploy/kb.gbapp/dialogs/AskDialog.ts @@ -50,15 +50,19 @@ export class AskDialog extends IGBDialog { appId: min.instance.nlpAppId, subscriptionKey: min.instance.nlpSubscriptionKey, serviceEndpoint: min.instance.nlpServerUrl - }); + }); min.dialogs.add("/answer", [ async (dc, args) => { - const user = min.userState.get(dc.context); + // Initialize values. + + const user = min.userState.get(dc.context); let text = ""; + // Handle extra text from FAQ. + if (args && args.query) { text = args.query; } else if (args && args.fromFaq) { @@ -71,132 +75,130 @@ export class AskDialog extends IGBDialog { dc.context.sendActivity(messages[0]); // TODO: Handle rnd. } - await model.recognize(dc.context).then(res => { - console.log(res); - }).catch(err => { - console.log(err); - }); + // Spells check the input text before sending Search or NLP. - // await min.conversationalService.runNLP( - // dc, - // min, + // DISABLED: + // AzureText.getSpelledText( + // min.instance.spellcheckerKey, // text, - // (data, error) => { - - // if (!data) { - // let messages = [ - // "Desculpe-me, não encontrei nada a respeito.", - // "Lamento... Não encontrei nada sobre isso. Vamos tentar novamente?", - // "Desculpe-me, não achei nada parecido. Poderia tentar escrever de outra forma?" - // ]; - - // dc.context.sendActivity(messages[0]); // TODO: Handle rnd. - // dc.replace("/ask", { isReturning: true }); - // } - // } - // ); - - - // if (text === "") { - // dc.replace("/ask"); - // } else { - // // AzureText.getSpelledText( - // // min.instance.spellcheckerKey, - // // text, - // // async (data, err) => { - // var data = text; - // if (data != text) { - // logger.trace("Spelled Text: " + data); - // text = data; - // } - // user.lastQuestion = data; - - // service.ask( - // min.instance, - // text, - // min.instance.searchScore, - // user.subjects, - // async resultsA => { - // min.conversationalService.sendEvent(dc, "stop", null); - - // if (resultsA && resultsA.answer) { - // user.isAsking = false; - // service.sendAnswer(min.conversationalService, - // dc, - // resultsA.answer - // ); - // user.lastQuestionId = resultsA.questionId; - - // dc.replace("/ask", { isReturning: true }); - // } else { - // //if (min.isAsking) { - // // Second time with no filter. - - // service.ask( - // min.instance, - // text, - // min.instance.searchScore, - // null, - // async resultsB => { - // if (resultsB && resultsB.answer) { - // const user = min.userState.get(dc.context); - - // user.isAsking = false; - - // if (user.subjects.length > 0) { - // let subjectText = - // `${KBService.getSubjectItemsSeparatedBySpaces( - // user.subjects - // )}`; - - // let messages = [ - // `Respondendo nao apenas sobre ${subjectText}... `, - // `Respondendo de modo mais abrangente...`, - // `Vou te responder de modo mais abrangente... - // Não apenas sobre ${subjectText}` - // ]; - // dc.context.sendActivity(messages[0]); // TODO: Handle rnd. - // } - // user.isAsking = false; - // service.sendAnswer(min.conversationalService, - // dc, - // resultsB.answer - // ); - // dc.replace("/ask", { isReturning: true }); - - // user.lastQuestionId = resultsB.questionId; - // } else { - - // await min.conversationalService.runNLP( - // dc, - // min, - // text, - // (data, error) => { - - // if (!data) { - // let messages = [ - // "Desculpe-me, não encontrei nada a respeito.", - // "Lamento... Não encontrei nada sobre isso. Vamos tentar novamente?", - // "Desculpe-me, não achei nada parecido. Poderia tentar escrever de outra forma?" - // ]; - - // dc.context.sendActivity(messages[0]); // TODO: Handle rnd. - // dc.replace("/ask", { isReturning: true }); - // } - // } - // ); - // } - // } - // ); - // } - // } - // ); + // async (data, err) => { + // var data = res.text; + // if (data != text) { + // logger.trace("Spelled Text: " + data); + // text = data; // } - //); } + + user.lastQuestion = text; + + // Searches KB for the first time. + + service.ask( + min.instance, + text, + min.instance.searchScore, + user.subjects, + async resultsA => { + + // Stops any content on projector. + + min.conversationalService.sendEvent(dc, "stop", null); + + // If there is some result, answer immediately. + + if (resultsA && resultsA.answer) { + + // Saves some context info. + + user.isAsking = false; + user.lastQuestionId = resultsA.questionId; + + // Sends the answer to all outputs, including projector. + + service.sendAnswer(min.conversationalService, + dc, + resultsA.answer + ); + + // Goes to ask loop, again. + + dc.replace("/ask", { isReturning: true }); + + } else { + + // Second time running Search, now with no filter. + + service.ask( + min.instance, + text, + min.instance.searchScore, + null, + async resultsB => { + + // If there is some result, answer immediately. + + if (resultsB && resultsB.answer) { + + // Saves some context info. + + const user = min.userState.get(dc.context); + user.isAsking = false; + user.lastQuestionId = resultsB.questionId; + + // Inform user that a broader search will be used. + + if (user.subjects.length > 0) { + let subjectText = + `${KBService.getSubjectItemsSeparatedBySpaces( + user.subjects + )}`; + let messages = [ + `Respondendo nao apenas sobre ${subjectText}... `, + `Respondendo de modo mais abrangente...`, + `Vou te responder de modo mais abrangente... + Não apenas sobre ${subjectText}` + ]; + dc.context.sendActivity(messages[0]); // TODO: Handle rnd. + } + + // Sends the answer to all outputs, including projector. + + service.sendAnswer(min.conversationalService, + dc, + resultsB.answer + ); + dc.replace("/ask", { isReturning: true }); + + + } else { + await min.conversationalService.runNLP( + dc, + min, + text, + (data, error) => { + + if (!data) { + let messages = [ + "Desculpe-me, não encontrei nada a respeito.", + "Lamento... Não encontrei nada sobre isso. Vamos tentar novamente?", + "Desculpe-me, não achei nada parecido. Poderia tentar escrever de outra forma?" + ]; + + dc.context.sendActivity(messages[0]); // TODO: Handle rnd. + dc.replace("/ask", { isReturning: true }); + } + }).catch(err => { + console.log(err); + }); + } + + }); + } + } + ); } ]); - bot + min.dialogs.add("/ask", [ async (dc, args) => { const user = min.userState.get(dc.context); diff --git a/tsconfig.json b/tsconfig.json index 67fd63f7..d583fed9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,8 @@ "declaration": false, "emitDecoratorMetadata": true, "experimentalDecorators": true, + "keyofStringsOnly": true, + "skipLibCheck": true, "mapRoot": "./dist/", "module": "commonjs", "moduleResolution": "node",