@bot-whatsapp/bot
Advanced tools
Comparing version 0.1.30 to 0.1.31
const { toCtx } = require('../io/methods') | ||
const { printer } = require('../utils/interactive') | ||
const { delay } = require('../utils/delay') | ||
const Queue = require('../utils/queue') | ||
const { Console } = require('console') | ||
const { createWriteStream } = require('fs') | ||
const Queue = require('../utils/queue') | ||
const { LIST_REGEX } = require('../io/events') | ||
const GlobalState = require('../context/state.class') | ||
const SingleState = require('../context/state.class') | ||
const GlobalState = require('../context/globalState.class') | ||
@@ -13,5 +15,8 @@ const logger = new Console({ | ||
}) | ||
const loggerQueue = new Console({ | ||
stdout: createWriteStream(`${process.cwd()}/queue.class.log`), | ||
}) | ||
const QueuePrincipal = new Queue() | ||
const StateHandler = new GlobalState() | ||
const StateHandler = new SingleState() | ||
const GlobalStateHandler = new GlobalState() | ||
@@ -28,3 +33,14 @@ /** | ||
providerClass | ||
generalArgs = { blackList: [], listEvents: {}, delay: 0 } | ||
queuePrincipal | ||
generalArgs = { | ||
blackList: [], | ||
listEvents: {}, | ||
delay: 0, | ||
globalState: {}, | ||
extensions: undefined, | ||
queue: { | ||
timeout: 20000, | ||
concurrencyLimit: 15, | ||
}, | ||
} | ||
constructor(_flow, _database, _provider, _args) { | ||
@@ -36,2 +52,12 @@ this.flowClass = _flow | ||
this.queuePrincipal = new Queue( | ||
loggerQueue, | ||
this.generalArgs.queue.concurrencyLimit, | ||
this.generalArgs.queue.timeout | ||
) | ||
GlobalStateHandler.updateState()(this.generalArgs.globalState) | ||
if (this.generalArgs.extensions) GlobalStateHandler.RAW = this.generalArgs.extensions | ||
for (const { event, func } of this.listenerBusEvents()) { | ||
@@ -107,2 +133,12 @@ this.providerClass.on(event, func) | ||
// 📄 Mantener estado global | ||
const globalState = { | ||
getMyState: GlobalStateHandler.getMyState(), | ||
getAllState: GlobalStateHandler.getAllState, | ||
update: GlobalStateHandler.updateState(messageCtxInComming), | ||
clear: GlobalStateHandler.clear(), | ||
} | ||
const extensions = GlobalStateHandler.RAW | ||
// 📄 Crar CTX de mensaje (uso private) | ||
@@ -114,2 +150,3 @@ const createCtxMessage = (payload = {}, index = 0) => { | ||
const capture = payload?.capture ?? false | ||
const delay = payload?.delay ?? 0 | ||
@@ -121,3 +158,3 @@ return toCtx({ | ||
index, | ||
options: { media, buttons, capture }, | ||
options: { media, buttons, capture, delay }, | ||
}) | ||
@@ -128,4 +165,3 @@ } | ||
const clearQueue = () => { | ||
QueuePrincipal.pendingPromise = false | ||
QueuePrincipal.queue = [] | ||
this.queuePrincipal.clearQueue(from) | ||
} | ||
@@ -146,13 +182,32 @@ | ||
const sendFlow = async (messageToSend, numberOrId, options = { prev: prevMsg }) => { | ||
if (options.prev?.options?.capture) await cbEveryCtx(options.prev?.ref) | ||
const queue = [] | ||
if (options.prev?.options?.capture) { | ||
await cbEveryCtx(options.prev?.ref) | ||
} | ||
for (const ctxMessage of messageToSend) { | ||
if (endFlowFlag) return | ||
if (endFlowFlag) { | ||
return // Si endFlowFlag es verdadero, detener el flujo | ||
} | ||
const delayMs = ctxMessage?.options?.delay ?? this.generalArgs.delay ?? 0 | ||
if (delayMs) await delay(delayMs) | ||
await QueuePrincipal.enqueue(() => | ||
this.sendProviderAndSave(numberOrId, ctxMessage).then(() => resolveCbEveryCtx(ctxMessage)) | ||
) | ||
if (delayMs) { | ||
await delay(delayMs) // Esperar según el retraso configurado | ||
} | ||
logger.log(`[sendQueue_A]: `, ctxMessage) | ||
try { | ||
await this.queuePrincipal.enqueue(from, async () => { | ||
// Usar async en la función pasada a enqueue | ||
await this.sendProviderAndSave(numberOrId, ctxMessage) | ||
logger.log(`[QUEUE_SE_ENVIO]: `, ctxMessage) | ||
await resolveCbEveryCtx(ctxMessage) | ||
}) | ||
} catch (error) { | ||
logger.error(`Error al encolar: ${error.message}`) | ||
return Promise.reject | ||
// Puedes considerar manejar el error aquí o rechazar la promesa | ||
// Pasada a resolveCbEveryCtx con el error correspondiente. | ||
} | ||
} | ||
return Promise.all(queue) | ||
} | ||
@@ -183,3 +238,3 @@ | ||
async (message = null) => { | ||
QueuePrincipal.queue = [] | ||
this.queuePrincipal.clearQueue(from) | ||
flag.fallBack = true | ||
@@ -227,2 +282,4 @@ await this.sendProviderAndSave(from, { | ||
for (const msg of parseListMsg) { | ||
const delayMs = msg?.options?.delay ?? this.generalArgs.delay ?? 0 | ||
if (delayMs) await delay(delayMs) | ||
await this.sendProviderAndSave(from, msg) | ||
@@ -250,2 +307,3 @@ } | ||
const provider = this.providerClass | ||
const database = this.databaseClass | ||
@@ -255,4 +313,7 @@ if (!this.flowClass.allCallbacks[inRef]) return Promise.resolve() | ||
const argsCb = { | ||
database, | ||
provider, | ||
state, | ||
globalState, | ||
extensions, | ||
fallBack: fallBack(flags), | ||
@@ -297,2 +358,3 @@ flowDynamic: flowDynamic(flags), | ||
msgToSend = this.flowClass.find(body) || [] | ||
if (msgToSend.length) return sendFlow(msgToSend, from) | ||
@@ -329,10 +391,18 @@ | ||
sendProviderAndSave = async (numberOrId, ctxMessage) => { | ||
const { answer } = ctxMessage | ||
try { | ||
const { answer } = ctxMessage | ||
logger.log(`[sendProviderAndSave]: `, ctxMessage) | ||
if (answer && answer.length && answer !== '__call_action__') { | ||
await this.providerClass.sendMessage(numberOrId, answer, ctxMessage) | ||
logger.log(`[providerClass.sendMessage]: `, ctxMessage) | ||
await this.databaseClass.save({ ...ctxMessage, from: numberOrId }) | ||
logger.log(`[databaseClass.save]: `, ctxMessage) | ||
} | ||
if (answer && answer.length && answer !== '__call_action__') { | ||
await this.providerClass.sendMessage(numberOrId, answer, ctxMessage) | ||
await this.databaseClass.save({ ...ctxMessage, from: numberOrId }) | ||
return Promise.resolve | ||
} catch (err) { | ||
logger.log(`[ERROR.save]: `, ctxMessage) | ||
console.log('ERROR:Enviando') | ||
return Promise.reject | ||
} | ||
return | ||
} | ||
@@ -363,11 +433,11 @@ | ||
sendFlowSimple = async (messageToSend, numberOrId) => { | ||
const queue = [] | ||
for (const ctxMessage of messageToSend) { | ||
const delayMs = ctxMessage?.options?.delay ?? this.generalArgs.delay ?? 0 | ||
if (delayMs) await delay(delayMs) | ||
QueuePrincipal.enqueue(() => this.sendProviderAndSave(numberOrId, ctxMessage)) | ||
await this.queuePrincipal.enqueue(numberOrId, () => this.sendProviderAndSave(numberOrId, ctxMessage)) | ||
// await queuePromises.dequeue() | ||
} | ||
return Promise.all(queue) | ||
return Promise.resolve | ||
} | ||
} | ||
module.exports = CoreClass |
{ | ||
"name": "@bot-whatsapp/bot", | ||
"version": "0.1.30", | ||
"version": "0.1.31", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./lib/bundle.bot.cjs", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
108621
2899