@bot-whatsapp/bot
Advanced tools
Comparing version 0.0.218-alpha.0 to 0.0.219-alpha.0
@@ -177,34 +177,35 @@ const { EventEmitter } = require('node:events') | ||
(flag) => | ||
async (messages = null, options = { fromGotoFlow: false, end: false }) => { | ||
flag.endFlow = true | ||
endFlowFlag = true | ||
async (messages = null, options = { fromGotoFlow: false, end: false }) => { | ||
flag.endFlow = true | ||
endFlowFlag = true | ||
if (typeof messages === 'string' || messages === null) { | ||
await this.sendProviderAndSave(from, createCtxMessage(messages)) | ||
clearQueue() | ||
return | ||
} | ||
if (typeof messages === 'string' || messages === null) { | ||
// await this.sendProviderAndSave(from, createCtxMessage(messages)) | ||
await this.sendProviderAndSave(from, createCtxMessage('__end_flow__')) | ||
clearQueue() | ||
return | ||
} | ||
// Procesos de callback que se deben execute como exepciones | ||
if (Array.isArray(messages)) { | ||
// console.log('options.fromGotoFlow', messages) | ||
// Procesos de callback que se deben execute como exepciones | ||
if (Array.isArray(messages)) { | ||
// console.log('options.fromGotoFlow', messages) | ||
// const indexLimit = messages.findIndex((m) => m.ref === inRef) | ||
for (const iteratorCtxMessage of messages) { | ||
// console.log(`Counter ${indexLimit}`) | ||
// if(indexLimit !== -1 && counterFor === indexLimit) break | ||
const scopeCtx = await resolveCbEveryCtx(iteratorCtxMessage, { | ||
omitEndFlow: options.fromGotoFlow, | ||
idleCtx: !!iteratorCtxMessage?.options?.idle, | ||
triggerKey: iteratorCtxMessage.keyword.startsWith('key_'), | ||
}) | ||
// const indexLimit = messages.findIndex((m) => m.ref === inRef) | ||
for (const iteratorCtxMessage of messages) { | ||
// console.log(`Counter ${indexLimit}`) | ||
// if(indexLimit !== -1 && counterFor === indexLimit) break | ||
const scopeCtx = await resolveCbEveryCtx(iteratorCtxMessage, { | ||
omitEndFlow: options.fromGotoFlow, | ||
idleCtx: !!iteratorCtxMessage?.options?.idle, | ||
triggerKey: iteratorCtxMessage.keyword.startsWith('key_'), | ||
}) | ||
if (scopeCtx?.endFlow) break | ||
if (scopeCtx?.endFlow) break | ||
// options.fromGotoFlow = false | ||
} | ||
// options.fromGotoFlow = false | ||
} | ||
clearQueue() | ||
return | ||
} | ||
clearQueue() | ||
return | ||
} | ||
@@ -215,3 +216,3 @@ // 📄 Esta funcion se encarga de enviar un array de mensajes dentro de este ctx | ||
if (options.prev?.options?.capture && !options?.omitEndFlow) { | ||
if (options.prev?.options?.capture) { | ||
await cbEveryCtx(options.prev?.ref) | ||
@@ -267,3 +268,3 @@ } | ||
const continueFlow = async (initRef = undefined, options = {}) => { | ||
const continueFlow = async (initRef = undefined) => { | ||
const currentPrev = await this.databaseClass.getPrevByNumber(from) | ||
@@ -283,5 +284,5 @@ let nextFlow = (await this.flowClass.find(refToContinue?.ref, true)) ?? [] | ||
if (nextChildMessages?.length) | ||
return exportFunctionsSend(() => sendFlow(nextChildMessages, from, { ...options, ...{ prev: undefined } })) | ||
return exportFunctionsSend(() => sendFlow(nextChildMessages, from, { prev: undefined })) | ||
return exportFunctionsSend(() => sendFlow(filterNextFlow, from, { ...options, ...{ prev: undefined } })) | ||
return exportFunctionsSend(() => sendFlow(filterNextFlow, from, { prev: undefined })) | ||
} | ||
@@ -292,51 +293,51 @@ } | ||
(flag) => | ||
async (message = null) => { | ||
this.queuePrincipal.clearQueue(from) | ||
flag.fallBack = true | ||
await this.sendProviderAndSave(from, { | ||
...prevMsg, | ||
answer: typeof message === 'string' ? message : message?.body ?? prevMsg.answer, | ||
options: { | ||
...prevMsg.options, | ||
buttons: prevMsg.options?.buttons, | ||
}, | ||
}) | ||
return | ||
} | ||
async (message = null) => { | ||
this.queuePrincipal.clearQueue(from) | ||
flag.fallBack = true | ||
await this.sendProviderAndSave(from, { | ||
...prevMsg, | ||
answer: typeof message === 'string' ? message : message?.body ?? prevMsg.answer, | ||
options: { | ||
...prevMsg.options, | ||
buttons: prevMsg.options?.buttons, | ||
}, | ||
}) | ||
return | ||
} | ||
const gotoFlow = | ||
(flag) => | ||
async (flowInstance, step = 0) => { | ||
const promises = [] | ||
flag.gotoFlow = true | ||
async (flowInstance, step = 0) => { | ||
const promises = [] | ||
flag.gotoFlow = true | ||
if (!flowInstance?.toJson) { | ||
printer([ | ||
`[POSSIBLE_CIRCULAR_DEPENDENCY]: Se ha detectado una dependencia circular.`, | ||
`Para evitar problemas, te recomendamos utilizar 'require'('./ruta_del_flow')`, | ||
`Ejemplo: gotoFlow(helloFlow) --> gotoFlow(require('./flows/helloFlow.js'))`, | ||
`[INFO]: https://bot-whatsapp.netlify.app/docs/goto-flow/`, | ||
]) | ||
return | ||
} | ||
if (!flowInstance?.toJson) { | ||
printer([ | ||
`[POSSIBLE_CIRCULAR_DEPENDENCY]: Se ha detectado una dependencia circular.`, | ||
`Para evitar problemas, te recomendamos utilizar 'require'('./ruta_del_flow')`, | ||
`Ejemplo: gotoFlow(helloFlow) --> gotoFlow(require('./flows/helloFlow.js'))`, | ||
`[INFO]: https://bot-whatsapp.netlify.app/docs/goto-flow/`, | ||
]) | ||
return | ||
} | ||
await delay(flowInstance?.ctx?.options?.delay ?? 0) | ||
await delay(flowInstance?.ctx?.options?.delay ?? 0) | ||
const flowTree = flowInstance.toJson() | ||
const flowTree = flowInstance.toJson() | ||
const flowParentId = flowTree[step] | ||
const flowParentId = flowTree[step] | ||
const parseListMsg = await this.flowClass.find(flowParentId?.ref, true, flowTree) | ||
const parseListMsg = await this.flowClass.find(flowParentId?.ref, true, flowTree) | ||
for (const msg of parseListMsg) { | ||
const msgParse = this.flowClass.findSerializeByRef(msg?.ref) | ||
for (const msg of parseListMsg) { | ||
const msgParse = this.flowClass.findSerializeByRef(msg?.ref) | ||
const ctxMessage = { ...msgParse, ...msg } | ||
await this.sendProviderAndSave(from, ctxMessage).then(() => promises.push(ctxMessage)) | ||
} | ||
await endFlow(flag)(promises, { fromGotoFlow: true, ...{ end: endFlowFlag } }) | ||
return | ||
const ctxMessage = { ...msgParse, ...msg } | ||
await this.sendProviderAndSave(from, ctxMessage).then(() => promises.push(ctxMessage)) | ||
} | ||
await endFlow(flag)(promises, { fromGotoFlow: true, ...{ end: endFlowFlag } }) | ||
return | ||
} | ||
// 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes | ||
@@ -347,38 +348,38 @@ // para evitar bloque de whatsapp | ||
(flag, inRef, privateOptions) => | ||
async (listMsg = [], options = { continue: true }) => { | ||
if (!options.hasOwnProperty('continue')) { | ||
options = { ...options, continue: true } | ||
} | ||
async (listMsg = [], options = { continue: true }) => { | ||
if (!options.hasOwnProperty('continue')) { | ||
options = { ...options, continue: true } | ||
} | ||
flag.flowDynamic = true | ||
flag.flowDynamic = true | ||
if (!Array.isArray(listMsg)) { | ||
listMsg = [{ body: listMsg, ...options }] | ||
} | ||
if (!Array.isArray(listMsg)) { | ||
listMsg = [{ body: listMsg, ...options }] | ||
} | ||
const parseListMsg = listMsg.map((opt, index) => createCtxMessage(opt, index)) | ||
const parseListMsg = listMsg.map((opt, index) => createCtxMessage(opt, index)) | ||
// Si endFlowFlag existe y no se omite la finalización del flujo, no hacer nada. | ||
if (endFlowFlag && !privateOptions?.omitEndFlow) { | ||
return | ||
} | ||
// Si endFlowFlag existe y no se omite la finalización del flujo, no hacer nada. | ||
if (endFlowFlag && !privateOptions?.omitEndFlow) { | ||
return | ||
} | ||
this.queuePrincipal.setFingerTime(from, inRef) // Debe decirle al sistema que finalizó el flujo aquí. | ||
this.queuePrincipal.setFingerTime(from, inRef) // Debe decirle al sistema que finalizó el flujo aquí. | ||
for (const msg of parseListMsg) { | ||
if (privateOptions?.idleCtx) { | ||
continue // Saltar al siguiente mensaje si se está en modo idleCtx. | ||
} | ||
const delayMs = msg?.options?.delay ?? this.generalArgs.delay ?? 0 | ||
await delay(delayMs) | ||
await this.sendProviderAndSave(from, msg) | ||
for (const msg of parseListMsg) { | ||
if (privateOptions?.idleCtx) { | ||
continue // Saltar al siguiente mensaje si se está en modo idleCtx. | ||
} | ||
if (options?.continue) { | ||
await continueFlow() | ||
return | ||
} | ||
const delayMs = msg?.options?.delay ?? this.generalArgs.delay ?? 0 | ||
await delay(delayMs) | ||
await this.sendProviderAndSave(from, msg) | ||
} | ||
if (options?.continue) { | ||
await continueFlow() | ||
return | ||
} | ||
return | ||
} | ||
@@ -395,5 +396,5 @@ // 📄 Se encarga de revisar si el contexto del mensaje tiene callback o idle | ||
} | ||
const currentPrev = await this.databaseClass.getPrevByNumber(from) | ||
if(currentPrev?.answer === '__end_flow__') return undefined | ||
// if(endFlowFlag) return | ||
if (ctxMessage?.options?.idle) { | ||
@@ -450,3 +451,3 @@ const run = await cbEveryCtx(ctxMessage?.ref, { ...options, startIdleMs: ctxMessage?.options?.idle }) | ||
if (continueAfterIdle) { | ||
await continueFlow(overCtx, options) | ||
await continueFlow(overCtx) | ||
return | ||
@@ -456,3 +457,3 @@ } | ||
if (ifContinue) { | ||
await continueFlow(undefined, options) | ||
await continueFlow() | ||
return | ||
@@ -562,3 +563,3 @@ } | ||
const { answer } = ctxMessage | ||
if (answer && answer.length && answer !== '__call_action__' && answer !== '__goto_flow__') { | ||
if (answer && answer.length && answer !== '__call_action__' && answer !== '__goto_flow__' && answer !== '__end_flow__') { | ||
if (answer !== '__capture_only_intended__') { | ||
@@ -565,0 +566,0 @@ await this.providerClass.sendMessage(numberOrId, answer, ctxMessage) |
{ | ||
"name": "@bot-whatsapp/bot", | ||
"version": "0.0.218-alpha.0", | ||
"version": "0.0.219-alpha.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./lib/bundle.bot.cjs", |
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
2467
99735