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'
? '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}`);