From 134cf31695adc78ecce23412d4e7ea62b5f4255e Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Fri, 3 Apr 2020 00:17:21 -0300 Subject: [PATCH] new(core.gbapp): New param for disabling welcome messages. --- packages/core.gbapp/services/GBMinService.ts | 45 +++++++++++--------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 5df8df88..6b1f884e 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -137,33 +137,40 @@ export class GBMinService { res.end(); return; // Exit here. } + let activeMin; + if (process.env.WHATSAPP_WELCOME_DISABLED !== "true") { - const toSwitchMin = GBServer.globals.minInstances.filter(p => p.botId === text)[0]; - let activeMin = toSwitchMin ? toSwitchMin : GBServer.globals.minBoot; + const toSwitchMin = GBServer.globals.minInstances.filter(p => p.botId === text)[0]; + activeMin = toSwitchMin ? toSwitchMin : GBServer.globals.minBoot; - let sec = new SecService(); - let user = await sec.getUserFromPhone(id); + let sec = new SecService(); + let user = await sec.getUserFromPhone(id); - if (user === null) { - user = await sec.ensureUser(activeMin.instance.instanceId, id, - activeMin.botId, id, "", "whatsapp", id, id); - await (activeMin as any).whatsAppDirectLine.sendToDevice(id, `Olá! Seja bem-vinda(o)!\nMe chamo ${activeMin.instance.title}. Como posso ajudar?`); - res.end(); - } else { - // User wants to switch bots. - if (toSwitchMin !== undefined) { - await sec.updateCurrentBotId(id, text); - await (activeMin as any).whatsAppDirectLine.sendToDevice(id, `Agora falando com ${activeMin.instance.title}...`); + if (user === null) { + user = await sec.ensureUser(activeMin.instance.instanceId, id, + activeMin.botId, id, "", "whatsapp", id, id); + await (activeMin as any).whatsAppDirectLine.sendToDevice(id, `Olá! Seja bem-vinda(o)!\nMe chamo ${activeMin.instance.title}. Como posso ajudar?`); res.end(); + } else { + // User wants to switch bots. + if (toSwitchMin !== undefined) { + await sec.updateCurrentBotId(id, text); + await (activeMin as any).whatsAppDirectLine.sendToDevice(id, `Agora falando com ${activeMin.instance.title}...`); + res.end(); + } + else { + activeMin = GBServer.globals.minInstances.filter(p => p.botId === user.currentBotId)[0];; + (activeMin as any).whatsAppDirectLine.received(req, res); + } } - else { - activeMin = GBServer.globals.minInstances.filter(p => p.botId === user.currentBotId)[0];; - (activeMin as any).whatsAppDirectLine.received(req, res); - } + } + else { + (GBServer.globals.minBoot as any).whatsAppDirectLine.received(req, res); } } catch (error) { GBLog.error(`Error on Whatsapp callback: ${error.message}`); } + }); await Promise.all( @@ -554,7 +561,7 @@ export class GBMinService { user.conversation, user.systemUser, context.activity.text); } - + // Checks for global exit kewywords cancelling any active dialogs. const globalQuit = (locale, utterance) => {