From c560ac9e6ad03a211ee518768ae3ea09401b51f8 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 2 Sep 2023 20:09:55 -0300 Subject: [PATCH 01/68] fix(azuredeployer.gbapp): Fix in bot booting. --- package.json | 2 +- packages/core.gbapp/services/GBDeployer.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index f8a1cb21c..72f7776a0 100644 --- a/package.json +++ b/package.json @@ -180,7 +180,7 @@ "vm2-process": "2.1.1", "walk-promise": "0.2.0", "washyourmouthoutwithsoap": "1.0.2", - "whatsapp-web.js": "git://github.com/pedroslopez/whatsapp-web.js#abac063b779570729476cf42e29dc694e5345ca6", + "whatsapp-web.js": "git://github.com/pedroslopez/whatsapp-web.js#b671b0c708f0bc6187ccec078a0f3e9c08db4bce", "winston": "3.8.2", "winston-logs-display": "1.0.0", "ws": "8.12.1", diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 431a2f9cc..87bacd67d 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -280,7 +280,6 @@ export class GBDeployer implements IGBDeployer { `${publicAddress}/api/messages/${instance.botId}` ); } else { - const botId = GBConfigService.get('BOT_ID'); // Internally create resources on cloud provider. From 9e498f85a3cf5f8d399a6082e2c669a8ed41dd10 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 2 Sep 2023 21:04:46 -0300 Subject: [PATCH 02/68] fix(all): Updating ngrok. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 72f7776a0..bd8c8c708 100644 --- a/package.json +++ b/package.json @@ -128,7 +128,7 @@ "moment": "1.3.0", "ms-rest-azure": "3.0.0", "nexmo": "2.9.1", - "ngrok": "4.3.3", + "ngrok": "5.0.0-beta.2", "node-cron": "3.0.2", "node-html-parser": "6.1.5", "node-nlp": "4.26.1", From 7cc85a317e30505566cb8a67fc82ebee4c8d9ca8 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 3 Sep 2023 12:40:25 -0300 Subject: [PATCH 03/68] fix(all): Updating ngrok. --- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index a791686cc..ee0b33103 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -1026,6 +1026,7 @@ export class WhatsappDirectLine extends GBService { const botNumber = urlMin ? urlMin.core.getParam(urlMin.instance, 'Bot Number', null) : null; if (botNumber) { + GBLog.info(`${user.userSystemId} user changed Bot to: ${botId}.`); user = await sec.updateUserInstance(user.userSystemId, urlMin.instance.instanceId); } let activeMin; From b60ebbf7baa3eba3eb5b057e5ec031b3db233f20 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 3 Sep 2023 14:48:05 -0300 Subject: [PATCH 04/68] fix(whatsapp.gblib): Fix in bot switching. --- packages/core.gbapp/services/GBMinService.ts | 25 +++++++++++-------- .../services/WhatsappDirectLine.ts | 20 ++++++++++++++- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 2b0133622..b4e5f9602 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -769,12 +769,12 @@ export class GBMinService { } const group = min.core.getParam(min.instance, 'WhatsApp Group ID', null); - + WhatsappDirectLine.botGroups[min.botId] = group; - + // If there is WhatsApp configuration specified, initialize // infrastructure objects. - + if (min.instance.whatsappServiceKey) { min.whatsAppDirectLine = new WhatsappDirectLine( min, @@ -798,16 +798,21 @@ export class GBMinService { minBoot.instance.whatsappServiceNumber, minBoot.instance.whatsappServiceUrl, group - ); - await min.whatsAppDirectLine.setup(false); + ); + await min.whatsAppDirectLine.setup(false); + } } - } - - // Setups default BOT Framework dialogs. - + + const botNumber = min.core.getParam(min.instance, 'Bot Number', null); + if (botNumber){ + WhatsappDirectLine.botsByNumber[botNumber] = min.whatsAppDirectLine; + } + + // Setups default BOT Framework dialogs. + min.userProfile = conversationState.createProperty('userProfile'); const dialogState = conversationState.createProperty('dialogState'); - + min.dialogs = new DialogSet(dialogState); min.dialogs.add(new TextPrompt('textPrompt')); min.dialogs.add(new AttachmentPrompt('attachmentPrompt')); diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index ee0b33103..242a5c796 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -58,6 +58,7 @@ const { List, Buttons, Client, MessageMedia } = pkg; */ export class WhatsappDirectLine extends GBService { public static conversationIds = {}; + public static botsByNumber = {}; public static mobiles = {}; public static phones = {}; public static chatIds = {}; @@ -303,14 +304,31 @@ export class WhatsappDirectLine extends GBService { public async received(req, res) { const provider = WhatsappDirectLine.providerFromRequest(req); - let message, from, fromName, text: string; + let message, to, from, fromName, text: string; let group = ''; let answerText = null; let attachments = null; + switch (provider) { case 'GeneralBots': + message = req; + to = message.to.endsWith('@g.us') ? message.to.split('@')[0] : message.to.split('@')[0]; + const newThis= WhatsappDirectLine.botsByNumber[to]; + if (newThis === undefined){ + throw GBError.create(`Bot Number ${to} not setup for any loaded bot.`); + } + else + { + if (newThis.min.botId !== this.min.botId) + { + await newThis.received (req, res); + + return; + } + } + text = message.body; from = message.from.endsWith('@g.us') ? message.author.split('@')[0] : message.from.split('@')[0]; fromName = message._data.notifyName; From 1125016bd6b084f5489beb18951466bcf1632d34 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 3 Sep 2023 16:03:22 -0300 Subject: [PATCH 05/68] fix(whatsapp.gblib): Fix in bot switching. --- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 242a5c796..d6f985fca 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -122,7 +122,7 @@ export class WhatsappDirectLine extends GBService { const client = await new SwaggerClient({ spec: JSON.parse(Fs.readFileSync('directline-3.0.json', 'utf8')), requestInterceptor: req => { - req.headers['Authorization'] = `Bearer ${this.min.instance.webchatKey}`; + req.headers['Authorization'] = `Bearer ${this.min.instance.whatsappBotKey}`; } }); this.directLineClient = client; @@ -677,7 +677,7 @@ export class WhatsappDirectLine extends GBService { } }); } catch (e) { - GBLog.error(e); + GBLog .error(e); } } From c76b368dff14a01f23e764f91a1a29302765a5c7 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 3 Sep 2023 16:13:28 -0300 Subject: [PATCH 06/68] fix(whatsapp.gblib): Fix in bot switching. --- packages/core.gbapp/services/GBDeployer.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 87bacd67d..22c472232 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -108,7 +108,8 @@ export class GBDeployer implements IGBDeployer { if (min['cacheToken']) { return min['cacheToken']; } else { - token = await (min.adminService as any)['acquireElevatedToken'](min.instance.instanceId, true); + token = await (min.adminService as any)['acquireElevatedToken'] + (min.instance.instanceId, min.instance.authenticatorTenant?false:true); const siteId = process.env.STORAGE_SITE_ID; const libraryId = process.env.STORAGE_LIBRARY; From 74c7b64482ca488db6809d545eb30fb1ec588eca Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 3 Sep 2023 16:22:03 -0300 Subject: [PATCH 07/68] fix(whatsapp.gblib): Fix in bot switching. --- packages/core.gbapp/services/GBDeployer.ts | 4 ++++ packages/core.gbapp/services/GBImporterService.ts | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 22c472232..28c3feedf 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -108,6 +108,10 @@ export class GBDeployer implements IGBDeployer { if (min['cacheToken']) { return min['cacheToken']; } else { + + // Get token as root only if the bot does not have + // an custom tenant for retrieving packages. + token = await (min.adminService as any)['acquireElevatedToken'] (min.instance.instanceId, min.instance.authenticatorTenant?false:true); diff --git a/packages/core.gbapp/services/GBImporterService.ts b/packages/core.gbapp/services/GBImporterService.ts index 70122ef6a..d3bf55fdd 100644 --- a/packages/core.gbapp/services/GBImporterService.ts +++ b/packages/core.gbapp/services/GBImporterService.ts @@ -60,10 +60,18 @@ export class GBImporter { localPath: string, additionalInstance: IGBInstance = null ) { - const settingsJson = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'settings.json'), 'utf8')); - if (botId === undefined) { - botId = settingsJson.botId; + const file = urlJoin(localPath, 'settings.json'); + + let settingsJson = {botId: botId}; + if (Fs.existsSync(file)){ + + settingsJson = JSON.parse(Fs.readFileSync(file, 'utf8')); + if (botId === undefined) { + botId = settingsJson.botId; + } + } + let instance: IGBInstance; if (botId === undefined) { botId = GBConfigService.get('BOT_ID'); From 6c0a86f1b0c574811b0df7ee837a3458804f9afc Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 3 Sep 2023 16:25:22 -0300 Subject: [PATCH 08/68] fix(whatsapp.gblib): Fix in bot switching. --- packages/core.gbapp/services/GBImporterService.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/core.gbapp/services/GBImporterService.ts b/packages/core.gbapp/services/GBImporterService.ts index d3bf55fdd..54d7c9c0c 100644 --- a/packages/core.gbapp/services/GBImporterService.ts +++ b/packages/core.gbapp/services/GBImporterService.ts @@ -119,10 +119,8 @@ export class GBImporter { localPath: string, settingsJson: any ) { - const packageJson = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'package.json'), 'utf8')); - const servicesJson = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'services.json'), 'utf8')); - - const fullSettingsJson = { ...GBServer.globals.bootInstance, ...packageJson, ...settingsJson, ...servicesJson }; + + const fullSettingsJson = { ...GBServer.globals.bootInstance, ...settingsJson }; if (botId !== undefined) { fullSettingsJson.botId = botId; From 766d355f47db2cf31ad0deb9fa1dc61573ec54f1 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 3 Sep 2023 16:33:08 -0300 Subject: [PATCH 09/68] fix(whatsapp.gblib): Fix in bot switching. --- packages/core.gbapp/services/GBImporterService.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core.gbapp/services/GBImporterService.ts b/packages/core.gbapp/services/GBImporterService.ts index 54d7c9c0c..ab85ac134 100644 --- a/packages/core.gbapp/services/GBImporterService.ts +++ b/packages/core.gbapp/services/GBImporterService.ts @@ -120,7 +120,9 @@ export class GBImporter { settingsJson: any ) { - const fullSettingsJson = { ...GBServer.globals.bootInstance, ...settingsJson }; + const fullSettingsJson = { ...GBServer.globals.bootInstance, ...settingsJson, + description:"General Bot" + }; if (botId !== undefined) { fullSettingsJson.botId = botId; From 80713f284dff9354768e038c55639752286d1e6c Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 3 Sep 2023 16:34:16 -0300 Subject: [PATCH 10/68] fix(whatsapp.gblib): Fix in bot switching. --- packages/core.gbapp/services/GBImporterService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core.gbapp/services/GBImporterService.ts b/packages/core.gbapp/services/GBImporterService.ts index ab85ac134..c6166461e 100644 --- a/packages/core.gbapp/services/GBImporterService.ts +++ b/packages/core.gbapp/services/GBImporterService.ts @@ -121,7 +121,7 @@ export class GBImporter { ) { const fullSettingsJson = { ...GBServer.globals.bootInstance, ...settingsJson, - description:"General Bot" + description:"General Bot", title:botId }; if (botId !== undefined) { From 176063cb9566b6a5f0e9e2d394d152b7221e696c Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 3 Sep 2023 16:38:40 -0300 Subject: [PATCH 11/68] fix(whatsapp.gblib): Fix in bot switching. --- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index d6f985fca..c8ccb59c9 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -760,6 +760,7 @@ export class WhatsappDirectLine extends GBService { switch (this.provider) { case 'GeneralBots': const attachment = await MessageMedia.fromUrl(url); + to = to.replace('+', ''); if (to.indexOf('@') == -1) { if (to.length == 18) { to = to + '@g.us'; From 31e49523be24e9ada3064c73c85b9200d400df13 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 3 Sep 2023 16:46:40 -0300 Subject: [PATCH 12/68] fix(whatsapp.gblib): Fix in bot switching. --- packages/kb.gbapp/dialogs/AskDialog.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/kb.gbapp/dialogs/AskDialog.ts b/packages/kb.gbapp/dialogs/AskDialog.ts index 2aca29d39..25654fe3a 100644 --- a/packages/kb.gbapp/dialogs/AskDialog.ts +++ b/packages/kb.gbapp/dialogs/AskDialog.ts @@ -108,8 +108,14 @@ export class AskDialog extends IGBDialog { text = Messages[locale].ask_first_time; } else if (step.options && step.options.isReturning && !step.context.activity.group) { const askForMore = min.core.getParam(min.instance, 'Ask For More', null); + if (askForMore){ + text = askForMore ; + } + else + { - text = askForMore ? Messages[locale].anything_else : ''; + return await step.endDialog(null); + } } else if (step.context.activity.group || (step.options && step.options.emptyPrompt)) { return await step.next(); } else if (user.subjects.length > 0) { From ed374c26c8a67ac9f0f3959cce9741b2bb30f316 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Mon, 4 Sep 2023 16:10:08 -0300 Subject: [PATCH 13/68] fix(all): Fix in bot proxy outdated columns. --- packages/admin.gbapp/dialogs/AdminDialog.ts | 2 +- .../azuredeployer.gbapp/services/AzureDeployerService.ts | 2 +- packages/core.gbapp/services/GBMinService.ts | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/admin.gbapp/dialogs/AdminDialog.ts b/packages/admin.gbapp/dialogs/AdminDialog.ts index e567e4daa..0422987ac 100644 --- a/packages/admin.gbapp/dialogs/AdminDialog.ts +++ b/packages/admin.gbapp/dialogs/AdminDialog.ts @@ -421,7 +421,7 @@ export class AdminDialog extends IGBDialog { min.adminService.setValue(min.instance.instanceId, 'AntiCSRFAttackState', state); - const redirectUri = urlJoin(min.instance.botEndpoint, min.instance.botId, '/token'); + const redirectUri = urlJoin(process.env.BOT_URL, min.instance.botId, '/token'); const url = `https://login.microsoftonline.com/${step.activeDialog.state.authenticatorTenant}/oauth2/authorize?client_id=${min.instance.marketplaceId}&response_type=code&redirect_uri=${redirectUri}&scope=https://graph.microsoft.com/.default&state=${state}&response_mode=query`; await min.conversationalService.sendText(min, step, Messages[locale].consent(url)); diff --git a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts index d14963ce2..b69f86c83 100644 --- a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts +++ b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts @@ -455,7 +455,7 @@ export class AzureDeployerService implements IGBInstallationDeployer { }; GBLog.info(`Deploying Bot...`); - instance.botEndpoint = this.defaultEndPoint; + instance.botEndpoint = 'TODO: remove this column.'; instance = await this.internalDeployBot( instance, diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index b4e5f9602..028dc3d5e 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -542,7 +542,7 @@ export class GBMinService { authenticationContext.acquireTokenWithAuthorizationCode( req.query.code, - urlJoin(instance.botEndpoint, min.instance.botId, '/token'), + urlJoin(process.env.BOT_URL, min.instance.botId, '/token'), resource, instance.marketplaceId, instance.marketplacePassword, @@ -561,7 +561,7 @@ export class GBMinService { // Inform the home for default .gbui after finishing token retrival. - res.redirect(min.instance.botEndpoint); + res.redirect(process.env.BOT_URL); } } ); @@ -581,7 +581,7 @@ export class GBMinService { ); authorizationUrl = `${authorizationUrl}?response_type=code&client_id=${ min.instance.marketplaceId - }&redirect_uri=${urlJoin(min.instance.botEndpoint, min.instance.botId, 'token')}`; + }&redirect_uri=${urlJoin(process.env.BOT_URL, min.instance.botId, 'token')}`; GBLog.info(`HandleOAuthRequests: ${authorizationUrl}.`); res.redirect(authorizationUrl); }); From ad9e5b75a5f2274a3e5981018df85be4e6b4a820 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Tue, 5 Sep 2023 12:26:23 -0300 Subject: [PATCH 14/68] fix(all): Fix in bot proxy outdated columns. --- .../azuredeployer.gbapp/services/AzureDeployerService.ts | 5 +++++ packages/core.gbapp/services/GBCoreService.ts | 1 + 2 files changed, 6 insertions(+) diff --git a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts index b69f86c83..3d677905c 100644 --- a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts +++ b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts @@ -266,6 +266,11 @@ export class AzureDeployerService implements IGBInstallationDeployer { } public async updateBotProxy(botId: string, group: string, endpoint: string) { + if (!await this.botExists(botId)) { + GBLog.error(`Bot ${botId} does not exist on cloud.`); + + return; + } const baseUrl = `https://management.azure.com/`; const username = GBConfigService.get('CLOUD_USERNAME'); const password = GBConfigService.get('CLOUD_PASSWORD'); diff --git a/packages/core.gbapp/services/GBCoreService.ts b/packages/core.gbapp/services/GBCoreService.ts index dd1c12312..b47b2875f 100644 --- a/packages/core.gbapp/services/GBCoreService.ts +++ b/packages/core.gbapp/services/GBCoreService.ts @@ -433,6 +433,7 @@ ENDPOINT_UPDATE=true await CollectionUtil.asyncForEach(instances, async instance => { GBLog.info(`Updating bot endpoint for ${instance.botId}...`); try { + await installationDeployer.updateBotProxy( instance.botId, GBConfigService.get('CLOUD_GROUP'), From 4ad3edda3c054e2d04acb6976047e4bcecddfc42 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 12:37:53 -0300 Subject: [PATCH 15/68] fix(all): Fix user switching on a single chip. --- .../whatsapp.gblib/services/WhatsappDirectLine.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index c8ccb59c9..b389c490a 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -1043,11 +1043,6 @@ export class WhatsappDirectLine extends GBService { let urlMin: any = GBServer.globals.minInstances.filter(p => p.instance.botId === botId)[0]; - const botNumber = urlMin ? urlMin.core.getParam(urlMin.instance, 'Bot Number', null) : null; - if (botNumber) { - GBLog.info(`${user.userSystemId} user changed Bot to: ${botId}.`); - user = await sec.updateUserInstance(user.userSystemId, urlMin.instance.instanceId); - } let activeMin; // Processes group behaviour. @@ -1102,10 +1097,15 @@ export class WhatsappDirectLine extends GBService { p.instance.activationCode ? p.instance.activationCode.toLowerCase() === text.toLowerCase() : false )[0]; } - + const botNumber = urlMin ? urlMin.core.getParam(urlMin.instance, 'Bot Number', null) : null; + // If bot has a fixed Find active bot instance. activeMin = botNumber ? urlMin : toSwitchMin ? toSwitchMin : GBServer.globals.minBoot; + if (botNumber) { + GBLog.info(`${user.userSystemId} user changed Bot to: ${botId}.`); + user = await sec.updateUserInstance(user.userSystemId, urlMin.instance.instanceId); + } // If it is the first time for the user, tries to auto-execute // start dialog if any is specified in Config.xlsx. From 9e9620d06cf8e0e22c4d9bb248d0fd1ef9347f29 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 12:50:45 -0300 Subject: [PATCH 16/68] fix(all): Fix user switching on a single chip. --- .../services/WhatsappDirectLine.ts | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index b389c490a..44afcf427 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -1043,12 +1043,18 @@ export class WhatsappDirectLine extends GBService { let urlMin: any = GBServer.globals.minInstances.filter(p => p.instance.botId === botId)[0]; + const botNumber = urlMin ? urlMin.core.getParam(urlMin.instance, 'Bot Number', null) : null; + if (botNumber && GBServer.globals.minBoot.botId !== urlMin.botId) { + GBLog.info(`${user.userSystemId} user changed Bot to: ${botId}.`); + user = await sec.updateUserInstance(user.userSystemId, urlMin.instance.instanceId); + } + let activeMin; - + // Processes group behaviour. text = text.replace(/\@\d+ /gi, ''); - + let group; if (provider === 'chatapi') { // Ensures that the bot group is the active bot for the user (like switching). @@ -1059,7 +1065,7 @@ export class WhatsappDirectLine extends GBService { } } else if (provider === 'GeneralBots') { // Ensures that the bot group is the active bot for the user (like switching). - + const message = req; if (message.from.endsWith('@g.us')) { group = message.from; @@ -1083,12 +1089,12 @@ export class WhatsappDirectLine extends GBService { GBLog.warn(`Group: ${group} not associated with botId:${botId}.`); } } - + // Detects if the welcome message is enabled. if (process.env.WHATSAPP_WELCOME_DISABLED !== 'true') { // Tries to find if user wants to switch bots. - + let toSwitchMin = GBServer.globals.minInstances.filter( p => p.instance.botId.toLowerCase() === text.toLowerCase() )[0]; @@ -1097,15 +1103,10 @@ export class WhatsappDirectLine extends GBService { p.instance.activationCode ? p.instance.activationCode.toLowerCase() === text.toLowerCase() : false )[0]; } - const botNumber = urlMin ? urlMin.core.getParam(urlMin.instance, 'Bot Number', null) : null; // If bot has a fixed Find active bot instance. activeMin = botNumber ? urlMin : toSwitchMin ? toSwitchMin : GBServer.globals.minBoot; - if (botNumber) { - GBLog.info(`${user.userSystemId} user changed Bot to: ${botId}.`); - user = await sec.updateUserInstance(user.userSystemId, urlMin.instance.instanceId); - } // If it is the first time for the user, tries to auto-execute // start dialog if any is specified in Config.xlsx. From 571a5d0922a52d8aa61b45fabb2fe1d644682d35 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 13:04:20 -0300 Subject: [PATCH 17/68] fix(all): Fix user switching on a single chip. --- .../services/WhatsappDirectLine.ts | 255 +++++++++--------- 1 file changed, 125 insertions(+), 130 deletions(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 44afcf427..3b711d1bc 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -105,10 +105,10 @@ export class WhatsappDirectLine extends GBService { whatsappServiceKey === 'internal' ? 'GeneralBots' : whatsappServiceNumber.indexOf(';') > -1 - ? 'maytapi' - : whatsappServiceKey !== 'internal' - ? 'graphapi' - : 'chatapi'; + ? 'maytapi' + : whatsappServiceKey !== 'internal' + ? 'graphapi' + : 'chatapi'; this.groupId = groupId; } @@ -198,8 +198,7 @@ export class WhatsappDirectLine extends GBService { }; if (setUrl) { createClient.bind(this)(); - } - else { + } else { this.customClient = minBoot.whatsAppDirectLine.customClient; } setUrl = false; @@ -308,25 +307,20 @@ export class WhatsappDirectLine extends GBService { let group = ''; let answerText = null; let attachments = null; - switch (provider) { case 'GeneralBots': - message = req; - to = message.to.endsWith('@g.us') ? message.to.split('@')[0] : message.to.split('@')[0]; - const newThis= WhatsappDirectLine.botsByNumber[to]; - if (newThis === undefined){ + to = message.to.endsWith('@g.us') ? message.to.split('@')[0] : message.to.split('@')[0]; + const newThis = WhatsappDirectLine.botsByNumber[to]; + if (newThis === undefined) { throw GBError.create(`Bot Number ${to} not setup for any loaded bot.`); - } - else - { - if (newThis.min.botId !== this.min.botId) - { - await newThis.received (req, res); - + } else { + if (newThis.min.botId !== this.min.botId) { + await newThis.received(req, res); + return; - } + } } text = message.body; @@ -336,9 +330,14 @@ export class WhatsappDirectLine extends GBService { if (message.hasMedia) { const base64Image = await message.downloadMedia(); - let buf: any = Buffer.from(base64Image.data, "base64"); + let buf: any = Buffer.from(base64Image.data, 'base64'); const gbaiName = DialogKeywords.getGBAIPath(this.min.botId); - const localName = Path.join('work', gbaiName, 'cache', `tmp${GBAdminService.getRndReadableIdentifier()}.docx`); + const localName = Path.join( + 'work', + gbaiName, + 'cache', + `tmp${GBAdminService.getRndReadableIdentifier()}.docx` + ); Fs.writeFileSync(localName, buf, { encoding: null }); const url = urlJoin(GBServer.globals.publicAddress, this.min.botId, 'cache', Path.basename(localName)); @@ -546,7 +545,6 @@ export class WhatsappDirectLine extends GBService { ); if (user.agentSystemId.indexOf('@') !== -1) { - // Agent is from Teams. await this.min.conversationalService['sendOnConversation']( this.min, @@ -581,10 +579,10 @@ export class WhatsappDirectLine extends GBService { await this.endTransfer(from, locale, user, agent, sec); } else { GBLog.info(`USER (${from}) TO AGENT ${agent.userSystemId}: ${text}`); - + const prompt = `the person said: ${text}. what can I tell her?`; const answer = await ChatServices.continue(this.min, prompt, 0); - text = `${text} \n\nGeneral Bots: ${answer}` + text = `${text} \n\nGeneral Bots: ${answer}`; if (user.agentSystemId.indexOf('@') !== -1) { // Agent is from Teams or Google Chat. @@ -627,21 +625,17 @@ export class WhatsappDirectLine extends GBService { } private async endTransfer(id: string, locale: string, user: GuaribasUser, agent: GuaribasUser, sec: SecService) { - await this.sendToDeviceEx(id, Messages[this.locale].notify_end_transfer(this.min.instance.botId), locale, null); if (user.agentSystemId.indexOf('@') !== -1) { - // Agent is from Teams. - + await this.min.conversationalService['sendOnConversation']( this.min, agent, Messages[this.locale].notify_end_transfer(this.min.instance.botId) ); - } else { - await this.sendToDeviceEx( user.agentSystemId, Messages[this.locale].notify_end_transfer(this.min.instance.botId), @@ -677,7 +671,7 @@ export class WhatsappDirectLine extends GBService { } }); } catch (e) { - GBLog .error(e); + GBLog.error(e); } } @@ -696,7 +690,8 @@ export class WhatsappDirectLine extends GBService { await this.printMessages(response.obj.activities, conversationId, from, fromName); } catch (err) { GBLog.error( - `Error calling printMessages on Whatsapp channel ${err.data === undefined ? err : err.data} ${err.errObj ? err.errObj.message : '' + `Error calling printMessages on Whatsapp channel ${err.data === undefined ? err : err.data} ${ + err.errObj ? err.errObj.message : '' }` ); } @@ -730,17 +725,15 @@ export class WhatsappDirectLine extends GBService { if (activity.attachments) { await CollectionUtil.asyncForEach(activity.attachments, async attachment => { - switch (attachment.contentType) { case 'application/vnd.microsoft.card.hero': output += `\n${this.renderHeroCard(attachment)}`; break; case 'image/png': - await this.sendFileToDevice(to, attachment.contentUrl, - attachment.name, attachment.name, 0); + await this.sendFileToDevice(to, attachment.contentUrl, attachment.name, attachment.name, 0); - return; + return; default: GBLog.info(`Unknown content type: ${attachment.contentType}`); @@ -982,11 +975,9 @@ export class WhatsappDirectLine extends GBService { switch (provider) { case 'GeneralBots': - - // Ignore E2E messages and status updates. + // Ignore E2E messages and status updates. - if (req.type && req.type === 'e2e_notification' - || req.isStatus) { + if ((req.type && req.type === 'e2e_notification') || req.isStatus) { return; } @@ -1048,13 +1039,13 @@ export class WhatsappDirectLine extends GBService { GBLog.info(`${user.userSystemId} user changed Bot to: ${botId}.`); user = await sec.updateUserInstance(user.userSystemId, urlMin.instance.instanceId); } - + let activeMin; - + // Processes group behaviour. text = text.replace(/\@\d+ /gi, ''); - + let group; if (provider === 'chatapi') { // Ensures that the bot group is the active bot for the user (like switching). @@ -1065,7 +1056,7 @@ export class WhatsappDirectLine extends GBService { } } else if (provider === 'GeneralBots') { // Ensures that the bot group is the active bot for the user (like switching). - + const message = req; if (message.from.endsWith('@g.us')) { group = message.from; @@ -1089,37 +1080,88 @@ export class WhatsappDirectLine extends GBService { GBLog.warn(`Group: ${group} not associated with botId:${botId}.`); } } - + // Detects if the welcome message is enabled. - if (process.env.WHATSAPP_WELCOME_DISABLED !== 'true') { - // Tries to find if user wants to switch bots. - - let toSwitchMin = GBServer.globals.minInstances.filter( - p => p.instance.botId.toLowerCase() === text.toLowerCase() + if (process.env.WHATSAPP_WELCOME_DISABLED === 'true') { + let minInstance = GBServer.globals.minInstances.filter( + p => p.instance.botId.toLowerCase() === botId.toLowerCase() )[0]; - if (!toSwitchMin) { - toSwitchMin = GBServer.globals.minInstances.filter(p => - p.instance.activationCode ? p.instance.activationCode.toLowerCase() === text.toLowerCase() : false - )[0]; + + // Just pass the message to the receiver. + + await minInstance.whatsAppDirectLine.received(req, res); + + return; + } + + // Tries to find if user wants to switch bots. + + let toSwitchMin = GBServer.globals.minInstances.filter( + p => p.instance.botId.toLowerCase() === text.toLowerCase() + )[0]; + + if (!toSwitchMin) { + toSwitchMin = GBServer.globals.minInstances.filter(p => + p.instance.activationCode ? p.instance.activationCode.toLowerCase() === text.toLowerCase() : false + )[0]; + } + + // If bot has a fixed Find active bot instance. + + activeMin = botNumber ? urlMin : toSwitchMin ? toSwitchMin : GBServer.globals.minBoot; + + // If it is the first time for the user, tries to auto-execute + // start dialog if any is specified in Config.xlsx. + + if (user === null || user.hearOnDialog) { + user = await sec.ensureUser(activeMin.instance.instanceId, id, senderName, '', 'whatsapp', senderName, null); + + const startDialog = user.hearOnDialog + ? user.hearOnDialog + : activeMin.core.getParam(activeMin.instance, 'Start Dialog', null); + + if (startDialog) { + GBLog.info(`Calling /start to Auto start ${startDialog} for ${activeMin.instance.instanceId}...`); + if (provider === 'chatapi') { + req.body.messages[0].body = `/start`; + } else if (provider === 'maytapi') { + req.body.message = `/start`; + } else { + req.body = `/start`; + } + + // Resets HEAR ON DIALOG value to none and passes + // current dialog to the direct line. + + await sec.updateUserHearOnDialog(user.userId, null); + await (activeMin as any).whatsAppDirectLine.received(req, res); + } else { + await (activeMin as any).whatsAppDirectLine.sendToDevice( + id, + `Olá! Seja bem-vinda(o)!\nMe chamo ${activeMin.instance.title}. Como posso ajudar? Pode me falar que eu te ouço, me manda um aúdio.`, + null + ); + if (res) { + res.end(); + } } - - // If bot has a fixed Find active bot instance. + } else { + // User wants to switch bots. - activeMin = botNumber ? urlMin : toSwitchMin ? toSwitchMin : GBServer.globals.minBoot; + if (toSwitchMin !== undefined) { + GBLog.info(`Switching bots from ${botId} to ${toSwitchMin.botId}...`); - // If it is the first time for the user, tries to auto-execute - // start dialog if any is specified in Config.xlsx. + // So gets the new bot instance information and prepares to + // auto start dialog if any is specified. - if (user === null || user.hearOnDialog) { - user = await sec.ensureUser(activeMin.instance.instanceId, id, senderName, '', 'whatsapp', senderName, null); - - const startDialog = user.hearOnDialog - ? user.hearOnDialog - : activeMin.core.getParam(activeMin.instance, 'Start Dialog', null); + const instance = await this.min.core.loadInstanceByBotId(activeMin.botId); + await sec.updateUserInstance(id, instance.instanceId); + await (activeMin as any).whatsAppDirectLine.resetConversationId(activeMin.botId, id, ''); + const startDialog = activeMin.core.getParam(activeMin.instance, 'Start Dialog', null); if (startDialog) { - GBLog.info(`Calling /start to Auto start ${startDialog} for ${activeMin.instance.instanceId}...`); + GBLog.info(`Calling /start for Auto start : ${startDialog} for ${activeMin.instance.botId}...`); if (provider === 'chatapi') { req.body.messages[0].body = `/start`; } else if (provider === 'maytapi') { @@ -1128,84 +1170,37 @@ export class WhatsappDirectLine extends GBService { req.body = `/start`; } - // Resets HEAR ON DIALOG value to none and passes - // current dialog to the direct line. - - await sec.updateUserHearOnDialog(user.userId, null); await (activeMin as any).whatsAppDirectLine.received(req, res); } else { await (activeMin as any).whatsAppDirectLine.sendToDevice( id, - `Olá! Seja bem-vinda(o)!\nMe chamo ${activeMin.instance.title}. Como posso ajudar? Pode me falar que eu te ouço, me manda um aúdio.`, + `Agora falando com ${activeMin.instance.title}...`, null ); - if (res) { - res.end(); - } + } + if (res) { + res.end(); } } else { - // User wants to switch bots. - - if (toSwitchMin !== undefined) { - // So gets the new bot instance information and prepares to - // auto start dialog if any is specified. - - const instance = await this.min.core.loadInstanceByBotId(activeMin.botId); - await sec.updateUserInstance(id, instance.instanceId); - await (activeMin as any).whatsAppDirectLine.resetConversationId(activeMin.botId, id, ''); - const startDialog = activeMin.core.getParam(activeMin.instance, 'Start Dialog', null); - - if (startDialog) { - GBLog.info(`Calling /start for Auto start : ${startDialog} for ${activeMin.instance.botId}...`); - if (provider === 'chatapi') { - req.body.messages[0].body = `/start`; - } else if (provider === 'maytapi') { - req.body.message = `/start`; - } else { - req.body = `/start`; - } - - await (activeMin as any).whatsAppDirectLine.received(req, res); - } else { - await (activeMin as any).whatsAppDirectLine.sendToDevice( - id, - `Agora falando com ${activeMin.instance.title}...`, - null - ); - } - if (res) { - res.end(); - } + let t; + activeMin = GBServer.globals.minInstances.filter(p => p.instance.instanceId === user.instanceId)[0]; + if (activeMin === undefined) { + activeMin = GBServer.globals.minBoot; + t = (activeMin as any).whatsAppDirectLine; + await t.sendToDevice( + id, + `O outro Bot que você estava falando(${user.instanceId}), não está mais disponível. Agora você está falando comigo, ${activeMin.instance.title}...` + ); } else { - let t; - activeMin = GBServer.globals.minInstances.filter(p => p.instance.instanceId === user.instanceId)[0]; - if (activeMin === undefined) { - activeMin = GBServer.globals.minBoot; + if ((activeMin as any).whatsAppDirectLine) { t = (activeMin as any).whatsAppDirectLine; - await t.sendToDevice( - id, - `O outro Bot que você estava falando(${user.instanceId}), não está mais disponível. Agora você está falando comigo, ${activeMin.instance.title}...` - ); + } else { + t = (GBServer.globals.minBoot as any).whatsAppDirectLine; } - else { - if ((activeMin as any).whatsAppDirectLine) { - t = (activeMin as any).whatsAppDirectLine; - } else { - t = (GBServer.globals.minBoot as any).whatsAppDirectLine; - } - } - - t.received(req, res); } + + t.received(req, res); } - } else { - let minInstance = GBServer.globals.minInstances.filter( - p => p.instance.botId.toLowerCase() === botId.toLowerCase() - )[0]; - - // Just pass the message to the receiver. - - await minInstance.whatsAppDirectLine.received(req, res); } } catch (error) { GBLog.error(`Error on Whatsapp callback: ${error.data ? error.data : error} ${error.stack}`); From f7ddc1078ab5a3fbbdfccc9ddf751ec3b6fbd47f Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 13:09:49 -0300 Subject: [PATCH 18/68] fix(all): Fix user switching on a single chip. --- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 3b711d1bc..4266df065 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -1036,7 +1036,7 @@ export class WhatsappDirectLine extends GBService { const botNumber = urlMin ? urlMin.core.getParam(urlMin.instance, 'Bot Number', null) : null; if (botNumber && GBServer.globals.minBoot.botId !== urlMin.botId) { - GBLog.info(`${user.userSystemId} user changed Bot to: ${botId}.`); + GBLog.info(`${user.userSystemId} fixed by bot number talked to: ${botId}.`); user = await sec.updateUserInstance(user.userSystemId, urlMin.instance.instanceId); } @@ -1154,7 +1154,7 @@ export class WhatsappDirectLine extends GBService { // So gets the new bot instance information and prepares to // auto start dialog if any is specified. - + activeMin = toSwitchMin; const instance = await this.min.core.loadInstanceByBotId(activeMin.botId); await sec.updateUserInstance(id, instance.instanceId); await (activeMin as any).whatsAppDirectLine.resetConversationId(activeMin.botId, id, ''); From debe2524c23d53cc8f565230e51fec4079ca247d Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 13:59:14 -0300 Subject: [PATCH 19/68] fix(all): Fix user switching on a single chip. --- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 4266df065..cefad9b49 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -313,10 +313,17 @@ export class WhatsappDirectLine extends GBService { message = req; to = message.to.endsWith('@g.us') ? message.to.split('@')[0] : message.to.split('@')[0]; const newThis = WhatsappDirectLine.botsByNumber[to]; + + // Every number must be mapped to a bot, even if shared by boot bot. + if (newThis === undefined) { throw GBError.create(`Bot Number ${to} not setup for any loaded bot.`); } else { - if (newThis.min.botId !== this.min.botId) { + + // Reroute to custom bot only if it is not boot bot. + + if (newThis.min.botId !== this.min.botId && + GBServer.globals.minBoot.botId !== this.min.botId) { await newThis.received(req, res); return; From 49ff8ea278932c1e3a79a2fbad4ce4d0a03e4b9e Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 14:07:05 -0300 Subject: [PATCH 20/68] fix(all): Fix user switching on a single chip. --- packages/core.gbapp/services/GBMinService.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 028dc3d5e..57195cf8e 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -771,6 +771,8 @@ export class GBMinService { const group = min.core.getParam(min.instance, 'WhatsApp Group ID', null); WhatsappDirectLine.botGroups[min.botId] = group; + + const minBoot = GBServer.globals.minBoot as any; // If there is WhatsApp configuration specified, initialize // infrastructure objects. @@ -788,7 +790,6 @@ export class GBMinService { await min.whatsAppDirectLine.setup(true); } else { - const minBoot = GBServer.globals.minBoot as any; if (min !== minBoot && minBoot.instance.whatsappServiceKey) { min.whatsAppDirectLine = new WhatsappDirectLine( min, @@ -803,11 +804,15 @@ export class GBMinService { } } - const botNumber = min.core.getParam(min.instance, 'Bot Number', null); - if (botNumber){ - WhatsappDirectLine.botsByNumber[botNumber] = min.whatsAppDirectLine; + // Builds bot numbers map in WhatsAppDirectLine globals. + + let botNumber = min.core.getParam(min.instance, 'Bot Number', null); + if (!botNumber){ + botNumber = minBoot.core.getParam(minBoot.instance, 'Bot Number', null); } - + + WhatsappDirectLine.botsByNumber[botNumber] = min.whatsAppDirectLine; + // Setups default BOT Framework dialogs. min.userProfile = conversationState.createProperty('userProfile'); From ac8f1eb70b2a19a778bd84fe698918be628d9966 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 14:13:22 -0300 Subject: [PATCH 21/68] fix(all): Fix user switching on a single chip. --- src/app.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/app.ts b/src/app.ts index bd631062b..52d2a5477 100644 --- a/src/app.ts +++ b/src/app.ts @@ -60,6 +60,7 @@ import { RootData } from './RootData.js'; import { GBSSR } from '../packages/core.gbapp/services/GBSSR.js'; import { Mutex } from 'async-mutex'; import { GBVMService } from '../packages/basic.gblib/services/GBVMService.js'; +import e from 'express'; /** * General Bots open-core entry point. @@ -108,6 +109,7 @@ export class GBServer { // Setups global error handlers. process.on('unhandledRejection', (err, p) => { + err = err['e'] ? err['e'] : err; GBLog.error(`UNHANDLED_REJECTION(promises): ${err.toString()} ${err['stack'] ? '\n' + err['stack'] : ''}`); if (err['response']?.obj?.httpStatusCode === 404) { GBLog.warn(`Check reverse proxy: ${process.env.BOT_URL} as it seems to be invalid.`); @@ -116,6 +118,7 @@ export class GBServer { process.on('uncaughtException', (err, p) => { if (err !== null) { + err = err['e'] ? err['e'] : err; GBLog.error(`UNCAUGHT_EXCEPTION: ${err.toString()} ${err['stack'] ? '\n' + err['stack'] : ''}`); } else { GBLog.error('UNCAUGHT_EXCEPTION: Unknown error (err is null)'); From 2369fb2d9d747e58d2156d15d1ac46309c0d6b1d Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 14:16:54 -0300 Subject: [PATCH 22/68] fix(all): Fix user switching on a single chip. --- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index cefad9b49..ef55a4336 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -51,6 +51,7 @@ import pkg from 'whatsapp-web.js'; import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js'; import { ChatServices } from '../../gpt.gblib/services/ChatServices.js'; import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js'; +import e from 'express'; const { List, Buttons, Client, MessageMedia } = pkg; /** @@ -1210,6 +1211,7 @@ export class WhatsappDirectLine extends GBService { } } } catch (error) { + error = error['e'] ? error['e'] : error; GBLog.error(`Error on Whatsapp callback: ${error.data ? error.data : error} ${error.stack}`); } } From e53b75dd6c2f7627732264d235739ffcd382cd3b Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 14:29:01 -0300 Subject: [PATCH 23/68] fix(all): Fix user switching on a single chip. --- packages/core.gbapp/services/GBMinService.ts | 6 ++---- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 4 +--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 57195cf8e..1f5786ff8 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -807,12 +807,10 @@ export class GBMinService { // Builds bot numbers map in WhatsAppDirectLine globals. let botNumber = min.core.getParam(min.instance, 'Bot Number', null); - if (!botNumber){ - botNumber = minBoot.core.getParam(minBoot.instance, 'Bot Number', null); + if (botNumber){ + WhatsappDirectLine.botsByNumber[botNumber] = min.whatsAppDirectLine; } - WhatsappDirectLine.botsByNumber[botNumber] = min.whatsAppDirectLine; - // Setups default BOT Framework dialogs. min.userProfile = conversationState.createProperty('userProfile'); diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index ef55a4336..632a9d026 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -317,9 +317,7 @@ export class WhatsappDirectLine extends GBService { // Every number must be mapped to a bot, even if shared by boot bot. - if (newThis === undefined) { - throw GBError.create(`Bot Number ${to} not setup for any loaded bot.`); - } else { + if (newThis) { // Reroute to custom bot only if it is not boot bot. From dfbccfb3ad73fb1f304a722eef8fae596ffa5f4d Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 16:19:17 -0300 Subject: [PATCH 24/68] fix(all): Fix user switching on a single chip. --- .../services/WhatsappDirectLine.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 632a9d026..599443f07 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -1034,8 +1034,15 @@ export class WhatsappDirectLine extends GBService { } const sec = new SecService(); - let user = await sec.getUserFromSystemId(id); + + // Tries to find if user wants to switch bots. + + let toSwitchMin = GBServer.globals.minInstances.filter( + p => p.instance.botId.toLowerCase() === text.toLowerCase() + )[0]; + + GBLog.info(`A WhatsApp mobile requested instance for: ${botId}.`); let urlMin: any = GBServer.globals.minInstances.filter(p => p.instance.botId === botId)[0]; @@ -1101,12 +1108,6 @@ export class WhatsappDirectLine extends GBService { return; } - // Tries to find if user wants to switch bots. - - let toSwitchMin = GBServer.globals.minInstances.filter( - p => p.instance.botId.toLowerCase() === text.toLowerCase() - )[0]; - if (!toSwitchMin) { toSwitchMin = GBServer.globals.minInstances.filter(p => p.instance.activationCode ? p.instance.activationCode.toLowerCase() === text.toLowerCase() : false @@ -1160,6 +1161,7 @@ export class WhatsappDirectLine extends GBService { // So gets the new bot instance information and prepares to // auto start dialog if any is specified. + activeMin = toSwitchMin; const instance = await this.min.core.loadInstanceByBotId(activeMin.botId); await sec.updateUserInstance(id, instance.instanceId); @@ -1167,6 +1169,7 @@ export class WhatsappDirectLine extends GBService { const startDialog = activeMin.core.getParam(activeMin.instance, 'Start Dialog', null); if (startDialog) { + GBLog.info(`Calling /start for Auto start : ${startDialog} for ${activeMin.instance.botId}...`); if (provider === 'chatapi') { req.body.messages[0].body = `/start`; From b97194ebaa6bf8d3449a6c2200b3375d1eb2c32e Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 16:38:47 -0300 Subject: [PATCH 25/68] fix(all): Fix user switching on a single chip. --- .../services/WhatsappDirectLine.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 599443f07..eab3d913c 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -315,18 +315,13 @@ export class WhatsappDirectLine extends GBService { to = message.to.endsWith('@g.us') ? message.to.split('@')[0] : message.to.split('@')[0]; const newThis = WhatsappDirectLine.botsByNumber[to]; - // Every number must be mapped to a bot, even if shared by boot bot. - - if (newThis) { + // If there is a number specified, checks if it + // is related to a custom bot and reroutes immediately. - // Reroute to custom bot only if it is not boot bot. + if (newThis && newThis.min.botId !== this.min.botId) { + await newThis.received(req, res); - if (newThis.min.botId !== this.min.botId && - GBServer.globals.minBoot.botId !== this.min.botId) { - await newThis.received(req, res); - - return; - } + return; } text = message.body; @@ -1169,7 +1164,7 @@ export class WhatsappDirectLine extends GBService { const startDialog = activeMin.core.getParam(activeMin.instance, 'Start Dialog', null); if (startDialog) { - + GBLog.info(`Calling /start for Auto start : ${startDialog} for ${activeMin.instance.botId}...`); if (provider === 'chatapi') { req.body.messages[0].body = `/start`; From 3ad01b7d2275fac1a4512286ada1c0e82080c233 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 16:44:53 -0300 Subject: [PATCH 26/68] fix(all): Fix user switching on a single chip. --- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index eab3d913c..f01e669a0 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -318,7 +318,7 @@ export class WhatsappDirectLine extends GBService { // If there is a number specified, checks if it // is related to a custom bot and reroutes immediately. - if (newThis && newThis.min.botId !== this.min.botId) { + if (newThis && newThis.min.botId !== GBServer.globals.minBoot.botId) { await newThis.received(req, res); return; From 2162b060ba0fe191b9fd9fadff3f88f97eb0b4be Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 21:36:00 -0300 Subject: [PATCH 27/68] fix(all): Fix user switching on a single chip. --- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index f01e669a0..893bf2a4b 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -318,7 +318,7 @@ export class WhatsappDirectLine extends GBService { // If there is a number specified, checks if it // is related to a custom bot and reroutes immediately. - if (newThis && newThis.min.botId !== GBServer.globals.minBoot.botId) { + if (newThis !== this && newThis.min.botId !== GBServer.globals.minBoot.botId) { await newThis.received(req, res); return; From 7e88eca8b281ca72d20abcb905b9f0dc6270dc8a Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 21:57:26 -0300 Subject: [PATCH 28/68] fix(all): Fix user switching on a single chip. --- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 893bf2a4b..6531be8c9 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -1159,7 +1159,7 @@ export class WhatsappDirectLine extends GBService { activeMin = toSwitchMin; const instance = await this.min.core.loadInstanceByBotId(activeMin.botId); - await sec.updateUserInstance(id, instance.instanceId); + user = await sec.updateUserInstance(id, instance.instanceId); await (activeMin as any).whatsAppDirectLine.resetConversationId(activeMin.botId, id, ''); const startDialog = activeMin.core.getParam(activeMin.instance, 'Start Dialog', null); From 7266b0f43bdf6da0f70514f3e6d5ed6fee98401c Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 9 Sep 2023 22:33:06 -0300 Subject: [PATCH 29/68] fix(all): Fix user switching on a single chip. --- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 6531be8c9..5900ed228 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -1151,7 +1151,7 @@ export class WhatsappDirectLine extends GBService { } else { // User wants to switch bots. - if (toSwitchMin !== undefined) { + if (toSwitchMin) { GBLog.info(`Switching bots from ${botId} to ${toSwitchMin.botId}...`); // So gets the new bot instance information and prepares to @@ -1203,7 +1203,7 @@ export class WhatsappDirectLine extends GBService { } } - t.received(req, res); + await t.received(req, res); } } } catch (error) { From 3319942b3175b7c30df836dcd80a3e1b5c7f20eb Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 10 Sep 2023 13:33:20 -0300 Subject: [PATCH 30/68] fix(all): Fix search on others bots than boot. --- packages/core.gbapp/services/GBDeployer.ts | 5 ++++- packages/kb.gbapp/services/KBService.ts | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 28c3feedf..b2326db77 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -105,7 +105,10 @@ export class GBDeployer implements IGBDeployer { */ public static async internalGetDriveClient(min: GBMinInstance) { let token; - if (min['cacheToken']) { + + // TODO: Add expiration logic. + + if (min['cacheToken'] && null) { return min['cacheToken']; } else { diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index d14431a8b..a35303408 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -289,9 +289,12 @@ export class KBService implements IGBKBService { } } + let key = instance.searchKey ? instance.searchKey: + GBServer.globals.minBoot.instance.searchKey; + // No direct match found, so Search is used. - if (instance.searchKey !== null && GBConfigService.get('STORAGE_DIALECT') === 'mssql') { + if (key !== null && GBConfigService.get('STORAGE_DIALECT') === 'mssql') { interface SearchResults { instanceId: number; questionId: number; @@ -304,7 +307,7 @@ export class KBService implements IGBKBService { } const client = new SearchClient('https://' + instance.searchHost, 'azuresql-index', { - key: instance.searchKey + key: key } as any); const results = await client.search(query, { From 45bc5d16e3cfd66a124337887f1d70408fd7c6b2 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 10 Sep 2023 13:39:07 -0300 Subject: [PATCH 31/68] fix(all): Fix search on others bots than boot. --- packages/kb.gbapp/services/KBService.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index a35303408..264ff589d 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -289,8 +289,10 @@ export class KBService implements IGBKBService { } } - let key = instance.searchKey ? instance.searchKey: + const key = instance.searchKey ? instance.searchKey: GBServer.globals.minBoot.instance.searchKey; + const host = instance.searchHost ? instance.searchHost : + GBServer.globals.minBoot.instance.searchHost; // No direct match found, so Search is used. @@ -306,7 +308,7 @@ export class KBService implements IGBKBService { subject4: string; } - const client = new SearchClient('https://' + instance.searchHost, 'azuresql-index', { + const client = new SearchClient('https://' + host, 'azuresql-index', { key: key } as any); From 53a5bfbeeb2fc382f96d71d06039365f2d1fdc41 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 10 Sep 2023 17:23:32 -0300 Subject: [PATCH 32/68] fix(all): Fix search on others bots than boot. --- packages/core.gbapp/services/GBDeployer.ts | 16 ++++++++++++---- packages/kb.gbapp/services/KBService.ts | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index b2326db77..e2ffa8cdb 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -720,13 +720,21 @@ export class GBDeployer implements IGBDeployer { GBLogEx.info(instance.instanceId, `Acquiring rebuildIndex mutex...`); release = await GBServer.globals.indexSemaphore.acquire(); GBLogEx.info(instance.instanceId, `Acquire rebuildIndex done.`); + + const key = instance.searchKey ? instance.searchKey : GBServer.globals.minBoot.instance.searchKey; + const searchIndex = instance.searchIndex ? instance.searchIndex : GBServer.globals.minBoot.instance.searchIndex; + const searchIndexer = instance.searchIndexer + ? instance.searchIndexer + : GBServer.globals.minBoot.instance.searchIndexer; + const host = instance.searchHost ? instance.searchHost : GBServer.globals.minBoot.instance.searchHost; + // Prepares search. const search = new AzureSearch( - instance.searchKey, - instance.searchHost, - instance.searchIndex, - instance.searchIndexer + key, + host, + searchIndex, + searchIndexer ); const connectionString = GBDeployer.getConnectionStringFromInstance(instance); const dsName = 'gb'; diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index 264ff589d..d03f2ae55 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -312,7 +312,7 @@ export class KBService implements IGBKBService { key: key } as any); - const results = await client.search(query, { + const results = await client.search(query.substring(0,499), { filter: `instanceId eq ${instance.instanceId} and skipIndex eq false`, searchFields: ['content', 'subject1', 'subject2', 'subject3', 'subject4'], select: ['instanceId', 'questionId', 'answerId'], From 081999396d6f30087f6c2a2968be87bb6cc59909 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 10 Sep 2023 17:37:13 -0300 Subject: [PATCH 33/68] fix(all): Fix search on others bots than boot. --- packages/basic.gblib/services/KeywordsExpressions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/basic.gblib/services/KeywordsExpressions.ts b/packages/basic.gblib/services/KeywordsExpressions.ts index b6dad81e7..b1e2ebce1 100644 --- a/packages/basic.gblib/services/KeywordsExpressions.ts +++ b/packages/basic.gblib/services/KeywordsExpressions.ts @@ -331,7 +331,7 @@ export class KeywordsExpressions { /^\s*(.*)\=\s*(REWRITE)(\s*)(.*)/gim, ($0, $1, $2, $3, $4) => { const params = this.getParams($4, ['text']); - return `await sys.rewrite ({pid: pid, ${params}})`; + return `${$1} = await sys.rewrite ({pid: pid, ${params}})`; } ]; From 292e62c4cc1231062a32a506b0ada002c94d3568 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 10 Sep 2023 20:40:42 -0300 Subject: [PATCH 34/68] fix(all): Fix search on others bots than boot. --- package.json | 4 ++-- packages/gpt.gblib/services/ChatServices.ts | 25 ++++++++++++--------- packages/kb.gbapp/services/KBService.ts | 10 +++++++++ src/app.ts | 5 +++-- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index bd8c8c708..bce9a400d 100644 --- a/package.json +++ b/package.json @@ -97,7 +97,7 @@ "botlib": "3.0.11", "c3-chart-maker": "0.2.8", "cd": "0.3.3", - "chatgpt": "2.4.2", + "chatgpt": "^2.4.2", "chrome-remote-interface": "0.31.3", "cli-progress": "3.11.2", "cli-spinner": "0.2.10", @@ -136,7 +136,7 @@ "npm": "9.6.1", "open": "8.4.0", "open-docxtemplater-image-module": "1.0.3", - "openai": "3.3.0", + "openai": "4.6.0", "pdf-extraction": "1.0.2", "pdf-to-png-converter": "3.1.0", "pdfkit": "0.13.0", diff --git a/packages/gpt.gblib/services/ChatServices.ts b/packages/gpt.gblib/services/ChatServices.ts index d3fa50c83..839bab85e 100644 --- a/packages/gpt.gblib/services/ChatServices.ts +++ b/packages/gpt.gblib/services/ChatServices.ts @@ -33,7 +33,8 @@ 'use strict'; import { GBMinInstance } from 'botlib'; -import { Configuration, OpenAIApi } from "openai"; +import OpenAI from "openai"; +import { ChatGPTAPIBrowser, getOpenAIAuth } from 'chatgpt' export class ChatServices { /** @@ -45,22 +46,24 @@ export class ChatServices { * */ public static async continue(min: GBMinInstance, text: string, chatId) { - let key = min.core.getParam(min.instance, 'Open AI Key', null); + let key; + if (process.env.OPENAI_KEY) { + key = process.env.OPENAI_KEY; + } + else { + key = min.core.getParam(min.instance, 'Open AI Key', null); + } if (!key) { throw new Error('Open AI Key not configured in .gbot.'); } - - const configuration = new Configuration({ - apiKey: key, + const openai = new OpenAI({ + apiKey: key }); - const openai = new OpenAIApi(configuration); - - const chatCompletion = await openai.createChatCompletion({ + const chatCompletion = await openai.chat.completions.create({ model: "gpt-3.5-turbo", - messages: [{role: "user", content: text}], + messages: [{ role: "user", content: text }], }); - return chatCompletion.data.choices[0].message.content; - + return chatCompletion.choices[0].message.content; } } diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index d03f2ae55..b4c9ec141 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -585,6 +585,16 @@ export class KBService implements IGBKBService { } }); + await GuaribasQuestion.destroy({ + where: { instanceId: min.instance.instanceId } + }); + await GuaribasAnswer.destroy({ + where: { instanceId: min.instance.instanceId } + }); + await GuaribasSubject.destroy({ + where: { instanceId: min.instance.instanceId} + }); + const answersCreated = await GuaribasAnswer.bulkCreate(answers); let i = 0; diff --git a/src/app.ts b/src/app.ts index 52d2a5477..3313ab34e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -59,8 +59,6 @@ import * as winston from 'winston-logs-display'; import { RootData } from './RootData.js'; import { GBSSR } from '../packages/core.gbapp/services/GBSSR.js'; import { Mutex } from 'async-mutex'; -import { GBVMService } from '../packages/basic.gblib/services/GBVMService.js'; -import e from 'express'; /** * General Bots open-core entry point. @@ -78,6 +76,8 @@ export class GBServer { GBConfigService.init(); const port = GBConfigService.getServerPort(); + + if (process.env.TEST_SHELL) { GBLog.info(`Running TEST_SHELL: ${process.env.TEST_SHELL}...`); try { @@ -139,6 +139,7 @@ export class GBServer { const mainCallback = () => { (async () => { + try { GBLog.info(`Now accepting connections on ${port}...`); process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; From 68c542bef30513d81c2a13c04d5d9f8b957765f7 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 10 Sep 2023 20:53:15 -0300 Subject: [PATCH 35/68] fix(all): Fix search on others bots than boot. --- packages/core.gbapp/services/GBDeployer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index e2ffa8cdb..1483181b4 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -736,7 +736,7 @@ export class GBDeployer implements IGBDeployer { searchIndex, searchIndexer ); - const connectionString = GBDeployer.getConnectionStringFromInstance(instance); + const connectionString = GBDeployer.getConnectionStringFromInstance(GBServer.globals.minBoot.instance); const dsName = 'gb'; // Removes any previous index. From 7992c6683154033f47a8285d6a3c54758187a468 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 10 Sep 2023 22:18:05 -0300 Subject: [PATCH 36/68] fix(all): Fix search on others bots than boot. --- packages/core.gbapp/services/GBDeployer.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 1483181b4..9af37018d 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -717,9 +717,9 @@ export class GBDeployer implements IGBDeployer { // Prepares search. let release; try { - GBLogEx.info(instance.instanceId, `Acquiring rebuildIndex mutex...`); - release = await GBServer.globals.indexSemaphore.acquire(); - GBLogEx.info(instance.instanceId, `Acquire rebuildIndex done.`); + // GBLogEx.info(instance.instanceId, `Acquiring rebuildIndex mutex...`); + // release = await GBServer.globals.indexSemaphore.acquire(); + // GBLogEx.info(instance.instanceId, `Acquire rebuildIndex done.`); const key = instance.searchKey ? instance.searchKey : GBServer.globals.minBoot.instance.searchKey; const searchIndex = instance.searchIndex ? instance.searchIndex : GBServer.globals.minBoot.instance.searchIndex; @@ -773,12 +773,12 @@ export class GBDeployer implements IGBDeployer { } await search.createIndex(searchSchema, dsName); - release(); + // release(); GBLogEx.info(instance.instanceId, `Released rebuildIndex mutex.`); } catch { - if (release) { - release(); - } + // if (release) { + // release(); + // } } } From b1bce0106427324c8da88cc84c525faeca71d2a9 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 10 Sep 2023 22:36:39 -0300 Subject: [PATCH 37/68] fix(all): Fix search on others bots than boot. --- packages/core.gbapp/services/GBDeployer.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 9af37018d..4f3525d13 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -716,8 +716,10 @@ export class GBDeployer implements IGBDeployer { public async rebuildIndex(instance: IGBInstance, searchSchema: any) { // Prepares search. let release; - try { - // GBLogEx.info(instance.instanceId, `Acquiring rebuildIndex mutex...`); + + // TODO: Semaphore logic. + //try { + GBLogEx.info(instance.instanceId, `rebuildIndex running...`); // release = await GBServer.globals.indexSemaphore.acquire(); // GBLogEx.info(instance.instanceId, `Acquire rebuildIndex done.`); @@ -775,11 +777,11 @@ export class GBDeployer implements IGBDeployer { // release(); GBLogEx.info(instance.instanceId, `Released rebuildIndex mutex.`); - } catch { + //} catch { // if (release) { // release(); // } - } + //} } /** From 6427be19ad8608f9ae06ad80d4b90522e22568e5 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 10 Sep 2023 22:42:32 -0300 Subject: [PATCH 38/68] fix(all): Fix search on others bots than boot. --- packages/admin.gbapp/services/GBAdminService.ts | 3 ++- packages/kb.gbapp/services/KBService.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/admin.gbapp/services/GBAdminService.ts b/packages/admin.gbapp/services/GBAdminService.ts index ef5aaa99b..c8648f8db 100644 --- a/packages/admin.gbapp/services/GBAdminService.ts +++ b/packages/admin.gbapp/services/GBAdminService.ts @@ -178,9 +178,10 @@ export class GBAdminService implements IGBAdminService { } public static async rebuildIndexPackageCommand(min: GBMinInstance, deployer: GBDeployer) { const service = await AzureDeployerService.createInstance(deployer); + const searchIndex = min.instance.searchIndex ? min.instance.searchIndex : GBServer.globals.minBoot.instance.searchIndex; await deployer.rebuildIndex( min.instance, - service.getKBSearchSchema(min.instance.searchIndex) + service.getKBSearchSchema(searchIndex) ); } diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index b4c9ec141..f685824ea 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -1031,7 +1031,8 @@ export class KBService implements IGBKBService { await this.importKbPackage(min, localPath, p, instance); GBDeployer.mountGBKBAssets(packageName, min.botId, localPath); const service = await AzureDeployerService.createInstance(deployer); - await deployer.rebuildIndex(instance, service.getKBSearchSchema(instance.searchIndex)); + const searchIndex = instance.searchIndex ? instance.searchIndex : GBServer.globals.minBoot.instance.searchIndex; + await deployer.rebuildIndex(instance, service.getKBSearchSchema(searchIndex)); min['groupCache'] = await KBService.getGroupReplies(instance.instanceId); await KBService.RefreshNER(min); From 90b34e7499bc2974ac589843a10edf99acf7328f Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Mon, 11 Sep 2023 10:47:47 -0300 Subject: [PATCH 39/68] fix(all): Fix search on others bots than boot. --- packages/core.gbapp/services/GBMinService.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 1f5786ff8..43ba9c120 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -1184,7 +1184,7 @@ export class GBMinService { return utterance.match(Messages.global_quit); } - private async handleUploads(min, step, user, params) + private async handleUploads(min, step, user, params, autoSave) { // Prepare Promises to download each attachment and then execute each Promise. @@ -1358,8 +1358,9 @@ export class GBMinService { return; } } - - await this.handleUploads(min, step, user, params); + + const notes = min.core.getParam(min.instance, 'Notes', null); + await this.handleUploads(min, step, user, params, notes != null); // Files in .gbdialog can be called directly by typing its name normalized into JS . @@ -1416,7 +1417,7 @@ export class GBMinService { step.context.activity['originalText'] step.context.activity['text'] = text; - const notes = min.core.getParam(min.instance, 'Notes', null); + if (notes && text && text !== "") { const sys = new SystemKeywords(); await sys.note({pid, text}); From f3d58ff4fcc127dd387f2a242b9d93d8699ad8b1 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Mon, 11 Sep 2023 10:51:43 -0300 Subject: [PATCH 40/68] fix(all): Fix search on others bots than boot. --- packages/core.gbapp/services/GBMinService.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 43ba9c120..ee3fbb13b 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -1202,7 +1202,7 @@ export class GBMinService { // In case of not having HEAR activated before, it is // a upload with no Dialog, so run Auto Save to .gbdrive. - if (!min.cbMap[user.userId]) { + if (!min.cbMap[user.userId] && autoSave) { const t = new SystemKeywords(); GBLog.info(`BASIC (${min.botId}): Upload done for ${attachmentData.fileName}.`); const handle = WebAutomationServices.cyrb53(min.botId + attachmentData.fileName); @@ -1358,7 +1358,7 @@ export class GBMinService { return; } } - + const notes = min.core.getParam(min.instance, 'Notes', null); await this.handleUploads(min, step, user, params, notes != null); From 6e74e86b50324ece96fd0c66277154bf2b924c48 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Mon, 11 Sep 2023 13:39:11 -0300 Subject: [PATCH 41/68] fix(all): Fix search on others bots than boot. --- packages/core.gbapp/services/GBConversationalService.ts | 9 ++++----- packages/core.gbapp/services/GBCoreService.ts | 4 +--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/core.gbapp/services/GBConversationalService.ts b/packages/core.gbapp/services/GBConversationalService.ts index 235cf75f2..07dc7c135 100644 --- a/packages/core.gbapp/services/GBConversationalService.ts +++ b/packages/core.gbapp/services/GBConversationalService.ts @@ -1068,14 +1068,13 @@ export class GBConversationalService { let locale = min.core.getParam( min.instance, 'Default User Language', - GBConfigService.get('DEFAULT_USER_LANGUAGE') - ); + GBConfigService.get('DEFAULT_USER_LANGUAGE')).toLowerCase(); const detectLanguage = min.core.getParam( min.instance, 'Language Detector', - GBConfigService.getBoolean('LANGUAGE_DETECTOR') - ) === 'true'; + GBConfigService.getBoolean('LANGUAGE_DETECTOR')).toLowerCase() + === 'true'; locale = user.locale; if (text != '' && detectLanguage && !locale) { locale = await min.conversationalService.getLanguage(min, text); @@ -1090,7 +1089,7 @@ export class GBConversationalService { const contentLocale = min.core.getParam( min.instance, 'Default Content Language', - GBConfigService.get('DEFAULT_CONTENT_LANGUAGE') + GBConfigService.get('DEFAULT_CONTENT_LANGUAGE').toLowerCase() ); text = await min.conversationalService.translate(min, text, contentLocale); GBLog.verbose(`Translated text (processMessageActivity): ${text}.`); diff --git a/packages/core.gbapp/services/GBCoreService.ts b/packages/core.gbapp/services/GBCoreService.ts index b47b2875f..e161cebc9 100644 --- a/packages/core.gbapp/services/GBCoreService.ts +++ b/packages/core.gbapp/services/GBCoreService.ts @@ -696,9 +696,7 @@ ENDPOINT_UPDATE=true value = instance['dataValues'][name]; if (value === null) { const minBoot = GBServer.globals.minBoot as any; - if (minBoot.instance && minBoot.instance.datavalues) { - value = minBoot.instance.datavalues[name]; - } + value = minBoot.instance[name]; } } From 0d345c605006dd1d11c4285f25e689ff0d7c7869 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Mon, 11 Sep 2023 13:47:36 -0300 Subject: [PATCH 42/68] fix(all): Fix search on others bots than boot. --- packages/core.gbapp/services/GBConversationalService.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/core.gbapp/services/GBConversationalService.ts b/packages/core.gbapp/services/GBConversationalService.ts index 07dc7c135..217a738b1 100644 --- a/packages/core.gbapp/services/GBConversationalService.ts +++ b/packages/core.gbapp/services/GBConversationalService.ts @@ -1068,13 +1068,14 @@ export class GBConversationalService { let locale = min.core.getParam( min.instance, 'Default User Language', - GBConfigService.get('DEFAULT_USER_LANGUAGE')).toLowerCase(); + GBConfigService.get('DEFAULT_USER_LANGUAGE')); + const detectLanguage = min.core.getParam( min.instance, 'Language Detector', - GBConfigService.getBoolean('LANGUAGE_DETECTOR')).toLowerCase() - === 'true'; + false) != false; + locale = user.locale; if (text != '' && detectLanguage && !locale) { locale = await min.conversationalService.getLanguage(min, text); @@ -1089,7 +1090,7 @@ export class GBConversationalService { const contentLocale = min.core.getParam( min.instance, 'Default Content Language', - GBConfigService.get('DEFAULT_CONTENT_LANGUAGE').toLowerCase() + GBConfigService.get('DEFAULT_CONTENT_LANGUAGE') ); text = await min.conversationalService.translate(min, text, contentLocale); GBLog.verbose(`Translated text (processMessageActivity): ${text}.`); From 2c836e71b33eb3cc7ce44da9577b9906e2add407 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 13 Sep 2023 18:39:36 -0300 Subject: [PATCH 43/68] fix(all): Fix search on others bots than boot. --- packages/kb.gbapp/services/KBService.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index f685824ea..a046796e3 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -585,16 +585,6 @@ export class KBService implements IGBKBService { } }); - await GuaribasQuestion.destroy({ - where: { instanceId: min.instance.instanceId } - }); - await GuaribasAnswer.destroy({ - where: { instanceId: min.instance.instanceId } - }); - await GuaribasSubject.destroy({ - where: { instanceId: min.instance.instanceId} - }); - const answersCreated = await GuaribasAnswer.bulkCreate(answers); let i = 0; @@ -677,6 +667,20 @@ export class KBService implements IGBKBService { const subjectFile = urlJoin(localPath, 'subjects.json'); const menuFile = urlJoin(localPath, 'menu.xlsx'); + // Bot KB store clean up. + + await GuaribasQuestion.destroy({ + where: { instanceId: min.instance.instanceId } + }); + await GuaribasAnswer.destroy({ + where: { instanceId: min.instance.instanceId } + }); + await GuaribasSubject.destroy({ + where: { instanceId: min.instance.instanceId} + }); + + // Imports menu.xlsx if any. + if (Fs.existsSync(subjectFile) || Fs.existsSync(menuFile)) { await this.importSubjectFile(packageStorage.packageId, subjectFile, menuFile, instance); } From 7e3b273a898a2064368291eec2e2a7289e66ffd3 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 13 Sep 2023 18:53:45 -0300 Subject: [PATCH 44/68] fix(kb.gbapp): Fix importing packages. --- packages/kb.gbapp/services/KBService.ts | 26 +++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index a046796e3..2c0a11212 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -667,18 +667,6 @@ export class KBService implements IGBKBService { const subjectFile = urlJoin(localPath, 'subjects.json'); const menuFile = urlJoin(localPath, 'menu.xlsx'); - // Bot KB store clean up. - - await GuaribasQuestion.destroy({ - where: { instanceId: min.instance.instanceId } - }); - await GuaribasAnswer.destroy({ - where: { instanceId: min.instance.instanceId } - }); - await GuaribasSubject.destroy({ - where: { instanceId: min.instance.instanceId} - }); - // Imports menu.xlsx if any. if (Fs.existsSync(subjectFile) || Fs.existsSync(menuFile)) { @@ -1031,6 +1019,20 @@ export class KBService implements IGBKBService { const packageName = Path.basename(localPath); const instance = await core.loadInstanceByBotId(min.botId); GBLog.info(`[GBDeployer] Importing: ${localPath}`); + + // Bot KB store clean up. + + await GuaribasQuestion.destroy({ + where: { instanceId: min.instance.instanceId } + }); + await GuaribasAnswer.destroy({ + where: { instanceId: min.instance.instanceId } + }); + await GuaribasSubject.destroy({ + where: { instanceId: min.instance.instanceId} + }); + + const p = await deployer.deployPackageToStorage(instance.instanceId, packageName); await this.importKbPackage(min, localPath, p, instance); GBDeployer.mountGBKBAssets(packageName, min.botId, localPath); From 71e5074a9b4dd4a72cd6153e2e425958bc613948 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 13 Sep 2023 19:34:38 -0300 Subject: [PATCH 45/68] fix(kb.gbapp): Fix importing packages. --- packages/kb.gbapp/services/KBService.ts | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index 2c0a11212..7aaa387d9 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -1020,18 +1020,7 @@ export class KBService implements IGBKBService { const instance = await core.loadInstanceByBotId(min.botId); GBLog.info(`[GBDeployer] Importing: ${localPath}`); - // Bot KB store clean up. - - await GuaribasQuestion.destroy({ - where: { instanceId: min.instance.instanceId } - }); - await GuaribasAnswer.destroy({ - where: { instanceId: min.instance.instanceId } - }); - await GuaribasSubject.destroy({ - where: { instanceId: min.instance.instanceId} - }); - + const p = await deployer.undeployPackageFromLocalPath(instance, packageName); const p = await deployer.deployPackageToStorage(instance.instanceId, packageName); await this.importKbPackage(min, localPath, p, instance); From b305fa2e6bb60fb412ef103e63a321a0bc18ebbf Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 13 Sep 2023 19:37:04 -0300 Subject: [PATCH 46/68] fix(kb.gbapp): Fix importing packages. --- packages/kb.gbapp/services/KBService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index 7aaa387d9..e392859ae 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -1020,7 +1020,7 @@ export class KBService implements IGBKBService { const instance = await core.loadInstanceByBotId(min.botId); GBLog.info(`[GBDeployer] Importing: ${localPath}`); - const p = await deployer.undeployPackageFromLocalPath(instance, packageName); + await deployer.undeployPackageFromLocalPath(instance, packageName); const p = await deployer.deployPackageToStorage(instance.instanceId, packageName); await this.importKbPackage(min, localPath, p, instance); From 52e0f46f5ea60981c8e295d3004cdf7187ad758e Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 13 Sep 2023 19:42:45 -0300 Subject: [PATCH 47/68] fix(kb.gbapp): Fix importing packages. --- packages/core.gbapp/services/GBDeployer.ts | 2 +- packages/kb.gbapp/services/KBService.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 4f3525d13..9c7c54222 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -560,7 +560,7 @@ export class GBDeployer implements IGBDeployer { }); } public async deployPackage(min: GBMinInstance, localPath: string) { - // TODO: @alanperdomo: Adjust interface mismatch. + // TODO: Adjust interface mismatch. } /** * Deploys a folder into the bot storage. diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index e392859ae..705f731bb 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -1020,8 +1020,8 @@ export class KBService implements IGBKBService { const instance = await core.loadInstanceByBotId(min.botId); GBLog.info(`[GBDeployer] Importing: ${localPath}`); - await deployer.undeployPackageFromLocalPath(instance, packageName); - + await deployer.undeployPackageFromLocalPath(instance, localPath); + const p = await deployer.deployPackageToStorage(instance.instanceId, packageName); await this.importKbPackage(min, localPath, p, instance); GBDeployer.mountGBKBAssets(packageName, min.botId, localPath); From 872f5ec1d8507a557073ca1b940618ef18b44f68 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 13 Sep 2023 21:02:33 -0300 Subject: [PATCH 48/68] fix(kb.gbapp): Fix importing packages. --- packages/core.gbapp/services/GBDeployer.ts | 16 ++++++++++++++++ packages/kb.gbapp/services/KBService.ts | 5 ++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 9c7c54222..bd2d38369 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -662,6 +662,22 @@ export class GBDeployer implements IGBDeployer { } } + + /** + * Removes the package from the storage and local work folders. + */ + public async undeployPackageFromPackageName(instance: IGBInstance, packageName: string) { + // Gets information about the package. + + const packageType = Path.extname(packageName); + const p = await this.getStoragePackageByName(instance.instanceId, packageName); + + const path = DialogKeywords.getGBAIPath(instance.botId, null, packageName); + const localFolder = Path.join('work', path); + + return await this.undeployPackageFromLocalPath(instance, localFolder); + } + /** * Removes the package from the storage and local work folders. */ diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index 705f731bb..9ac3936b9 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -1019,9 +1019,8 @@ export class KBService implements IGBKBService { const packageName = Path.basename(localPath); const instance = await core.loadInstanceByBotId(min.botId); GBLog.info(`[GBDeployer] Importing: ${localPath}`); - - await deployer.undeployPackageFromLocalPath(instance, localPath); - + + await deployer['undeployPackageFromPackageName'](instance, packageName); const p = await deployer.deployPackageToStorage(instance.instanceId, packageName); await this.importKbPackage(min, localPath, p, instance); GBDeployer.mountGBKBAssets(packageName, min.botId, localPath); From a665279cbf52882d847a4e0b491ef1d9e0980ac5 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Thu, 14 Sep 2023 00:09:31 -0300 Subject: [PATCH 49/68] fix(kb.gbapp): Fix importing packages. --- packages/core.gbapp/services/GBDeployer.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index bd2d38369..7d755ea02 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -700,8 +700,11 @@ export class GBDeployer implements IGBDeployer { const service = new KBService(this.core.sequelize); rimraf.sync(localPath); - return await service.undeployKbFromStorage(instance, this, p.packageId); + if (p){ + await service.undeployKbFromStorage(instance, this, p.packageId); + } + return; case '.gbui': break; From 7be2e50c37fc7176333ae525d20259587fe6752e Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Thu, 14 Sep 2023 00:19:33 -0300 Subject: [PATCH 50/68] fix(kb.gbapp): Fix importing packages. --- packages/admin.gbapp/services/GBAdminService.ts | 1 + packages/kb.gbapp/services/KBService.ts | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/admin.gbapp/services/GBAdminService.ts b/packages/admin.gbapp/services/GBAdminService.ts index c8648f8db..7a70a5055 100644 --- a/packages/admin.gbapp/services/GBAdminService.ts +++ b/packages/admin.gbapp/services/GBAdminService.ts @@ -170,6 +170,7 @@ export class GBAdminService implements IGBAdminService { // .gbot packages are handled using storage API, so no download // of local resources is required. const gbai = DialogKeywords.getGBAIPath(min.instance.botId); + await deployer['undeployPackageFromPackageName'](min.instance, packageName); await deployer['downloadFolder'](min, Path.join('work', `${gbai}`), Path.basename(localFolder)); diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index 9ac3936b9..dc9ba736e 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -1019,8 +1019,7 @@ export class KBService implements IGBKBService { const packageName = Path.basename(localPath); const instance = await core.loadInstanceByBotId(min.botId); GBLog.info(`[GBDeployer] Importing: ${localPath}`); - - await deployer['undeployPackageFromPackageName'](instance, packageName); + const p = await deployer.deployPackageToStorage(instance.instanceId, packageName); await this.importKbPackage(min, localPath, p, instance); GBDeployer.mountGBKBAssets(packageName, min.botId, localPath); From 6924ab1dad69d71c7f84791ec817ce5ae1050458 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Thu, 14 Sep 2023 12:02:53 -0300 Subject: [PATCH 51/68] fix(kb.gbapp): Fix importing packages. --- packages/admin.gbapp/services/GBAdminService.ts | 2 +- packages/core.gbapp/services/GBDeployer.ts | 10 +++++++++- packages/kb.gbapp/services/KBService.ts | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/admin.gbapp/services/GBAdminService.ts b/packages/admin.gbapp/services/GBAdminService.ts index 7a70a5055..df74e5c21 100644 --- a/packages/admin.gbapp/services/GBAdminService.ts +++ b/packages/admin.gbapp/services/GBAdminService.ts @@ -170,7 +170,7 @@ export class GBAdminService implements IGBAdminService { // .gbot packages are handled using storage API, so no download // of local resources is required. const gbai = DialogKeywords.getGBAIPath(min.instance.botId); - await deployer['undeployPackageFromPackageName'](min.instance, packageName); + await deployer['downloadFolder'](min, Path.join('work', `${gbai}`), Path.basename(localFolder)); diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 7d755ea02..7c552bc88 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -662,7 +662,15 @@ export class GBDeployer implements IGBDeployer { } } - + /** + * Removes the package local files from cache. + */ + public async cleanupPackage(instance: IGBInstance, packageName: string) { + const path = DialogKeywords.getGBAIPath(instance.botId, null, packageName); + const localFolder = Path.join('work', path); + rimraf.sync(localFolder); + } + /** * Removes the package from the storage and local work folders. */ diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index dc9ba736e..66a9af2b3 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -1019,7 +1019,7 @@ export class KBService implements IGBKBService { const packageName = Path.basename(localPath); const instance = await core.loadInstanceByBotId(min.botId); GBLog.info(`[GBDeployer] Importing: ${localPath}`); - + await deployer['cleanupPackage'](min.instance, packageName); const p = await deployer.deployPackageToStorage(instance.instanceId, packageName); await this.importKbPackage(min, localPath, p, instance); GBDeployer.mountGBKBAssets(packageName, min.botId, localPath); From ccc5777184f88d94c6f9590282a9c58fe67c16a4 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Thu, 14 Sep 2023 12:10:47 -0300 Subject: [PATCH 52/68] fix(kb.gbapp): Fix importing packages. --- packages/admin.gbapp/services/GBAdminService.ts | 6 +++++- packages/kb.gbapp/services/KBService.ts | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/admin.gbapp/services/GBAdminService.ts b/packages/admin.gbapp/services/GBAdminService.ts index df74e5c21..a2a450485 100644 --- a/packages/admin.gbapp/services/GBAdminService.ts +++ b/packages/admin.gbapp/services/GBAdminService.ts @@ -170,7 +170,11 @@ export class GBAdminService implements IGBAdminService { // .gbot packages are handled using storage API, so no download // of local resources is required. const gbai = DialogKeywords.getGBAIPath(min.instance.botId); - + + if (packageType === 'gbkb') { + await deployer['cleanupPackage'](min.instance, packageName); + } + await deployer['downloadFolder'](min, Path.join('work', `${gbai}`), Path.basename(localFolder)); diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index 66a9af2b3..17614f808 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -1019,7 +1019,7 @@ export class KBService implements IGBKBService { const packageName = Path.basename(localPath); const instance = await core.loadInstanceByBotId(min.botId); GBLog.info(`[GBDeployer] Importing: ${localPath}`); - await deployer['cleanupPackage'](min.instance, packageName); + const p = await deployer.deployPackageToStorage(instance.instanceId, packageName); await this.importKbPackage(min, localPath, p, instance); GBDeployer.mountGBKBAssets(packageName, min.botId, localPath); From 3bd95cbb9e716c87eca9b332dd939e180481d8aa Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 16 Sep 2023 09:41:31 -0300 Subject: [PATCH 53/68] fix(kb.gbapp): Fix importing packages. --- .../services/GBConversationalService.ts | 8 ++++- packages/core.gbapp/services/GBCoreService.ts | 4 +-- .../services/WhatsappDirectLine.ts | 36 ++++++++++--------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/packages/core.gbapp/services/GBConversationalService.ts b/packages/core.gbapp/services/GBConversationalService.ts index 217a738b1..8e577baa0 100644 --- a/packages/core.gbapp/services/GBConversationalService.ts +++ b/packages/core.gbapp/services/GBConversationalService.ts @@ -1059,7 +1059,13 @@ export class GBConversationalService { // If it is a group, spells and sends them back. const group = step.context.activity['group']; - if (textProcessed !== text && group) { + + const groupSpell = group ? await min.core.getParam( + min.instance, + 'Group Spell', + false): false; + + if (textProcessed !== text && group && groupSpell) { await min.whatsAppDirectLine.sendToDevice(group, `Spell: ${text}`); } diff --git a/packages/core.gbapp/services/GBCoreService.ts b/packages/core.gbapp/services/GBCoreService.ts index e161cebc9..0469c5eb2 100644 --- a/packages/core.gbapp/services/GBCoreService.ts +++ b/packages/core.gbapp/services/GBCoreService.ts @@ -683,13 +683,13 @@ ENDPOINT_UPDATE=true value = params ? params[name] : defaultValue; } if (typeof defaultValue === 'boolean') { - return new Boolean(value ? value.toString().toLowerCase() === 'true' : defaultValue); + return new Boolean(value ? value.toString().toLowerCase() === 'true' : defaultValue).valueOf(); } if (typeof defaultValue === 'string') { return value ? value : defaultValue; } if (typeof defaultValue === 'number') { - return new Number(value ? value : defaultValue ? defaultValue : 0); + return new Number(value ? value : defaultValue ? defaultValue : 0).valueOf(); } if (instance['dataValues'] && !value) { diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 5900ed228..4b8ef3c57 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -106,10 +106,10 @@ export class WhatsappDirectLine extends GBService { whatsappServiceKey === 'internal' ? 'GeneralBots' : whatsappServiceNumber.indexOf(';') > -1 - ? 'maytapi' - : whatsappServiceKey !== 'internal' - ? 'graphapi' - : 'chatapi'; + ? 'maytapi' + : whatsappServiceKey !== 'internal' + ? 'graphapi' + : 'chatapi'; this.groupId = groupId; } @@ -419,10 +419,12 @@ export class WhatsappDirectLine extends GBService { // Bot name must be specified on config. if (botGroupID === group) { + // Shortcut has been mentioned? let found = false; parts.forEach(e1 => { + botShortcuts.forEach(e2 => { if (e1 === e2 && !found) { found = true; @@ -443,17 +445,18 @@ export class WhatsappDirectLine extends GBService { } }); } - - // Ignore group messages without the mention to Bot. - - let smsServiceNumber = this.min.core.getParam(this.min.instance, 'whatsappServiceNumber', null); - if (smsServiceNumber && !answerText) { - smsServiceNumber = smsServiceNumber.replace('+', ''); - if (!message.body.startsWith('@' + smsServiceNumber)) { - return; - } - } }); + + // Ignore group messages without the mention to Bot. + + let botNumber = this.min.core.getParam(this.min.instance, 'Bot Number', null); + if (botNumber && !answerText && !found) { + botNumber = botNumber.replace('+', ''); + if (!message.body.startsWith('@' + botNumber)) { + + return; + } + } } } @@ -691,8 +694,7 @@ export class WhatsappDirectLine extends GBService { await this.printMessages(response.obj.activities, conversationId, from, fromName); } catch (err) { GBLog.error( - `Error calling printMessages on Whatsapp channel ${err.data === undefined ? err : err.data} ${ - err.errObj ? err.errObj.message : '' + `Error calling printMessages on Whatsapp channel ${err.data === undefined ? err : err.data} ${err.errObj ? err.errObj.message : '' }` ); } @@ -1037,7 +1039,7 @@ export class WhatsappDirectLine extends GBService { p => p.instance.botId.toLowerCase() === text.toLowerCase() )[0]; - + GBLog.info(`A WhatsApp mobile requested instance for: ${botId}.`); let urlMin: any = GBServer.globals.minInstances.filter(p => p.instance.botId === botId)[0]; From b1ecdc01ef85773ced5376640b77cba9961d3599 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 16 Sep 2023 17:41:36 -0300 Subject: [PATCH 54/68] fix(kb.gbapp): API after new Bot #370. --- packages/basic.gblib/index.ts | 2 +- packages/core.gbapp/services/GBDeployer.ts | 1 + packages/core.gbapp/services/GBMinService.ts | 190 +++++++++++-------- src/RootData.ts | 1 + 4 files changed, 113 insertions(+), 81 deletions(-) diff --git a/packages/basic.gblib/index.ts b/packages/basic.gblib/index.ts index 78bad742c..b9aae1b0f 100644 --- a/packages/basic.gblib/index.ts +++ b/packages/basic.gblib/index.ts @@ -60,7 +60,7 @@ export function createKoaHttpServer( app.use(koaBody.koaBody({ multipart: true })); app.use(middleware); const server = app.listen(port); - const SERVER_TIMEOUT = 60 * 60 * 24 * 1000; // Equals to client RPC set . + const SERVER_TIMEOUT = 60 * 60 * 24 * 1000; // Equals to client RPC set. server.timeout = SERVER_TIMEOUT; return { diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 7c552bc88..85e301a01 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -310,6 +310,7 @@ export class GBDeployer implements IGBDeployer { // Makes available bot to the channels and .gbui interfaces. await GBServer.globals.minService.mountBot(instance); + await GBServer.globals.minService.ensureAPI(); } // Saves final instance object and returns it. diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index ee3fbb13b..d261b060f 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -148,6 +148,11 @@ export class GBMinService { this.deployer = deployer; } + + public async enableAPI(min: GBMinInstance) { + + } + /** * Constructs a new minimal instance for each bot. */ @@ -167,51 +172,10 @@ export class GBMinService { GBServer.globals.server.get('/instances/:botId', this.handleGetInstanceForClient.bind(this)); } - - function getRemoteId(ctx: Koa.Context) { - return '1'; // share a single session for now, real impl could use cookies or some other meaning for HTTP sessions - } - - GBLogEx.info(0, 'Loading General Bots API...'); - - let proxies = {}; - await CollectionUtil.asyncForEach(instances, async instance => { - const proxy = { - dk: new DialogKeywords(), - wa: new WebAutomationServices(), - sys: new SystemKeywords(), - dbg: new DebuggerService(), - img: new ImageProcessingServices() - }; - proxies[instance.botId] = proxy; - }); - - const opts = { - pingSendTimeout: null, - keepAliveTimeout: null, - listeners: { - unsubscribed(subscriptions: number): void {}, - subscribed(subscriptions: number): void {}, - disconnected(remoteId: string, connections: number): void {}, - connected(remoteId: string, connections: number): void {}, - messageIn(...params): void { - params.shift(); - GBLogEx.verbose(0, '[IN] ' + params); - }, - messageOut(...params): void { - params.shift(); - GBLogEx.verbose(0, '[OUT] ' + params); - } - } - }; - - GBServer.globals.server.dk = createRpcServer( - proxies, - createKoaHttpServer(GBVMService.API_PORT, getRemoteId, { prefix: `api/v3` }), - opts - ); - + await this.ensureAPI(); + // Calls mountBot event to all bots. + let i = 1; if (instances.length > 1) { @@ -292,7 +256,7 @@ export class GBMinService { /** * Unmounts the bot web site (default.gbui) secure domain, if any. */ - public async unloadDomain(instance: IGBInstance) {} + public async unloadDomain(instance: IGBInstance) { } /** * Mount the instance by creating an BOT Framework bot object, @@ -579,9 +543,8 @@ export class GBMinService { min.instance.authenticatorTenant, '/oauth2/authorize' ); - authorizationUrl = `${authorizationUrl}?response_type=code&client_id=${ - min.instance.marketplaceId - }&redirect_uri=${urlJoin(process.env.BOT_URL, min.instance.botId, 'token')}`; + authorizationUrl = `${authorizationUrl}?response_type=code&client_id=${min.instance.marketplaceId + }&redirect_uri=${urlJoin(process.env.BOT_URL, min.instance.botId, 'token')}`; GBLog.info(`HandleOAuthRequests: ${authorizationUrl}.`); res.redirect(authorizationUrl); }); @@ -769,14 +732,14 @@ export class GBMinService { } const group = min.core.getParam(min.instance, 'WhatsApp Group ID', null); - + WhatsappDirectLine.botGroups[min.botId] = group; - + const minBoot = GBServer.globals.minBoot as any; - + // If there is WhatsApp configuration specified, initialize // infrastructure objects. - + if (min.instance.whatsappServiceKey) { min.whatsAppDirectLine = new WhatsappDirectLine( min, @@ -799,23 +762,23 @@ export class GBMinService { minBoot.instance.whatsappServiceNumber, minBoot.instance.whatsappServiceUrl, group - ); - await min.whatsAppDirectLine.setup(false); - } + ); + await min.whatsAppDirectLine.setup(false); } - - // Builds bot numbers map in WhatsAppDirectLine globals. + } - let botNumber = min.core.getParam(min.instance, 'Bot Number', null); - if (botNumber){ - WhatsappDirectLine.botsByNumber[botNumber] = min.whatsAppDirectLine; - } + // Builds bot numbers map in WhatsAppDirectLine globals. + + let botNumber = min.core.getParam(min.instance, 'Bot Number', null); + if (botNumber) { + WhatsappDirectLine.botsByNumber[botNumber] = min.whatsAppDirectLine; + } + + // Setups default BOT Framework dialogs. - // Setups default BOT Framework dialogs. - min.userProfile = conversationState.createProperty('userProfile'); const dialogState = conversationState.createProperty('dialogState'); - + min.dialogs = new DialogSet(dialogState); min.dialogs.add(new TextPrompt('textPrompt')); min.dialogs.add(new AttachmentPrompt('attachmentPrompt')); @@ -919,7 +882,7 @@ export class GBMinService { const step = await min.dialogs.createContext(context); step.context.activity.locale = 'pt-BR'; - + const member = context.activity.from; const sec = new SecService(); const user = await sec.ensureUser(instance.instanceId, member.id, member.name, '', 'web', member.name, null); @@ -947,7 +910,7 @@ export class GBMinService { await sec.setParam(userId, 'wholeWord', true); await sec.setParam(userId, 'theme', 'white'); await sec.setParam(userId, 'maxColumns', 40); - + // This same event is dispatched either to all participants // including the bot, that is filtered bellow. @@ -1077,9 +1040,8 @@ export class GBMinService { await this.processEventActivity(min, user, context, step); } } catch (error) { - const msg = `ERROR: ${error.message} ${error.stack} ${error.error ? error.error.body : ''} ${ - error.error ? (error.error.stack ? error.error.stack : '') : '' - }`; + const msg = `ERROR: ${error.message} ${error.stack} ${error.error ? error.error.body : ''} ${error.error ? (error.error.stack ? error.error.stack : '') : '' + }`; GBLog.error(msg); await min.conversationalService.sendText( @@ -1184,8 +1146,7 @@ export class GBMinService { return utterance.match(Messages.global_quit); } - private async handleUploads(min, step, user, params, autoSave) - { + private async handleUploads(min, step, user, params, autoSave) { // Prepare Promises to download each attachment and then execute each Promise. if ( @@ -1411,19 +1372,19 @@ export class GBMinService { } else { // Removes unwanted chars in input text. - + step.context.activity['originalText'] = context.activity.text; const text = await GBConversationalService.handleText(min, user, step, context.activity.text); step.context.activity['originalText'] step.context.activity['text'] = text; - - if (notes && text && text !== "") { + + if (notes && text && text !== "") { const sys = new SystemKeywords(); - await sys.note({pid, text}); + await sys.note({ pid, text }); await step.context.sendActivity('OK.'); return; - } + } // Checks for bad words on input text. @@ -1468,9 +1429,8 @@ export class GBMinService { try { await step.continueDialog(); } catch (error) { - const msg = `ERROR: ${error.message} ${error.stack} ${error.error ? error.error.body : ''} ${ - error.error ? (error.error.stack ? error.error.stack : '') : '' - }`; + const msg = `ERROR: ${error.message} ${error.stack} ${error.error ? error.error.body : ''} ${error.error ? (error.error.stack ? error.error.stack : '') : '' + }`; GBLog.error(msg); await min.conversationalService.sendText( min, @@ -1511,4 +1471,74 @@ export class GBMinService { } } } -} + + public async ensureAPI() { + + const instances = GBServer.globals.minInstances; + + + function getRemoteId(ctx: Koa.Context) { + return '1'; // Each bot has its own API. + } + + const close = async () => { + return new Promise(resolve => { + if (GBServer.globals.server.apiServer) { + GBServer.globals.server.apiServer.close( + cb => { + resolve(true); + GBLogEx.info(0, 'Reloading General Bots API...'); + } + ); + } + else{ + resolve(true); + GBLogEx.info(0, 'Loading General Bots API...'); + } + }); + }; + + await close(); + + let proxies = {}; + await CollectionUtil.asyncForEach(instances, async instance => { + const proxy = { + dk: new DialogKeywords(), + wa: new WebAutomationServices(), + sys: new SystemKeywords(), + dbg: new DebuggerService(), + img: new ImageProcessingServices() + }; + proxies[instance.botId] = proxy; + }); + + const opts = { + pingSendTimeout: null, + keepAliveTimeout: null, + listeners: { + unsubscribed(subscriptions: number): void { }, + subscribed(subscriptions: number): void { }, + disconnected(remoteId: string, connections: number): void { }, + connected(remoteId: string, connections: number): void { }, + messageIn(...params): void { + params.shift(); + GBLogEx.verbose(0, '[IN] ' + params); + }, + messageOut(...params): void { + params.shift(); + GBLogEx.verbose(0, '[OUT] ' + params); + } + } + }; + + GBServer.globals.server.apiServer = createKoaHttpServer(GBVMService.API_PORT, getRemoteId, { prefix: `api/v3` }); + + createRpcServer( + proxies, + GBServer.globals.server.apiServer, + opts + ); + + } + +} \ No newline at end of file diff --git a/src/RootData.ts b/src/RootData.ts index f07375796..a886ff0ed 100644 --- a/src/RootData.ts +++ b/src/RootData.ts @@ -48,6 +48,7 @@ export class RootData { public publicAddress: string; // URI for BotServer. public server: any; // Express reference. public httpsServer: any; // Express reference (HTTPS). + public apiServer: any; // Koa reference (HTTPS) for GB API (isolated from /). public sysPackages: any[]; // Loaded system package list. public appPackages: any[]; // Loaded .gbapp package list. public minService: GBMinService; // Minimalist service core. From 7df967167ac57ffe6f1eea6453a055040d7381b0 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 16 Sep 2023 17:49:16 -0300 Subject: [PATCH 55/68] fix(kb.gbapp): API after new Bot #370. --- packages/azuredeployer.gbapp/services/AzureDeployerService.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts index 3d677905c..6d9374643 100644 --- a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts +++ b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts @@ -571,8 +571,7 @@ export class AzureDeployerService implements IGBInstallationDeployer { luisKey: nlpKey, msaAppId: appId, msaAppPassword: appPassword, - enabledChannels: ['webchat', 'skype'], //, "facebook"], - configuredChannels: ['webchat', 'skype'] //, "facebook"] + configuredChannels: ['webchat'] } }; From c68cc7cb601ba5a0e3229997b3834341812e76b9 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 16 Sep 2023 17:54:47 -0300 Subject: [PATCH 56/68] fix(kb.gbapp): API after new Bot #370. --- packages/azuredeployer.gbapp/services/AzureDeployerService.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts index 6d9374643..8f78b0cc6 100644 --- a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts +++ b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts @@ -570,8 +570,7 @@ export class AzureDeployerService implements IGBInstallationDeployer { luisAppIds: [nlpAppId], luisKey: nlpKey, msaAppId: appId, - msaAppPassword: appPassword, - configuredChannels: ['webchat'] + msaAppPassword: appPassword } }; From fa0c2d4cd3932c0ed8cca2096d4fdaa09c67ef22 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 17 Sep 2023 21:40:34 -0300 Subject: [PATCH 57/68] fix(kb.gbapp): #361 GPT functions in Word. --- packages/basic.gblib/services/GBVMService.ts | 12 ++++- packages/core.gbapp/services/GBMinService.ts | 34 ++++++++++--- packages/gpt.gblib/services/ChatServices.ts | 50 +++++++++++++++++++- 3 files changed, 86 insertions(+), 10 deletions(-) diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index d217c0420..a66c994c6 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -389,7 +389,8 @@ export class GBVMService extends GBService { step, user: GuaribasUser, deployer: GBDeployer, - debug: boolean = false + debug: boolean = false, + params = [] ) { // Creates a class DialogKeywords which is the *this* pointer // in BASIC. @@ -414,6 +415,14 @@ export class GBVMService extends GBService { } } + // Adds params as variables to be added later as global objects.. + + let keys = Object.keys(params); + for (let j = 0; j < keys.length; j++) { + variables[keys[j]] = params[keys[j]]; + } + + const botId = min.botId; const path = DialogKeywords.getGBAIPath(min.botId, `gbdialog`); const gbdialogPath = urlJoin(process.cwd(), 'work', path); @@ -489,7 +498,6 @@ export class GBVMService extends GBService { } } catch (error) { throw new Error(`BASIC RUNTIME ERR: ${error.message ? error.message : error}\n Stack:${error.stack}`); - } finally { } return result; diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index d261b060f..99092938c 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -172,8 +172,6 @@ export class GBMinService { GBServer.globals.server.get('/instances/:botId', this.handleGetInstanceForClient.bind(this)); } - await this.ensureAPI(); - // Calls mountBot event to all bots. let i = 1; @@ -209,6 +207,10 @@ export class GBMinService { this.bar1.stop(); } + // Loads API. + + await this.ensureAPI(); + // Loads schedules. GBLog.info(`Loading SET SCHEDULE entries...`); @@ -1474,9 +1476,8 @@ export class GBMinService { public async ensureAPI() { - const instances = GBServer.globals.minInstances; + const mins = GBServer.globals.minInstances; - function getRemoteId(ctx: Koa.Context) { return '1'; // Each bot has its own API. } @@ -1500,16 +1501,35 @@ export class GBMinService { await close(); + let proxies = {}; - await CollectionUtil.asyncForEach(instances, async instance => { + await CollectionUtil.asyncForEach(mins, async min => { + + let dialogs = {}; + await CollectionUtil.asyncForEach(Object.values(min.scriptMap), async script => { + + + const f = new Function() + + dialogs[script] = async (data)=> { + let params; + if (data){ + params = JSON.parse(data); + } + + await GBVMService.callVM(script, min, null, null, null, false, params); + } + }); + const proxy = { dk: new DialogKeywords(), wa: new WebAutomationServices(), sys: new SystemKeywords(), dbg: new DebuggerService(), - img: new ImageProcessingServices() + img: new ImageProcessingServices(), + dialogs: dialogs }; - proxies[instance.botId] = proxy; + proxies[min.botId] = proxy; }); const opts = { diff --git a/packages/gpt.gblib/services/ChatServices.ts b/packages/gpt.gblib/services/ChatServices.ts index 839bab85e..31d0b7b7e 100644 --- a/packages/gpt.gblib/services/ChatServices.ts +++ b/packages/gpt.gblib/services/ChatServices.ts @@ -35,8 +35,55 @@ import { GBMinInstance } from 'botlib'; import OpenAI from "openai"; import { ChatGPTAPIBrowser, getOpenAIAuth } from 'chatgpt' +import { CollectionUtil } from 'pragmatismo-io-framework'; export class ChatServices { + + public static async sendMessage(min: GBMinInstance, text: string) { + let key; + if (process.env.OPENAI_KEY) { + key = process.env.OPENAI_KEY; + } + else { + key = min.core.getParam(min.instance, 'Open AI Key', null); + } + + if (!key) { + throw new Error('Open AI Key not configured in .gbot.'); + } + let functions = []; + + await CollectionUtil.asyncForEach(Object.values(min.scriptMap), async script => { + + functions.push({ + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + } }); + }); + + const openai = new OpenAI({ + apiKey: key + }); + const chatCompletion = await openai.chat.completions.create({ + model: "gpt-3.5-turbo", + messages: [{ role: "user", content: text }], + functions: functions + }); + return chatCompletion.choices[0].message.content; + } + + + /** * Generate text * @@ -62,7 +109,8 @@ export class ChatServices { }); const chatCompletion = await openai.chat.completions.create({ model: "gpt-3.5-turbo", - messages: [{ role: "user", content: text }], + messages: [{ role: "user", content: text }] + }); return chatCompletion.choices[0].message.content; } From 111b4230472327e87ca9ed117385239435211496 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Tue, 19 Sep 2023 19:36:05 -0300 Subject: [PATCH 58/68] fix(kb.gbapp): #361 GPT functions in Word. --- packages/basic.gblib/services/GBVMService.ts | 104 ++++++++++++++++--- 1 file changed, 88 insertions(+), 16 deletions(-) diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index a66c994c6..6fdfc49f1 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -89,6 +89,7 @@ export class GBVMService extends GBService { const docxStat = Fs.statSync(urlJoin(folder, wordFile)); const interval = 3000; // If compiled is older 30 seconds, then recompile. let writeVBS = true; + if (Fs.existsSync(fullVbsFile)) { const vbsStat = Fs.statSync(fullVbsFile); if (docxStat['mtimeMs'] < vbsStat['mtimeMs'] + interval) { @@ -102,6 +103,8 @@ export class GBVMService extends GBService { if (writeVBS) { let text = await this.getTextFromWord(folder, wordFile); + // Pre process SET SCHEDULE calls. + const schedule = GBVMService.getSetScheduleKeywordArgs(text); const s = new ScheduleServices(); if (schedule) { @@ -110,10 +113,14 @@ export class GBVMService extends GBService { await s.deleteScheduleIfAny(min, mainName); } text = text.replace(/^\s*SET SCHEDULE (.*)/gim, ''); + + // Write VBS file without pragma keywords. + Fs.writeFileSync(urlJoin(folder, vbsFile), text); } // Process node_modules install. + const node_modules = urlJoin(process.env.PWD, folder, 'node_modules'); if (!Fs.existsSync(node_modules)) { const packageJson = ` @@ -143,7 +150,7 @@ export class GBVMService extends GBService { const fullFilename = urlJoin(folder, filename); if (process.env.DEV_HOTSWAP) { Fs.watchFile(fullFilename, async () => { - await this.translateBASIC(fullFilename, min); + await this.translateBASIC(mainName, fullFilename, min); const parsedCode: string = Fs.readFileSync(jsfile, 'utf8'); min.sandBoxMap[mainName.toLowerCase().trim()] = parsedCode; }); @@ -156,17 +163,17 @@ export class GBVMService extends GBService { const jsStat = Fs.statSync(jsfile); const interval = 30000; // If compiled is older 30 seconds, then recompile. if (compiledAt.isFile() && compiledAt['mtimeMs'] > jsStat['mtimeMs'] + interval) { - await this.translateBASIC(fullFilename, min); + await this.translateBASIC(mainName, fullFilename, min); } } else { - await this.translateBASIC(fullFilename, min); + await this.translateBASIC(mainName, fullFilename, min); } const parsedCode: string = Fs.readFileSync(jsfile, 'utf8'); min.sandBoxMap[mainName.toLowerCase().trim()] = parsedCode; return filename; } - public async translateBASIC(filename: any, min: GBMinInstance) { + public async translateBASIC(mainName, filename: any, min: GBMinInstance) { // Converts General Bots BASIC into regular VBS let basicCode: string = Fs.readFileSync(filename, 'utf8'); @@ -191,7 +198,13 @@ export class GBVMService extends GBService { } } while (include); - let { code, jsonMap } = await this.convert(basicCode); + let { code, map: jsonMap, metadata } = await this.convert(mainName, basicCode); + + // Generates function JSON metadata to be used later. + + const jsonFile = `${filename}.json`; + Fs.writeFileSync(jsonFile, JSON.stringify(metadata)); + const mapFile = `${filename}.map`; Fs.writeFileSync(mapFile, JSON.stringify(jsonMap)); @@ -342,27 +355,70 @@ export class GBVMService extends GBService { return text; } + public static getMetadata(mainName: string, propertiesText, description) { + const properties = []; + + if (propertiesText) { + const getType = asClause => { + if (asClause.indexOf('AS STRING')) { + return 'string'; + } else { + return 'enum'; + } + }; + + for (let i = 0; i < propertiesText.length; i++) { + const propertiesExp = propertiesText[i]; + const t = getType(propertiesExp[2]); + let element = {}; + element['type'] = t; + + if (t === 'enum') { + element['enum'] = propertiesExp[2]; + } else if (t === 'string') { + element['description'] = propertiesExp[2]; + } + + properties.push(element); + } + } + + let json = { + name: `${mainName}`, + description: description ? description[1] : '', + parameters: { + type: 'object', + properties: properties ? properties : [] + } + }; + + return json; + } + /** * Converts General Bots BASIC * * * @param code General Bots BASIC */ - public async convert(code: string) { + public async convert(mainName: string, code: string) { + // Start and End of VB2TS tags of processing. code = process.env.ENABLE_AUTH ? `hear GBLogExin as login\n${code}` : code; var lines = code.split('\n'); const keywords = KeywordsExpressions.getKeywords(); let current = 41; - const map = {}; + const map = {}; + let properties = []; + let description; for (let i = 1; i <= lines.length; i++) { let line = lines[i - 1]; // Remove lines before statments. - line = line.replace(/^\s*\d+\s*/gi,''); + line = line.replace(/^\s*\d+\s*/gi, ''); for (let j = 0; j < keywords.length; j++) { line = line.replace(keywords[j][0], keywords[j][1]); @@ -374,10 +430,27 @@ export class GBVMService extends GBService { current = current + (add ? add : 0); map[i] = current; lines[i - 1] = line; + + // Pre-process static KEYWORDS. + + const params = /^\s*PARAM\s*(.*)\s*AS\s*(.*)/gim; + const param = params.exec(line); + if (param) { + properties.push(param); + } + + const descriptionKeyword = /^\s*DESCRIPTION\s*\"(.*)\"/gim; + let descriptionReg = descriptionKeyword.exec(line); + if (descriptionReg) { + description = descriptionReg[1]; + } } code = `${lines.join('\n')}\n`; - return { code, jsonMap: map }; + + let metadata = GBVMService.getMetadata(mainName, properties, description); + + return { code, map, metadata }; } /** @@ -389,7 +462,7 @@ export class GBVMService extends GBService { step, user: GuaribasUser, deployer: GBDeployer, - debug: boolean = false, + debug: boolean = false, params = [] ) { // Creates a class DialogKeywords which is the *this* pointer @@ -419,22 +492,21 @@ export class GBVMService extends GBService { let keys = Object.keys(params); for (let j = 0; j < keys.length; j++) { - variables[keys[j]] = params[keys[j]]; + variables[keys[j]] = params[keys[j]]; } - const botId = min.botId; const path = DialogKeywords.getGBAIPath(min.botId, `gbdialog`); const gbdialogPath = urlJoin(process.cwd(), 'work', path); const scriptPath = urlJoin(gbdialogPath, `${text}.js`); let code = min.sandBoxMap[text]; - const channel = step? step.context.activity.channelId : 'web'; + const channel = step ? step.context.activity.channelId : 'web'; const pid = GBVMService.createProcessInfo(user, min, channel); const dk = new DialogKeywords(); const sys = new SystemKeywords(); - await dk.setFilter ({pid: pid, value: null }); - + await dk.setFilter({ pid: pid, value: null }); + sandbox['variables'] = variables; sandbox['id'] = sys.getRandomId(); sandbox['username'] = await dk.userName({ pid }); @@ -448,7 +520,7 @@ export class GBVMService extends GBService { sandbox['httpPs'] = ''; sandbox['pid'] = pid; sandbox['contentLocale'] = contentLocale; - sandbox['callTimeout'] = 60 * 60 * 24 * 1000; + sandbox['callTimeout'] = 60 * 60 * 24 * 1000; sandbox['channel'] = channel; sandbox['today'] = await dk.getToday({ pid }); sandbox['now'] = await dk.getNow({ pid }); From 69f2942fcc775ffe4d3804e2e0952a73948fdc80 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 20 Sep 2023 15:25:44 -0300 Subject: [PATCH 59/68] fix(kb.gbapp): #361 GPT functions in Word. --- packages/basic.gblib/services/GBVMService.ts | 4 +-- packages/gpt.gblib/services/ChatServices.ts | 36 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index 6fdfc49f1..2a85e19b2 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -198,7 +198,7 @@ export class GBVMService extends GBService { } } while (include); - let { code, map: jsonMap, metadata } = await this.convert(mainName, basicCode); + let { code, map, metadata } = await this.convert(mainName, basicCode); // Generates function JSON metadata to be used later. @@ -207,7 +207,7 @@ export class GBVMService extends GBService { const mapFile = `${filename}.map`; - Fs.writeFileSync(mapFile, JSON.stringify(jsonMap)); + Fs.writeFileSync(mapFile, JSON.stringify(map)); // Run JS into the GB context. diff --git a/packages/gpt.gblib/services/ChatServices.ts b/packages/gpt.gblib/services/ChatServices.ts index 31d0b7b7e..928256702 100644 --- a/packages/gpt.gblib/services/ChatServices.ts +++ b/packages/gpt.gblib/services/ChatServices.ts @@ -36,6 +36,9 @@ import { GBMinInstance } from 'botlib'; import OpenAI from "openai"; import { ChatGPTAPIBrowser, getOpenAIAuth } from 'chatgpt' import { CollectionUtil } from 'pragmatismo-io-framework'; +import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords'; +import Path from 'path'; +import * as Fs from 'fs'; export class ChatServices { @@ -52,24 +55,21 @@ export class ChatServices { throw new Error('Open AI Key not configured in .gbot.'); } let functions = []; - + + // Adds .gbdialog as functions if any to GPT Functions. + await CollectionUtil.asyncForEach(Object.values(min.scriptMap), async script => { - - functions.push({ - "name": "get_current_weather", - "description": "Get the current weather in a given location", - "parameters": { - "type": "object", - "properties": { - "location": { - "type": "string", - "description": "The city and state, e.g. San Francisco, CA", - }, - "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, - }, - "required": ["location"], - } }); - }); + const path = DialogKeywords.getGBAIPath(min.botId, "gbdialog", null); + const localFolder = Path.join('work', path, `${script}.json`); + + if (Fs.existsSync(localFolder)) { + const func = Fs.readFileSync(localFolder).toJSON(); + functions.push(func); + } + + }); + + // Calls Model. const openai = new OpenAI({ apiKey: key @@ -110,7 +110,7 @@ export class ChatServices { const chatCompletion = await openai.chat.completions.create({ model: "gpt-3.5-turbo", messages: [{ role: "user", content: text }] - + }); return chatCompletion.choices[0].message.content; } From d950a436f92878c4d1f16fc09764355d8231feb4 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Thu, 21 Sep 2023 11:15:09 -0300 Subject: [PATCH 60/68] fix(whatsapp.gblib): Fix in sending numbers with plus sign. --- packages/whatsapp.gblib/services/WhatsappDirectLine.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 4b8ef3c57..1484fe577 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -900,6 +900,7 @@ export class WhatsappDirectLine extends GBService { switch (this.provider) { case 'GeneralBots': + to = to.replace('+', ''); if (to.indexOf('@') == -1) { if (to.length == 18) { to = to + '@g.us'; From 58f56d2ea3fb877f5a745be31db649e4be8b9033 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Thu, 21 Sep 2023 11:20:54 -0300 Subject: [PATCH 61/68] fix(whatsapp.gblib): Fix in sending numbers with plus sign. --- packages/gpt.gblib/services/ChatServices.ts | 36 ++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/gpt.gblib/services/ChatServices.ts b/packages/gpt.gblib/services/ChatServices.ts index 928256702..4a27b772a 100644 --- a/packages/gpt.gblib/services/ChatServices.ts +++ b/packages/gpt.gblib/services/ChatServices.ts @@ -33,7 +33,7 @@ 'use strict'; import { GBMinInstance } from 'botlib'; -import OpenAI from "openai"; +//import OpenAI from "openai"; import { ChatGPTAPIBrowser, getOpenAIAuth } from 'chatgpt' import { CollectionUtil } from 'pragmatismo-io-framework'; import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords'; @@ -71,15 +71,15 @@ export class ChatServices { // Calls Model. - const openai = new OpenAI({ - apiKey: key - }); - const chatCompletion = await openai.chat.completions.create({ - model: "gpt-3.5-turbo", - messages: [{ role: "user", content: text }], - functions: functions - }); - return chatCompletion.choices[0].message.content; + // const openai = new OpenAI({ + // apiKey: key + // }); + // const chatCompletion = await openai.chat.completions.create({ + // model: "gpt-3.5-turbo", + // messages: [{ role: "user", content: text }], + // functions: functions + // }); + // return chatCompletion.choices[0].message.content; } @@ -104,14 +104,14 @@ export class ChatServices { if (!key) { throw new Error('Open AI Key not configured in .gbot.'); } - const openai = new OpenAI({ - apiKey: key - }); - const chatCompletion = await openai.chat.completions.create({ - model: "gpt-3.5-turbo", - messages: [{ role: "user", content: text }] + // const openai = new OpenAI({ + // apiKey: key + // }); + // const chatCompletion = await openai.chat.completions.create({ + // model: "gpt-3.5-turbo", + // messages: [{ role: "user", content: text }] - }); - return chatCompletion.choices[0].message.content; + // }); + // return chatCompletion.choices[0].message.content; } } From 7e3a29b1a03ca8d393e8728861e952f127243c07 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Thu, 21 Sep 2023 11:24:08 -0300 Subject: [PATCH 62/68] fix(whatsapp.gblib): Fix in sending numbers with plus sign. --- packages/gpt.gblib/services/ChatServices.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gpt.gblib/services/ChatServices.ts b/packages/gpt.gblib/services/ChatServices.ts index 4a27b772a..503eb5b43 100644 --- a/packages/gpt.gblib/services/ChatServices.ts +++ b/packages/gpt.gblib/services/ChatServices.ts @@ -36,7 +36,7 @@ import { GBMinInstance } from 'botlib'; //import OpenAI from "openai"; import { ChatGPTAPIBrowser, getOpenAIAuth } from 'chatgpt' import { CollectionUtil } from 'pragmatismo-io-framework'; -import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords'; +import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js'; import Path from 'path'; import * as Fs from 'fs'; From 31e8edfd5b31ad81adfa9aff1fa40d679b41bd06 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Fri, 29 Sep 2023 13:24:24 -0300 Subject: [PATCH 63/68] fix(core.gbapp): #372 fix. --- packages/basic.gblib/services/GBVMService.ts | 14 +++++++------- packages/core.gbapp/services/GBMinService.ts | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index 2a85e19b2..bb8aa5934 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -224,14 +224,14 @@ export class GBVMService extends GBService { // Unmarshalls Local variables from server VM. - let pid = this.pid; + const pid = this.pid; let id = this.id; let username = this.username; let mobile = this.mobile; let from = this.from; - let channel = this.channel; - let ENTER = this.ENTER; - let headers = this.headers; + const channel = this.channel; + const ENTER = this.ENTER; + const headers = this.headers; let data = this.data; let list = this.list; let httpUsername = this.httpUsername; @@ -239,9 +239,9 @@ export class GBVMService extends GBService { let today = this.today; let now = this.now; let page = null; - let files = []; + const files = []; let col = 1; - let index = 1 + let index = 1; // Makes objects in BASIC insensitive. @@ -249,7 +249,7 @@ export class GBVMService extends GBService { if (!listOrRow) { - return listOrRow + return listOrRow; }; const lowercase = (oldKey) => typeof oldKey === 'string' ? oldKey.toLowerCase() : oldKey; diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 99092938c..bc27440b1 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -1297,11 +1297,12 @@ export class GBMinService { context.activity.text ); - const conversationReference = JSON.stringify(TurnContext.getConversationReference(context.activity)); - await sec.updateConversationReferenceById(userId, conversationReference); } } + const conversationReference = JSON.stringify(TurnContext.getConversationReference(context.activity)); + await sec.updateConversationReferenceById(userId, conversationReference); + if (GBMinService.userMobile(step)) { const startDialog = user.hearOnDialog ? user.hearOnDialog : min.core.getParam(min.instance, 'Start Dialog', null); From 5e4e4b4163fc6cb8cd17c1dc2c8d9162b71d3836 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Fri, 29 Sep 2023 15:18:50 -0300 Subject: [PATCH 64/68] fix(core.gbapp): #373 fix. --- packages/core.gbapp/services/GBConversationalService.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core.gbapp/services/GBConversationalService.ts b/packages/core.gbapp/services/GBConversationalService.ts index 8e577baa0..48396f181 100644 --- a/packages/core.gbapp/services/GBConversationalService.ts +++ b/packages/core.gbapp/services/GBConversationalService.ts @@ -527,7 +527,9 @@ export class GBConversationalService { } public async playMarkdown(min: GBMinInstance, answer: string, channel: string, step: GBDialogStep, mobile: string) { - const user = step ? await min.userProfile.get(step.context, {}) : null; + const sec = new SecService(); + let user = await sec.getUserFromSystemId(step?step.context.activity.from.id:mobile); + let text = answer; // Calls language translator. From 94e96c3fe1a72ca5eab639ae08265441d66955d3 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Fri, 29 Sep 2023 15:38:41 -0300 Subject: [PATCH 65/68] fix(core.gbapp): #372 fix. --- packages/core.gbapp/services/GBConversationalService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core.gbapp/services/GBConversationalService.ts b/packages/core.gbapp/services/GBConversationalService.ts index 48396f181..58b479379 100644 --- a/packages/core.gbapp/services/GBConversationalService.ts +++ b/packages/core.gbapp/services/GBConversationalService.ts @@ -528,7 +528,7 @@ export class GBConversationalService { public async playMarkdown(min: GBMinInstance, answer: string, channel: string, step: GBDialogStep, mobile: string) { const sec = new SecService(); - let user = await sec.getUserFromSystemId(step?step.context.activity.from.id:mobile); + const user = await sec.getUserFromSystemId(mobile?mobile:step.context.activity.from.id); let text = answer; From 4f143a20695635c487860f00202f707a4c36b11f Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Fri, 29 Sep 2023 15:55:35 -0300 Subject: [PATCH 66/68] fix(core.gbapp): #372 fix. --- .../basic.gblib/services/DialogKeywords.ts | 2 +- .../services/GBConversationalService.ts | 62 ++++++++++--------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/packages/basic.gblib/services/DialogKeywords.ts b/packages/basic.gblib/services/DialogKeywords.ts index 7cf8e71f7..e95b5d084 100644 --- a/packages/basic.gblib/services/DialogKeywords.ts +++ b/packages/basic.gblib/services/DialogKeywords.ts @@ -1230,7 +1230,7 @@ export class DialogKeywords { GBLog.info(`BASIC: Markdown file ${filename} not found on database for ${min.instance.botId}.`); } - await min.conversationalService['playMarkdown'](min, md, DialogKeywords.getChannel(), mobile); + await min.conversationalService['playMarkdown'](min, md, DialogKeywords.getChannel(), null, mobile); } else { const gbaiName = DialogKeywords.getGBAIPath(min.botId, `gbkb`); diff --git a/packages/core.gbapp/services/GBConversationalService.ts b/packages/core.gbapp/services/GBConversationalService.ts index 58b479379..a6d68b6dc 100644 --- a/packages/core.gbapp/services/GBConversationalService.ts +++ b/packages/core.gbapp/services/GBConversationalService.ts @@ -308,10 +308,10 @@ export class GBConversationalService { step: GBDialogStep, mobile: string, url: string, - caption: string, + caption: string, channel: string ): Promise { - return await this.sendFile(min, step, mobile, url , caption); + return await this.sendFile(min, step, mobile, url, caption); } public async sendFile( @@ -393,23 +393,23 @@ export class GBConversationalService { } } else { if (min.instance.smsKey && min.instance.smsSecret) { - return new Promise((resolve: any, reject: any): any => { - const nexmo = new Nexmo({ - apiKey: min.instance.smsKey, - apiSecret: min.instance.smsSecret + return new Promise((resolve: any, reject: any): any => { + const nexmo = new Nexmo({ + apiKey: min.instance.smsKey, + apiSecret: min.instance.smsSecret + }); + // tslint:disable-next-line:no-unsafe-any + nexmo.message.sendSms(min.instance.smsServiceNumber, mobile, text, {}, (err, data) => { + const message = data.messages ? data.messages[0] : {}; + if (err || message['error-text']) { + GBLog.error(`BASIC: error sending SMS to ${mobile}: ${message['error-text']}`); + reject(message['error-text']); + } else { + resolve(data); + } + }); }); - // tslint:disable-next-line:no-unsafe-any - nexmo.message.sendSms(min.instance.smsServiceNumber, mobile, text, {}, (err, data) => { - const message = data.messages ? data.messages[0] : {}; - if (err || message['error-text']) { - GBLog.error(`BASIC: error sending SMS to ${mobile}: ${message['error-text']}`); - reject(message['error-text']); - } else { - resolve(data); - } - }); - }); - } + } } } @@ -462,7 +462,7 @@ export class GBConversationalService { return new Promise(async (resolve, reject) => { try { const oggFile = new Readable(); - oggFile._read = () => {}; // _read is required but you can noop it + oggFile._read = () => { }; // _read is required but you can noop it oggFile.push(buffer); oggFile.push(null); @@ -526,10 +526,12 @@ export class GBConversationalService { }); } - public async playMarkdown(min: GBMinInstance, answer: string, channel: string, step: GBDialogStep, mobile: string) { + public async playMarkdown(min: GBMinInstance, answer: string, channel: string, + step: GBDialogStep, mobile: string) { + const sec = new SecService(); - const user = await sec.getUserFromSystemId(mobile?mobile:step.context.activity.from.id); - + const user = await sec.getUserFromSystemId(mobile ? mobile : step.context.activity.from.id); + let text = answer; // Calls language translator. @@ -904,7 +906,7 @@ export class GBConversationalService { const key = min.core.getParam(min.instance, 'spellcheckerKey', null); if (key) { - text = text.charAt(0).toUpperCase() + text.slice(1); + text = text.charAt(0).toUpperCase() + text.slice(1); const data = await AzureText.getSpelledText(key, text); if (data !== text) { GBLog.info(`Spelling>: ${data}`); @@ -916,7 +918,7 @@ export class GBConversationalService { } public async translate(min: GBMinInstance, text: string, language: string): Promise { - + const translatorEnabled = () => { if (min.instance.params) { const params = JSON.parse(min.instance.params); @@ -996,7 +998,7 @@ export class GBConversationalService { } } - public static async handleText (min, user, step, text: string){ + public static async handleText(min, user, step, text: string) { const sec = new SecService(); text = text.replace(/<([^>]+?)([^>]*?)>(.*?)<\/\1>/gi, ''); @@ -1065,7 +1067,7 @@ export class GBConversationalService { const groupSpell = group ? await min.core.getParam( min.instance, 'Group Spell', - false): false; + false) : false; if (textProcessed !== text && group && groupSpell) { await min.whatsAppDirectLine.sendToDevice(group, `Spell: ${text}`); @@ -1129,11 +1131,11 @@ export class GBConversationalService { GBLog.verbose(`Translated text(prompt): ${text}.`); } if (step.activeDialog.state.options['kind'] === 'file') { - + return await step.prompt('attachmentPrompt', {}); } else { await this.sendText(min, step, text); - return await step.prompt('textPrompt', {}); + return await step.prompt('textPrompt', {}); } } @@ -1229,8 +1231,8 @@ export class GBConversationalService { */ public async sendOnConversation(min: GBMinInstance, user: GuaribasUser, message: any) { if (message['buttons'] || message['sections']) { - await min['whatsAppDirectLine'].sendToDevice(user.userSystemId, message, user.conversationReference ); - } else if (user.conversationReference.startsWith('spaces')) { + await min['whatsAppDirectLine'].sendToDevice(user.userSystemId, message, user.conversationReference); + } else if (user.conversationReference.startsWith('spaces')) { await min['googleDirectLine'].sendToDevice(user.userSystemId, null, user.conversationReference, message); } else { const ref = JSON.parse(user.conversationReference); From 08171fe5d4c732cad1a5b6f29b584177147351f1 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Fri, 29 Sep 2023 16:15:43 -0300 Subject: [PATCH 67/68] fix(core.gbapp): #372 fix. --- packages/basic.gblib/services/DialogKeywords.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/basic.gblib/services/DialogKeywords.ts b/packages/basic.gblib/services/DialogKeywords.ts index e95b5d084..042d44ccb 100644 --- a/packages/basic.gblib/services/DialogKeywords.ts +++ b/packages/basic.gblib/services/DialogKeywords.ts @@ -1246,12 +1246,12 @@ export class DialogKeywords { } if (!url) { - const imageData = await (await fetch(url)).arrayBuffer(); + const ext = mime.extension(Path.extname(filename.localName)); - + // Prepare a cache to be referenced by Bot Framework. - - let buf: any = Buffer.from(imageData); + + const buf = Fs.readFileSync(filename); const gbaiName = DialogKeywords.getGBAIPath(min.botId); const localName = Path.join('work', gbaiName, 'cache', `tmp${GBAdminService.getRndReadableIdentifier()}.${ext}`); Fs.writeFileSync(localName, buf, { encoding: null }); From d9c4f8dc87e3894aa55f42363672193179c5e78f Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Fri, 29 Sep 2023 17:23:02 -0300 Subject: [PATCH 68/68] fix(core.gbapp): #372 fix. --- packages/basic.gblib/services/DialogKeywords.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/basic.gblib/services/DialogKeywords.ts b/packages/basic.gblib/services/DialogKeywords.ts index 042d44ccb..55a4b9d73 100644 --- a/packages/basic.gblib/services/DialogKeywords.ts +++ b/packages/basic.gblib/services/DialogKeywords.ts @@ -1231,6 +1231,8 @@ export class DialogKeywords { } await min.conversationalService['playMarkdown'](min, md, DialogKeywords.getChannel(), null, mobile); + + return; } else { const gbaiName = DialogKeywords.getGBAIPath(min.botId, `gbkb`);