From 8a1bdc58785950493cc2683b0ecb0fa881dc683f Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Thu, 12 Jan 2023 16:35:43 +0100 Subject: [PATCH] refactor(bot): :fire: fallback --- packages/bot/core/core.class.js | 25 +++++++++++++++++++------ packages/bot/rollup-bot.config.js | 1 + 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/bot/core/core.class.js b/packages/bot/core/core.class.js index e084b20..c0186d8 100644 --- a/packages/bot/core/core.class.js +++ b/packages/bot/core/core.class.js @@ -10,7 +10,6 @@ const logger = new Console({ }) const QueuePrincipal = new Queue() -const QueueDynamic = new Queue() /** * [ ] Escuchar eventos del provider asegurarte que los provider emitan eventos @@ -92,12 +91,13 @@ class CoreClass { const sendFlow = async (messageToSend, numberOrId) => { const queue = [] for (const ctxMessage of messageToSend) { + console.log('🙌 ', ctxMessage.answer) const delayMs = ctxMessage?.options?.delay || 0 if (delayMs) await delay(delayMs) QueuePrincipal.enqueue(() => Promise.all([ this.sendProviderAndSave(numberOrId, ctxMessage), - cbEveryCtx(ctxMessage?.ref), + resolveCbEveryCtx(ctxMessage), ]) ) } @@ -105,19 +105,23 @@ class CoreClass { } // 📄 [options: fallBack]: esta funcion se encarga de repetir el ultimo mensaje - const fallBack = () => { + const fallBack = async () => { fallBackFlag = true - msgToSend = this.flowClass.find(refToContinue?.keyword, true) || [] - sendFlow(msgToSend, from) + await this.sendProviderAndSave(from, refToContinue) + QueuePrincipal.queue = [] return refToContinue } // 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes // para evitar bloque de whatsapp - const flowDynamic = async (listMsg = [], optListMsg = { limit: 3 }) => { + const flowDynamic = async ( + listMsg = [], + optListMsg = { limit: 5, fallback: false } + ) => { if (!Array.isArray(listMsg)) throw new Error('Esto debe ser un ARRAY') + fallBackFlag = optListMsg.fallback const parseListMsg = listMsg .map(({ body }, index) => toCtx({ @@ -134,6 +138,13 @@ class CoreClass { return } + // 📄 Se encarga de revisar si el contexto del mensaje tiene callback o fallback + const resolveCbEveryCtx = async (ctxMessage) => { + if (prevMsg?.options?.capture) return cbEveryCtx(prevMsg?.ref) + if (!ctxMessage?.options?.capture) + return await cbEveryCtx(ctxMessage?.ref) + } + // 📄 Se encarga de revisar si el contexto del mensaje tiene callback y ejecutarlo const cbEveryCtx = async (inRef) => { if (!this.flowClass.allCallbacks[inRef]) return Promise.resolve() @@ -143,6 +154,8 @@ class CoreClass { }) } + if (prevMsg?.ref) resolveCbEveryCtx(prevMsg) + // 📄 [options: callback]: Si se tiene un callback se ejecuta //TODO AQUI // if (!fallBackFlag) { diff --git a/packages/bot/rollup-bot.config.js b/packages/bot/rollup-bot.config.js index f8ffa2a..36bbb87 100644 --- a/packages/bot/rollup-bot.config.js +++ b/packages/bot/rollup-bot.config.js @@ -10,6 +10,7 @@ module.exports = [ banner: banner['banner.output'].join(''), file: join(__dirname, 'lib', 'bundle.bot.cjs'), format: 'cjs', + sourcemap: true, }, plugins: [commonjs(), nodeResolve()], },