mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-20 20:49:15 +00:00
refactor(bot): ✨ async callbacks
This commit is contained in:
@@ -8,6 +8,10 @@ const { createWriteStream } = require('fs')
|
|||||||
const logger = new Console({
|
const logger = new Console({
|
||||||
stdout: createWriteStream(`${process.cwd()}/core.class.log`),
|
stdout: createWriteStream(`${process.cwd()}/core.class.log`),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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
|
||||||
* [ ] Guardar historial en db
|
* [ ] Guardar historial en db
|
||||||
@@ -84,17 +88,33 @@ class CoreClass {
|
|||||||
this.databaseClass.save(ctxByNumber)
|
this.databaseClass.save(ctxByNumber)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 📄 Esta funcion se encarga de enviar un array de mensajes dentro de este ctx
|
||||||
|
const sendFlow = async (messageToSend, numberOrId) => {
|
||||||
|
const queue = []
|
||||||
|
for (const ctxMessage of messageToSend) {
|
||||||
|
const delayMs = ctxMessage?.options?.delay || 0
|
||||||
|
if (delayMs) await delay(delayMs)
|
||||||
|
QueuePrincipal.enqueue(() =>
|
||||||
|
Promise.all([
|
||||||
|
this.sendProviderAndSave(numberOrId, ctxMessage),
|
||||||
|
cbEveryCtx(ctxMessage?.ref),
|
||||||
|
])
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return Promise.all(queue)
|
||||||
|
}
|
||||||
|
|
||||||
// 📄 [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 = () => {
|
||||||
fallBackFlag = true
|
fallBackFlag = true
|
||||||
msgToSend = this.flowClass.find(refToContinue?.keyword, true) || []
|
msgToSend = this.flowClass.find(refToContinue?.keyword, true) || []
|
||||||
this.sendFlow(msgToSend, from)
|
sendFlow(msgToSend, from)
|
||||||
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 = (listMsg = [], optListMsg = { limit: 3 }) => {
|
const flowDynamic = async (listMsg = [], optListMsg = { limit: 3 }) => {
|
||||||
if (!Array.isArray(listMsg))
|
if (!Array.isArray(listMsg))
|
||||||
throw new Error('Esto debe ser un ARRAY')
|
throw new Error('Esto debe ser un ARRAY')
|
||||||
|
|
||||||
@@ -108,26 +128,29 @@ class CoreClass {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
.slice(0, optListMsg.limit)
|
.slice(0, optListMsg.limit)
|
||||||
msgToSend = parseListMsg
|
for (const msg of parseListMsg) {
|
||||||
this.sendFlow(msgToSend, from)
|
await this.sendProviderAndSave(from, msg)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 📄 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 = (inRef) => {
|
const cbEveryCtx = async (inRef) => {
|
||||||
this.flowClass.allCallbacks[inRef](messageCtxInComming, {
|
if (!this.flowClass.allCallbacks[inRef]) return Promise.resolve()
|
||||||
|
return this.flowClass.allCallbacks[inRef](messageCtxInComming, {
|
||||||
fallBack,
|
fallBack,
|
||||||
flowDynamic,
|
flowDynamic,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 📄 [options: callback]: Si se tiene un callback se ejecuta
|
// 📄 [options: callback]: Si se tiene un callback se ejecuta
|
||||||
if (!fallBackFlag) {
|
//TODO AQUI
|
||||||
if (prevMsg?.options?.capture) cbEveryCtx(prevMsg?.ref)
|
// if (!fallBackFlag) {
|
||||||
for (const ite of this.flowClass.find(body)) {
|
// if (prevMsg?.options?.capture) cbEveryCtx(prevMsg?.ref)
|
||||||
if (!ite?.options?.capture) cbEveryCtx(ite?.ref)
|
// for (const ite of this.flowClass.find(body)) {
|
||||||
}
|
// if (!ite?.options?.capture) cbEveryCtx(ite?.ref)
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
// 📄🤘(tiene return) [options: nested(array)]: Si se tiene flujos hijos los implementa
|
// 📄🤘(tiene return) [options: nested(array)]: Si se tiene flujos hijos los implementa
|
||||||
if (!fallBackFlag && prevMsg?.options?.nested?.length) {
|
if (!fallBackFlag && prevMsg?.options?.nested?.length) {
|
||||||
@@ -138,11 +161,12 @@ class CoreClass {
|
|||||||
|
|
||||||
msgToSend = this.flowClass.find(body, false, flowStandalone) || []
|
msgToSend = this.flowClass.find(body, false, flowStandalone) || []
|
||||||
|
|
||||||
for (const ite of msgToSend) {
|
// //TODO AQUI
|
||||||
cbEveryCtx(ite?.ref)
|
// for (const ite of msgToSend) {
|
||||||
}
|
// cbEveryCtx(ite?.ref)
|
||||||
|
// }
|
||||||
|
|
||||||
this.sendFlow(msgToSend, from)
|
sendFlow(msgToSend, from)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,13 +177,13 @@ class CoreClass {
|
|||||||
|
|
||||||
if (['string', 'boolean'].includes(typeCapture) && valueCapture) {
|
if (['string', 'boolean'].includes(typeCapture) && valueCapture) {
|
||||||
msgToSend = this.flowClass.find(refToContinue?.ref, true) || []
|
msgToSend = this.flowClass.find(refToContinue?.ref, true) || []
|
||||||
this.sendFlow(msgToSend, from)
|
sendFlow(msgToSend, from)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
msgToSend = this.flowClass.find(body) || []
|
msgToSend = this.flowClass.find(body) || []
|
||||||
this.sendFlow(msgToSend, from)
|
sendFlow(msgToSend, from)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -176,18 +200,6 @@ class CoreClass {
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
sendFlow = async (messageToSend, numberOrId) => {
|
|
||||||
const queue = []
|
|
||||||
for (const ctxMessage of messageToSend) {
|
|
||||||
const delayMs = ctxMessage?.options?.delay || 0
|
|
||||||
if (delayMs) await delay(delayMs)
|
|
||||||
Queue.enqueue(() =>
|
|
||||||
this.sendProviderAndSave(numberOrId, ctxMessage)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return Promise.all(queue)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @param {*} message
|
* @param {*} message
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
class Queue {
|
class Queue {
|
||||||
static queue = []
|
queue = []
|
||||||
static pendingPromise = false
|
pendingPromise = false
|
||||||
|
|
||||||
static enqueue(promise) {
|
enqueue(promise) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.queue.push({
|
this.queue.push({
|
||||||
promise,
|
promise,
|
||||||
@@ -13,7 +13,7 @@ class Queue {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
static dequeue() {
|
dequeue() {
|
||||||
if (this.workingOnPromise) {
|
if (this.workingOnPromise) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user