refactor(bot): 🔥 fallback

This commit is contained in:
Leifer Mendez
2023-01-12 16:35:43 +01:00
parent f742caa9a4
commit 8a1bdc5878
2 changed files with 20 additions and 6 deletions

View File

@@ -10,7 +10,6 @@ const logger = new Console({
}) })
const QueuePrincipal = new Queue() const QueuePrincipal = new Queue()
const QueueDynamic = new Queue()
/** /**
* [ ] Escuchar eventos del provider asegurarte que los provider emitan eventos * [ ] Escuchar eventos del provider asegurarte que los provider emitan eventos
@@ -92,12 +91,13 @@ class CoreClass {
const sendFlow = async (messageToSend, numberOrId) => { const sendFlow = async (messageToSend, numberOrId) => {
const queue = [] const queue = []
for (const ctxMessage of messageToSend) { for (const ctxMessage of messageToSend) {
console.log('🙌 ', ctxMessage.answer)
const delayMs = ctxMessage?.options?.delay || 0 const delayMs = ctxMessage?.options?.delay || 0
if (delayMs) await delay(delayMs) if (delayMs) await delay(delayMs)
QueuePrincipal.enqueue(() => QueuePrincipal.enqueue(() =>
Promise.all([ Promise.all([
this.sendProviderAndSave(numberOrId, ctxMessage), 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 // 📄 [options: fallBack]: esta funcion se encarga de repetir el ultimo mensaje
const fallBack = () => { const fallBack = async () => {
fallBackFlag = true fallBackFlag = true
msgToSend = this.flowClass.find(refToContinue?.keyword, true) || [] await this.sendProviderAndSave(from, refToContinue)
sendFlow(msgToSend, from) QueuePrincipal.queue = []
return refToContinue return refToContinue
} }
// 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes // 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes
// para evitar bloque de whatsapp // 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)) if (!Array.isArray(listMsg))
throw new Error('Esto debe ser un ARRAY') throw new Error('Esto debe ser un ARRAY')
fallBackFlag = optListMsg.fallback
const parseListMsg = listMsg const parseListMsg = listMsg
.map(({ body }, index) => .map(({ body }, index) =>
toCtx({ toCtx({
@@ -134,6 +138,13 @@ class CoreClass {
return 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 // 📄 Se encarga de revisar si el contexto del mensaje tiene callback y ejecutarlo
const cbEveryCtx = async (inRef) => { const cbEveryCtx = async (inRef) => {
if (!this.flowClass.allCallbacks[inRef]) return Promise.resolve() 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 // 📄 [options: callback]: Si se tiene un callback se ejecuta
//TODO AQUI //TODO AQUI
// if (!fallBackFlag) { // if (!fallBackFlag) {

View File

@@ -10,6 +10,7 @@ module.exports = [
banner: banner['banner.output'].join(''), banner: banner['banner.output'].join(''),
file: join(__dirname, 'lib', 'bundle.bot.cjs'), file: join(__dirname, 'lib', 'bundle.bot.cjs'),
format: 'cjs', format: 'cjs',
sourcemap: true,
}, },
plugins: [commonjs(), nodeResolve()], plugins: [commonjs(), nodeResolve()],
}, },