fix(whatsapp.gblib): Fix in bot switching.

This commit is contained in:
Rodrigo Rodriguez 2023-09-03 14:48:05 -03:00
parent 6c4a546202
commit 929b36d578
2 changed files with 34 additions and 11 deletions

View file

@ -769,12 +769,12 @@ export class GBMinService {
}
const group = min.core.getParam<string>(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<string>(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'));

View file

@ -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;