@bot-whatsapp/bot
Advanced tools
Comparing version 0.0.1 to 0.0.4-alpha.0
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 logger = new Console({ | ||
stdout: createWriteStream(`${process.cwd()}/core.class.log`), | ||
}) | ||
/** | ||
@@ -24,4 +31,11 @@ * [ ] Escuchar eventos del provider asegurarte que los provider emitan eventos | ||
/** | ||
* Manejador de eventos | ||
*/ | ||
listenerBusEvents = () => [ | ||
{ | ||
event: 'preinit', | ||
func: () => printer('Iniciando provider espere...'), | ||
}, | ||
{ | ||
event: 'require_action', | ||
@@ -48,12 +62,15 @@ func: ({ instructions, title = '⚡⚡ ACCION REQUERIDA ⚡⚡' }) => | ||
/** | ||
* @private | ||
* @param {*} ctxMessage | ||
* | ||
* @param {*} messageInComming | ||
* @returns | ||
*/ | ||
handleMsg = async (messageInComming) => { | ||
logger.log(`[handleMsg]: `, messageInComming) | ||
const { body, from } = messageInComming | ||
let msgToSend = [] | ||
let fallBackFlag = false | ||
//Consultamos mensaje previo en DB | ||
if (!body.length) return | ||
const prevMsg = await this.databaseClass.getPrevByNumber(from) | ||
//Consultamos for refSerializada en el flow actual | ||
const refToContinue = this.flowClass.findBySerialize( | ||
@@ -72,10 +89,20 @@ prevMsg?.refSerialize | ||
//Si se tiene un callback se ejecuta | ||
if (refToContinue && prevMsg?.options?.callback) { | ||
// 📄 [options: fallback]: esta funcion se encarga de repetir el ultimo mensaje | ||
const fallBack = () => { | ||
fallBackFlag = true | ||
msgToSend = this.flowClass.find(refToContinue?.keyword, true) || [] | ||
this.sendFlow(msgToSend, from) | ||
return refToContinue | ||
} | ||
// 📄 [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) | ||
this.flowClass.allCallbacks[indexFlow].callback(messageInComming, { | ||
fallBack, | ||
}) | ||
} | ||
//Si se tiene anidaciones de flows, si tienes anidados obligatoriamente capture:true | ||
if (prevMsg?.options?.nested?.length) { | ||
// 📄🤘(tiene return) [options: nested(array)]: Si se tiene flujos hijos los implementa | ||
if (!fallBackFlag && prevMsg?.options?.nested?.length) { | ||
const nestedRef = prevMsg.options.nested | ||
@@ -91,16 +118,28 @@ const flowStandalone = nestedRef.map((f) => ({ | ||
//Consultamos si se espera respuesta por parte de cliente "Ejemplo: Dime tu nombre" | ||
if (!prevMsg?.options?.nested?.length && prevMsg?.options?.capture) { | ||
msgToSend = this.flowClass.find(refToContinue?.ref, true) || [] | ||
} else { | ||
msgToSend = this.flowClass.find(body) || [] | ||
// 📄🤘(tiene return) [options: capture (boolean)]: Si se tiene option boolean | ||
if (!fallBackFlag && !prevMsg?.options?.nested?.length) { | ||
const typeCapture = typeof prevMsg?.options?.capture | ||
const valueCapture = prevMsg?.options?.capture | ||
if (['string', 'boolean'].includes(typeCapture) && valueCapture) { | ||
msgToSend = this.flowClass.find(refToContinue?.ref, true) || [] | ||
this.sendFlow(msgToSend, from) | ||
return | ||
} | ||
} | ||
msgToSend = this.flowClass.find(body) || [] | ||
this.sendFlow(msgToSend, from) | ||
} | ||
/** | ||
* Enviar mensaje con contexto atraves del proveedor de whatsapp | ||
* @param {*} numberOrId | ||
* @param {*} ctxMessage ver más en GLOSSARY.md | ||
* @returns | ||
*/ | ||
sendProviderAndSave = (numberOrId, ctxMessage) => { | ||
const { answer } = ctxMessage | ||
return Promise.all([ | ||
this.providerClass.sendMessage(numberOrId, answer), | ||
this.providerClass.sendMessage(numberOrId, answer, ctxMessage), | ||
this.databaseClass.save({ ...ctxMessage, from: numberOrId }), | ||
@@ -110,6 +149,10 @@ ]) | ||
sendFlow = (messageToSend, numberOrId) => { | ||
sendFlow = async (messageToSend, numberOrId) => { | ||
const queue = [] | ||
for (const ctxMessage of messageToSend) { | ||
queue.push(this.sendProviderAndSave(numberOrId, ctxMessage)) | ||
const delayMs = ctxMessage?.options?.delay || 0 | ||
if (delayMs) await delay(delayMs) | ||
Queue.enqueue(() => | ||
this.sendProviderAndSave(numberOrId, ctxMessage) | ||
) | ||
} | ||
@@ -116,0 +159,0 @@ return Promise.all(queue) |
@@ -24,2 +24,3 @@ const { toSerialize } = require('./methods/toSerialize') | ||
find = (keyOrWord, symbol = false, overFlow = null) => { | ||
keyOrWord = `${keyOrWord}` | ||
let capture = false | ||
@@ -30,12 +31,9 @@ let messages = [] | ||
const mapSensitiveString = (str, flag = false) => { | ||
if (!flag && Array.isArray(str)) { | ||
return str.map((c) => c.toLowerCase()) | ||
/** Retornar expresion regular para buscar coincidencia */ | ||
const mapSensitive = (str, flag = false) => { | ||
const regexSensitive = flag ? 'g' : 'i' | ||
if (Array.isArray(str)) { | ||
return new RegExp(str.join('|'), regexSensitive) | ||
} | ||
if (!flag && typeof str === 'string') { | ||
return str.toLowerCase() | ||
} | ||
return str | ||
return new RegExp(str, regexSensitive) | ||
} | ||
@@ -47,4 +45,2 @@ | ||
keyOrWord = mapSensitiveString(keyOrWord, sensitive) | ||
if (capture) return messages | ||
@@ -57,5 +53,5 @@ | ||
} else { | ||
refSymbol = flow.find((c) => | ||
mapSensitiveString(c.keyword, sensitive).includes(keyOrWord) | ||
) | ||
refSymbol = flow.find((c) => { | ||
return mapSensitive(c.keyword, sensitive).test(keyOrWord) | ||
}) | ||
if (refSymbol?.ref) findIn(refSymbol.ref, true) | ||
@@ -62,0 +58,0 @@ return messages |
@@ -0,0 +0,0 @@ const commonjs = require('@rollup/plugin-commonjs') |
{ | ||
"name": "@bot-whatsapp/bot", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "./lib/bundle.bot.cjs", | ||
"scripts": { | ||
"bot:rollup": "node ../../node_modules/.bin/rollup index.js --config ./rollup-cli.config.js", | ||
"format:check": "prettier --check .", | ||
"format:write": "prettier --write .", | ||
"lint:check": "eslint .", | ||
"lint:fix": "eslint --fix .", | ||
"test.unit": "cross-env NODE_ENV=test node ../../node_modules/uvu/bin.js tests" | ||
}, | ||
"keywords": [], | ||
"files": [ | ||
"./lib/bundle.bot.cjs", | ||
"./provider/*", | ||
"./core/*", | ||
"./io/*" | ||
], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@bot-whatsapp/cli": "*", | ||
"@bot-whatsapp/database": "*", | ||
"@bot-whatsapp/provider": "*", | ||
"kleur": "^4.1.5" | ||
}, | ||
"dependencies": { | ||
"dotenv": "^16.0.3" | ||
} | ||
} | ||
"name": "@bot-whatsapp/bot", | ||
"version": "0.0.4-alpha.0", | ||
"description": "", | ||
"main": "./lib/bundle.bot.cjs", | ||
"scripts": { | ||
"bot:rollup": "node ../../node_modules/.bin/rollup index.js --config ./rollup-cli.config.js", | ||
"format:check": "prettier --check .", | ||
"format:write": "prettier --write .", | ||
"lint:check": "eslint .", | ||
"lint:fix": "eslint --fix .", | ||
"test.unit": "cross-env NODE_ENV=test node ../../node_modules/uvu/bin.js tests" | ||
}, | ||
"keywords": [], | ||
"files": [ | ||
"./lib/bundle.bot.cjs", | ||
"./provider/*", | ||
"./core/*", | ||
"./io/*" | ||
], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@bot-whatsapp/cli": "*", | ||
"@bot-whatsapp/database": "*", | ||
"@bot-whatsapp/provider": "*", | ||
"kleur": "^4.1.5" | ||
}, | ||
"dependencies": { | ||
"dotenv": "^16.0.3" | ||
} | ||
} |
@@ -0,0 +0,0 @@ const { EventEmitter } = require('node:events') |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
30880
896
7