@bot-whatsapp/bot
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -61,9 +61,9 @@ const { toCtx } = require('../io/methods') | ||
/** | ||
* | ||
* @param {*} messageInComming | ||
* GLOSSARY.md | ||
* @param {*} messageCtxInComming | ||
* @returns | ||
*/ | ||
handleMsg = async (messageInComming) => { | ||
logger.log(`[handleMsg]: `, messageInComming) | ||
const { body, from } = messageInComming | ||
handleMsg = async (messageCtxInComming) => { | ||
logger.log(`[handleMsg]: `, messageCtxInComming) | ||
const { body, from } = messageCtxInComming | ||
let msgToSend = [] | ||
@@ -88,3 +88,3 @@ let fallBackFlag = false | ||
// 📄 [options: fallback]: esta funcion se encarga de repetir el ultimo mensaje | ||
// 📄 [options: fallBack]: esta funcion se encarga de repetir el ultimo mensaje | ||
const fallBack = () => { | ||
@@ -97,10 +97,38 @@ fallBackFlag = true | ||
// 📄 [options: callback]: Si se tiene un callback se ejecuta | ||
if (!fallBackFlag && refToContinue && prevMsg?.options?.callback) { | ||
const indexFlow = this.flowClass.findIndexByRef(refToContinue?.ref) | ||
this.flowClass.allCallbacks[indexFlow].callback(messageInComming, { | ||
// 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes | ||
// para evitar bloque de whatsapp | ||
const flowDynamic = (listMsg = [], optListMsg = { limit: 3 }) => { | ||
if (!Array.isArray(listMsg)) | ||
throw new Error('Esto debe ser un ARRAY') | ||
const parseListMsg = listMsg | ||
.map(({ body }, index) => | ||
toCtx({ | ||
body, | ||
from, | ||
keyword: null, | ||
index, | ||
}) | ||
) | ||
.slice(0, optListMsg.limit) | ||
msgToSend = parseListMsg | ||
this.sendFlow(msgToSend, from) | ||
return | ||
} | ||
// 📄 Se encarga de revisar si el contexto del mensaje tiene callback y ejecutarlo | ||
const cbEveryCtx = (inRef) => { | ||
this.flowClass.allCallbacks[inRef](messageCtxInComming, { | ||
fallBack, | ||
flowDynamic, | ||
}) | ||
} | ||
// 📄 [options: callback]: Si se tiene un callback se ejecuta | ||
if (!fallBackFlag) { | ||
for (const ite of this.flowClass.find(body)) { | ||
cbEveryCtx(ite?.ref) | ||
} | ||
} | ||
// 📄🤘(tiene return) [options: nested(array)]: Si se tiene flujos hijos los implementa | ||
@@ -114,2 +142,7 @@ if (!fallBackFlag && prevMsg?.options?.nested?.length) { | ||
msgToSend = this.flowClass.find(body, false, flowStandalone) || [] | ||
for (const ite of msgToSend) { | ||
cbEveryCtx(ite?.ref) | ||
} | ||
this.sendFlow(msgToSend, from) | ||
@@ -116,0 +149,0 @@ return |
const { toSerialize } = require('./methods/toSerialize') | ||
const { flatObject } = require('../utils/flattener') | ||
@@ -11,6 +12,3 @@ class FlowClass { | ||
this.allCallbacks = _flow | ||
.map((cbIn) => cbIn.ctx.callbacks) | ||
.flat(2) | ||
.map((c, i) => ({ callback: c?.callback, index: i })) | ||
this.allCallbacks = flatObject(_flow) | ||
@@ -17,0 +15,0 @@ const mergeToJsonSerialize = Object.keys(_flow) |
@@ -0,2 +1,4 @@ | ||
const { flatObject } = require('../../utils/flattener') | ||
const { generateRef } = require('../../utils/hash') | ||
const { addChild } = require('./addChild') | ||
const { toJson } = require('./toJson') | ||
@@ -30,11 +32,27 @@ /** | ||
const getNested = () => ({ | ||
nested: Array.isArray(nested) ? nested : [], | ||
}) | ||
const getNested = () => { | ||
let flatNested = [] | ||
if (Array.isArray(nested)) { | ||
for (const iterator of nested) { | ||
flatNested = [...flatNested, ...addChild(iterator)] | ||
} | ||
const callback = | ||
typeof cb === 'function' | ||
? cb | ||
: () => console.log('Callback no definida') | ||
return { | ||
nested: flatNested, | ||
} | ||
} | ||
return { | ||
nested: addChild(nested), | ||
} | ||
} | ||
/** | ||
* Esta funcion aplana y busca los callback anidados de los hijos | ||
* @returns | ||
*/ | ||
const getCbFromNested = () => | ||
flatObject(Array.isArray(nested) ? nested : [nested]) | ||
const callback = typeof cb === 'function' ? cb : () => null | ||
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx | ||
@@ -66,8 +84,8 @@ | ||
const callbacks = [].concat(inCtx.callbacks).concat([ | ||
{ | ||
ref: lastCtx.ref, | ||
callback, | ||
}, | ||
]) | ||
getCbFromNested() | ||
const callbacks = { | ||
...inCtx.callbacks, | ||
...getCbFromNested(), | ||
[ref]: callback, | ||
} | ||
@@ -74,0 +92,0 @@ return { |
{ | ||
"name": "@bot-whatsapp/bot", | ||
"version": "v0.1.2", | ||
"version": "v0.1.3", | ||
"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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
39973
1188
788
32