From 8c68332802380ae231be6704ee67903e42644c72 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Mon, 15 Jun 2020 00:40:25 -0300 Subject: [PATCH] fix(core.gbapp): Faster bot creation, fix on WhatsApp switcher. --- .../services/AzureDeployerService.ts | 36 ++++---- .../services/GBConversationalService.ts | 2 +- packages/core.gbapp/services/GBDeployer.ts | 3 - packages/core.gbapp/services/GBMinService.ts | 84 +++++++++---------- .../services/WhatsappDirectLine.ts | 12 +-- 5 files changed, 66 insertions(+), 71 deletions(-) diff --git a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts index df3729f02..dab345022 100644 --- a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts +++ b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts @@ -522,7 +522,7 @@ export class AzureDeployerService implements IGBInstallationDeployer { msaAppId: appId, msaAppPassword: appPassword, enabledChannels: ['webchat', "skype"],//, "facebook"], - configuredChannels: ['webchat' , "skype"]//, "facebook"] + configuredChannels: ['webchat', "skype"]//, "facebook"] } }; @@ -539,23 +539,23 @@ export class AzureDeployerService implements IGBInstallationDeployer { return; } - setTimeout(async () => { - try { - //tslint:disable-next-line:max-line-length - query = `subscriptions/${subscriptionId}/resourceGroups/${group}/providers/Microsoft.BotService/botServices/${botId}/channels/WebChatChannel/listChannelWithKeys?api-version=${ - this.apiVersion - }`; - url = urlJoin(baseUrl, query); - req = AzureDeployerService.createRequestObject(url, accessToken, 'POST', JSON.stringify(parameters)); - const resChannel = await httpClient.sendRequest(req); - const key = JSON.parse(resChannel.bodyAsText).properties.properties.sites[0].key; - instance.webchatKey = key; - instance.whatsappBotKey = key; - resolve(instance); - } catch (error) { - reject(error); - } - }, 60000); + + try { + //tslint:disable-next-line:max-line-length + query = `subscriptions/${subscriptionId}/resourceGroups/${group}/providers/Microsoft.BotService/botServices/${botId}/channels/WebChatChannel/listChannelWithKeys?api-version=${ + this.apiVersion + }`; + url = urlJoin(baseUrl, query); + req = AzureDeployerService.createRequestObject(url, accessToken, 'POST', JSON.stringify(parameters)); + const resChannel = await httpClient.sendRequest(req); + const key = JSON.parse(resChannel.bodyAsText).properties.properties.sites[0].key; + instance.webchatKey = key; + instance.whatsappBotKey = key; + resolve(instance); + } catch (error) { + reject(error); + } + }); } diff --git a/packages/core.gbapp/services/GBConversationalService.ts b/packages/core.gbapp/services/GBConversationalService.ts index 4211954b4..1a720bb47 100644 --- a/packages/core.gbapp/services/GBConversationalService.ts +++ b/packages/core.gbapp/services/GBConversationalService.ts @@ -459,7 +459,7 @@ export class GBConversationalService { public async routeNLP(step: GBDialogStep, min: GBMinInstance, text: string): Promise { - if (min.instance.nlpAppId === null) { + if (min.instance.nlpAppId === null || min.instance.nlpAppId === undefined) { return false; } diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 1bf00cd97..01df266b8 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -170,7 +170,6 @@ export class GBDeployer implements IGBDeployer { instance.nlpScore = 0.80; // TODO: Migrate to Excel Config.xlsx. instance.searchScore = 0.45; instance.whatsappServiceKey = bootInstance.whatsappServiceKey; - instance.whatsappBotKey = bootInstance.whatsappBotKey; instance.whatsappServiceNumber = bootInstance.whatsappServiceNumber; instance.whatsappServiceUrl = bootInstance.whatsappServiceUrl; @@ -215,14 +214,12 @@ export class GBDeployer implements IGBDeployer { instance.searchIndexer = bootInstance.searchIndexer; instance.searchKey = bootInstance.searchKey; instance.whatsappServiceKey = bootInstance.whatsappServiceKey; - instance.whatsappBotKey = bootInstance.whatsappBotKey; instance.whatsappServiceNumber = bootInstance.whatsappServiceNumber; instance.whatsappServiceUrl = bootInstance.whatsappServiceUrl; instance.storageServer = bootInstance.storageServer; instance.storageName = bootInstance.storageName; instance.storageUsername = bootInstance.storageUsername; instance.storagePassword = bootInstance.storagePassword; - instance.webchatKey = bootInstance.webchatKey; instance.cloudLocation = bootInstance.cloudLocation; instance.speechEndpoint = bootInstance.speechEndpoint; instance.speechKey = bootInstance.speechKey; diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index d6b20dde4..7f957be5c 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -461,7 +461,7 @@ export class GBMinService { min.adminService = this.adminService; min.deployService = this.deployer; min.kbService = new KBService(this.core.sequelize); - min.instance = await this.core.loadInstanceByBotId(min.botId); + min.instance = instance; min.cbMap = {}; min.scriptMap = {}; min.sandBoxMap = {}; @@ -487,7 +487,7 @@ export class GBMinService { new WhatsappDirectLine( min, min.botId, - min.instance.webchatKey, + min.instance.whatsappBotKey, minBoot.instance.whatsappServiceKey, minBoot.instance.whatsappServiceNumber, minBoot.instance.whatsappServiceUrl @@ -690,54 +690,52 @@ export class GBMinService { await step.beginDialog('/menu', JSON.parse(context.activity.text)); // Otherwise, continue to the active dialog in the stack. } else { + if (!await this.deployer.getStoragePackageByName(min.instance.instanceId, `${min.instance.botId}.gbkb`)) { + await step.context.sendActivity(`Oi, ainda não possuo pacotes de conhecimento publicados. Por favor, aguarde alguns segundos enquanto eu auto-publico alguns pacotes.`); + return await step.beginDialog('/publish', { confirm: true }); + } + if (step.activeDialog !== undefined) { await step.continueDialog(); } else { - if (!await this.deployer.getStoragePackageByName(min.instance.instanceId, `${min.instance.botId}.gbkb`)) { - await step.context.sendActivity(`Oi, ainda não possuo pacotes de conhecimento publicados. Por favor, aguarde alguns segundos enquanto eu auto-publico alguns pacotes.`); - return await step.beginDialog('/publish', { confirm: true }); - } - else { - - let query = context.activity.text; + let query = context.activity.text; - const translatorEnabled = () => { - if (min.instance.params) { - const params = JSON.parse(min.instance.params); - return params?params['Enable Worldwide Translator'] === "TRUE": false; - } - return false; - } // TODO: Encapsulate. - - let locale = 'pt'; - if (process.env.TRANSLATOR_DISABLED !== "true" || translatorEnabled()) { - const minBoot = GBServer.globals.minBoot as any; // TODO: Switch keys automatically to master/per bot. - locale = await AzureText.getLocale(minBoot.instance.textAnalyticsKey ? - minBoot.instance.textAnalyticsKey : minBoot.instance.textAnalyticsKey, - minBoot.instance.textAnalyticsEndpoint ? - minBoot.instance.textAnalyticsEndpoint : minBoot.instance.textAnalyticsEndpoint, query); + const translatorEnabled = () => { + if (min.instance.params) { + const params = JSON.parse(min.instance.params); + return params ? params['Enable Worldwide Translator'] === "TRUE" : false; } - - let sec = new SecService(); - const member = step.context.activity.from; - - const user = await sec.ensureUser(min.instance.instanceId, member.id, - member.name, "", "web", member.name); - user.locale = locale; - await user.save(); - const minBoot = GBServer.globals.minBoot as any; - query = await min.conversationalService.translate(min, - min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey, - min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint, - query, - 'pt'); - GBLog.info(`Translated text: ${query}.`) - - await step.beginDialog('/answer', { - query: query - }); + return false; + } // TODO: Encapsulate. + + let locale = 'pt'; + if (process.env.TRANSLATOR_DISABLED !== "true" || translatorEnabled()) { + const minBoot = GBServer.globals.minBoot as any; // TODO: Switch keys automatically to master/per bot. + locale = await AzureText.getLocale(minBoot.instance.textAnalyticsKey ? + minBoot.instance.textAnalyticsKey : minBoot.instance.textAnalyticsKey, + minBoot.instance.textAnalyticsEndpoint ? + minBoot.instance.textAnalyticsEndpoint : minBoot.instance.textAnalyticsEndpoint, query); } + + let sec = new SecService(); + const member = step.context.activity.from; + + const user = await sec.ensureUser(min.instance.instanceId, member.id, + member.name, "", "web", member.name); + user.locale = locale; + await user.save(); + const minBoot = GBServer.globals.minBoot as any; + query = await min.conversationalService.translate(min, + min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey, + min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint, + query, + 'pt'); + GBLog.info(`Translated text: ${query}.`) + + await step.beginDialog('/answer', { + query: query + }); } } } diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 36cd8cedd..bd0e5426b 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -58,7 +58,7 @@ export class WhatsappDirectLine extends GBService { private directLineSecret: string; private locale: string = 'pt-BR'; - public conversationIds = {}; + static conversationIds = {}; min: GBMinInstance; constructor( @@ -130,7 +130,7 @@ export class WhatsappDirectLine extends GBService { } public async resetConversationId(number) { - this.conversationIds[number] = undefined; + WhatsappDirectLine.conversationIds[number] = undefined; } public async check() { @@ -200,7 +200,7 @@ export class WhatsappDirectLine extends GBService { } } - const conversationId = this.conversationIds[from]; + const conversationId = WhatsappDirectLine.conversationIds[from]; let client = await this.directLineClient; if (user.agentMode === "self") { @@ -254,12 +254,12 @@ export class WhatsappDirectLine extends GBService { } else if (user.agentMode === "bot" || user.agentMode === null) { - if (this.conversationIds[from] === undefined) { + if (WhatsappDirectLine.conversationIds[from] === undefined) { GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`); - const response = await client.Conversations.Conversations_StartConversation() + const response = await client.Conversations.Conversations_StartConversation(); const generatedConversationId = response.obj.conversationId; - this.conversationIds[from] = generatedConversationId; + WhatsappDirectLine.conversationIds[from] = generatedConversationId; this.pollMessages(client, generatedConversationId, from, fromName); this.inputMessage(client, generatedConversationId, text, from, fromName);