From 929b36d578d1d91924d92ad4755b41899169d606 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 3 Sep 2023 14:48:05 -0300 Subject: [PATCH] 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 2b013362..b4e5f960 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 ee0b3310..242a5c79 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;