From 64d77b4b43e01d77059e653dbbd72c443103cfef Mon Sep 17 00:00:00 2001 From: rodrigorodriguez Date: Wed, 12 Oct 2022 02:05:26 -0300 Subject: [PATCH] new(whatsapp.gblib): Native provider works in groups now. --- .../services/GBConversationalService.ts | 5 ++- .../services/WhatsappDirectLine.ts | 40 ++++++++++++------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/core.gbapp/services/GBConversationalService.ts b/packages/core.gbapp/services/GBConversationalService.ts index 8ca53358..4f9e505c 100644 --- a/packages/core.gbapp/services/GBConversationalService.ts +++ b/packages/core.gbapp/services/GBConversationalService.ts @@ -991,7 +991,10 @@ export class GBConversationalService { analytics.createMessage(min.instance.instanceId, user.conversation, null, text); if (!isNaN(member.id) && !member.id.startsWith('1000')) { - await min.whatsAppDirectLine.sendToDevice(member.id, text, step.context.activity.conversation.id); + + const to = step.context.activity.group? step.context.activity.group : member.id; + + await min.whatsAppDirectLine.sendToDevice(to, text, step.context.activity.conversation.id); } else { await step.context.sendActivity(text); } diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 1aa7e3d8..8a1acc0d 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -344,7 +344,8 @@ export class WhatsappDirectLine extends GBService { case 'GeneralBots': message = req; text = message.body; - from = message.from.split('@')[0]; + from = message.from.endsWith('@g.us') ? + message.author.split('@')[0] : message.from.split('@')[0]; fromName = message._data.notifyName; if (message.hasMedia) { @@ -402,7 +403,7 @@ export class WhatsappDirectLine extends GBService { GBLog.info(`GBWhatsapp: RCV ${from}(${fromName}): ${text})`); let botGroupID = WhatsappDirectLine.botGroups[this.min.botId]; - let botShortcuts = this.min.core.getParam(this.min.instance, 'WhatsApp Group Shortcuts', null); + let botShortcuts = this.min.core.getParam(this.min.instance, 'WhatsApp Group Shortcuts', null); if (!botShortcuts) { botShortcuts = new Array() } @@ -727,7 +728,16 @@ export class WhatsappDirectLine extends GBService { switch (this.provider) { case 'GeneralBots': const attachment = await MessageMedia.fromUrl(url); - await this.customClient.sendMessage(to + '@c.us', attachment, { caption: caption }); + if (to.indexOf('@') == -1) { + if (to.length == 18) { + to = to + '@g.us'; + } + else { + to = to + '@c.us'; + } + } + + await this.customClient.sendMessage(to, attachment, { caption: caption }); break; case 'chatapi': @@ -859,15 +869,15 @@ export class WhatsappDirectLine extends GBService { switch (this.provider) { case 'GeneralBots': - if (to.length == 18) - { - to = to + '@g.us'; + if (to.indexOf('@') == -1) { + if (to.length == 18) { + to = to + '@g.us'; + } + else { + to = to + '@c.us'; + } } - else - { - to = to + '@c.us'; - } - this.customClient.sendMessage(to, msg); + await this.customClient.sendMessage(to, msg); break; @@ -945,7 +955,7 @@ export class WhatsappDirectLine extends GBService { let senderName; let botId; let text; - + switch (provider) { case "GeneralBots": @@ -1007,8 +1017,8 @@ export class WhatsappDirectLine extends GBService { } const sec = new SecService(); - - let user = await sec.ensureUser(this.min.instance.instanceId, id,senderName,'', 'whatsApp', senderName, null); + + let user = await sec.ensureUser(this.min.instance.instanceId, id, senderName, '', 'whatsApp', senderName, null); GBLog.info(`A WhatsApp mobile requested instance for: ${botId}.`); let urlMin: any = GBServer.globals.minInstances.filter @@ -1045,7 +1055,7 @@ export class WhatsappDirectLine extends GBService { function getKeyByValue(object, value) { return Object.keys(object).find(key => object[key] === value); } - const botId = getKeyByValue(WhatsappDirectLine.botGroups, group) ; + const botId = getKeyByValue(WhatsappDirectLine.botGroups, group); if (botId && user.instanceId !== this.min.instance.instanceId) { await sec.updateUserInstance(id, this.min.instance.instanceId); }