fix(all): Fix user switching on a single chip.

This commit is contained in:
Rodrigo Rodriguez 2023-09-09 13:04:20 -03:00
parent c577b64369
commit da02804ac9

View file

@ -105,10 +105,10 @@ export class WhatsappDirectLine extends GBService {
whatsappServiceKey === 'internal' whatsappServiceKey === 'internal'
? 'GeneralBots' ? 'GeneralBots'
: whatsappServiceNumber.indexOf(';') > -1 : whatsappServiceNumber.indexOf(';') > -1
? 'maytapi' ? 'maytapi'
: whatsappServiceKey !== 'internal' : whatsappServiceKey !== 'internal'
? 'graphapi' ? 'graphapi'
: 'chatapi'; : 'chatapi';
this.groupId = groupId; this.groupId = groupId;
} }
@ -198,8 +198,7 @@ export class WhatsappDirectLine extends GBService {
}; };
if (setUrl) { if (setUrl) {
createClient.bind(this)(); createClient.bind(this)();
} } else {
else {
this.customClient = minBoot.whatsAppDirectLine.customClient; this.customClient = minBoot.whatsAppDirectLine.customClient;
} }
setUrl = false; setUrl = false;
@ -308,25 +307,20 @@ export class WhatsappDirectLine extends GBService {
let group = ''; let group = '';
let answerText = null; let answerText = null;
let attachments = null; let attachments = null;
switch (provider) { switch (provider) {
case 'GeneralBots': case 'GeneralBots':
message = req; message = req;
to = message.to.endsWith('@g.us') ? message.to.split('@')[0] : message.to.split('@')[0]; to = message.to.endsWith('@g.us') ? message.to.split('@')[0] : message.to.split('@')[0];
const newThis= WhatsappDirectLine.botsByNumber[to]; const newThis = WhatsappDirectLine.botsByNumber[to];
if (newThis === undefined){ if (newThis === undefined) {
throw GBError.create(`Bot Number ${to} not setup for any loaded bot.`); throw GBError.create(`Bot Number ${to} not setup for any loaded bot.`);
} } else {
else if (newThis.min.botId !== this.min.botId) {
{ await newThis.received(req, res);
if (newThis.min.botId !== this.min.botId)
{
await newThis.received (req, res);
return; return;
} }
} }
text = message.body; text = message.body;
@ -336,9 +330,14 @@ export class WhatsappDirectLine extends GBService {
if (message.hasMedia) { if (message.hasMedia) {
const base64Image = await message.downloadMedia(); 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 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 }); Fs.writeFileSync(localName, buf, { encoding: null });
const url = urlJoin(GBServer.globals.publicAddress, this.min.botId, 'cache', Path.basename(localName)); 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) { if (user.agentSystemId.indexOf('@') !== -1) {
// Agent is from Teams. // Agent is from Teams.
await this.min.conversationalService['sendOnConversation']( await this.min.conversationalService['sendOnConversation'](
this.min, this.min,
@ -581,10 +579,10 @@ export class WhatsappDirectLine extends GBService {
await this.endTransfer(from, locale, user, agent, sec); await this.endTransfer(from, locale, user, agent, sec);
} else { } else {
GBLog.info(`USER (${from}) TO AGENT ${agent.userSystemId}: ${text}`); GBLog.info(`USER (${from}) TO AGENT ${agent.userSystemId}: ${text}`);
const prompt = `the person said: ${text}. what can I tell her?`; const prompt = `the person said: ${text}. what can I tell her?`;
const answer = await ChatServices.continue(this.min, prompt, 0); 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) { if (user.agentSystemId.indexOf('@') !== -1) {
// Agent is from Teams or Google Chat. // 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) { 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); await this.sendToDeviceEx(id, Messages[this.locale].notify_end_transfer(this.min.instance.botId), locale, null);
if (user.agentSystemId.indexOf('@') !== -1) { if (user.agentSystemId.indexOf('@') !== -1) {
// Agent is from Teams. // Agent is from Teams.
await this.min.conversationalService['sendOnConversation']( await this.min.conversationalService['sendOnConversation'](
this.min, this.min,
agent, agent,
Messages[this.locale].notify_end_transfer(this.min.instance.botId) Messages[this.locale].notify_end_transfer(this.min.instance.botId)
); );
} else { } else {
await this.sendToDeviceEx( await this.sendToDeviceEx(
user.agentSystemId, user.agentSystemId,
Messages[this.locale].notify_end_transfer(this.min.instance.botId), Messages[this.locale].notify_end_transfer(this.min.instance.botId),
@ -677,7 +671,7 @@ export class WhatsappDirectLine extends GBService {
} }
}); });
} catch (e) { } 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); await this.printMessages(response.obj.activities, conversationId, from, fromName);
} catch (err) { } catch (err) {
GBLog.error( 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) { if (activity.attachments) {
await CollectionUtil.asyncForEach(activity.attachments, async attachment => { await CollectionUtil.asyncForEach(activity.attachments, async attachment => {
switch (attachment.contentType) { switch (attachment.contentType) {
case 'application/vnd.microsoft.card.hero': case 'application/vnd.microsoft.card.hero':
output += `\n${this.renderHeroCard(attachment)}`; output += `\n${this.renderHeroCard(attachment)}`;
break; break;
case 'image/png': case 'image/png':
await this.sendFileToDevice(to, attachment.contentUrl, await this.sendFileToDevice(to, attachment.contentUrl, attachment.name, attachment.name, 0);
attachment.name, attachment.name, 0);
return; return;
default: default:
GBLog.info(`Unknown content type: ${attachment.contentType}`); GBLog.info(`Unknown content type: ${attachment.contentType}`);
@ -982,11 +975,9 @@ export class WhatsappDirectLine extends GBService {
switch (provider) { switch (provider) {
case 'GeneralBots': case 'GeneralBots':
// Ignore E2E messages and status updates.
// Ignore E2E messages and status updates.
if (req.type && req.type === 'e2e_notification' if ((req.type && req.type === 'e2e_notification') || req.isStatus) {
|| req.isStatus) {
return; return;
} }
@ -1048,13 +1039,13 @@ export class WhatsappDirectLine extends GBService {
GBLog.info(`${user.userSystemId} user changed Bot to: ${botId}.`); GBLog.info(`${user.userSystemId} user changed Bot to: ${botId}.`);
user = await sec.updateUserInstance(user.userSystemId, urlMin.instance.instanceId); user = await sec.updateUserInstance(user.userSystemId, urlMin.instance.instanceId);
} }
let activeMin; let activeMin;
// Processes group behaviour. // Processes group behaviour.
text = text.replace(/\@\d+ /gi, ''); text = text.replace(/\@\d+ /gi, '');
let group; let group;
if (provider === 'chatapi') { if (provider === 'chatapi') {
// Ensures that the bot group is the active bot for the user (like switching). // 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') { } else if (provider === 'GeneralBots') {
// Ensures that the bot group is the active bot for the user (like switching). // Ensures that the bot group is the active bot for the user (like switching).
const message = req; const message = req;
if (message.from.endsWith('@g.us')) { if (message.from.endsWith('@g.us')) {
group = message.from; group = message.from;
@ -1089,37 +1080,88 @@ export class WhatsappDirectLine extends GBService {
GBLog.warn(`Group: ${group} not associated with botId:${botId}.`); GBLog.warn(`Group: ${group} not associated with botId:${botId}.`);
} }
} }
// Detects if the welcome message is enabled. // Detects if the welcome message is enabled.
if (process.env.WHATSAPP_WELCOME_DISABLED !== 'true') { if (process.env.WHATSAPP_WELCOME_DISABLED === 'true') {
// Tries to find if user wants to switch bots. let minInstance = GBServer.globals.minInstances.filter(
p => p.instance.botId.toLowerCase() === botId.toLowerCase()
let toSwitchMin = GBServer.globals.minInstances.filter(
p => p.instance.botId.toLowerCase() === text.toLowerCase()
)[0]; )[0];
if (!toSwitchMin) {
toSwitchMin = GBServer.globals.minInstances.filter(p => // Just pass the message to the receiver.
p.instance.activationCode ? p.instance.activationCode.toLowerCase() === text.toLowerCase() : false
)[0]; 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();
}
} }
} else {
// If bot has a fixed Find active bot instance. // 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 // So gets the new bot instance information and prepares to
// start dialog if any is specified in Config.xlsx. // auto start dialog if any is specified.
if (user === null || user.hearOnDialog) { const instance = await this.min.core.loadInstanceByBotId(activeMin.botId);
user = await sec.ensureUser(activeMin.instance.instanceId, id, senderName, '', 'whatsapp', senderName, null); await sec.updateUserInstance(id, instance.instanceId);
await (activeMin as any).whatsAppDirectLine.resetConversationId(activeMin.botId, id, '');
const startDialog = user.hearOnDialog const startDialog = activeMin.core.getParam(activeMin.instance, 'Start Dialog', null);
? user.hearOnDialog
: activeMin.core.getParam(activeMin.instance, 'Start Dialog', null);
if (startDialog) { 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') { if (provider === 'chatapi') {
req.body.messages[0].body = `/start`; req.body.messages[0].body = `/start`;
} else if (provider === 'maytapi') { } else if (provider === 'maytapi') {
@ -1128,84 +1170,37 @@ export class WhatsappDirectLine extends GBService {
req.body = `/start`; 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); await (activeMin as any).whatsAppDirectLine.received(req, res);
} else { } else {
await (activeMin as any).whatsAppDirectLine.sendToDevice( await (activeMin as any).whatsAppDirectLine.sendToDevice(
id, 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 null
); );
if (res) { }
res.end(); if (res) {
} res.end();
} }
} else { } else {
// User wants to switch bots. let t;
activeMin = GBServer.globals.minInstances.filter(p => p.instance.instanceId === user.instanceId)[0];
if (toSwitchMin !== undefined) { if (activeMin === undefined) {
// So gets the new bot instance information and prepares to activeMin = GBServer.globals.minBoot;
// auto start dialog if any is specified. t = (activeMin as any).whatsAppDirectLine;
await t.sendToDevice(
const instance = await this.min.core.loadInstanceByBotId(activeMin.botId); id,
await sec.updateUserInstance(id, instance.instanceId); `O outro Bot que você estava falando(${user.instanceId}), não está mais disponível. Agora você está falando comigo, ${activeMin.instance.title}...`
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();
}
} else { } else {
let t; if ((activeMin as any).whatsAppDirectLine) {
activeMin = GBServer.globals.minInstances.filter(p => p.instance.instanceId === user.instanceId)[0];
if (activeMin === undefined) {
activeMin = GBServer.globals.minBoot;
t = (activeMin as any).whatsAppDirectLine; t = (activeMin as any).whatsAppDirectLine;
await t.sendToDevice( } else {
id, t = (GBServer.globals.minBoot as any).whatsAppDirectLine;
`O outro Bot que você estava falando(${user.instanceId}), não está mais disponível. Agora você está falando comigo, ${activeMin.instance.title}...`
);
} }
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) { } catch (error) {
GBLog.error(`Error on Whatsapp callback: ${error.data ? error.data : error} ${error.stack}`); GBLog.error(`Error on Whatsapp callback: ${error.data ? error.data : error} ${error.stack}`);