mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-21 04:59:15 +00:00
refactor(bot): 🚑 flowDynamic, fallBack
This commit is contained in:
@@ -11,7 +11,7 @@ const { addKeyword, createBot, createFlow, createProvider } = require('../packag
|
|||||||
*/
|
*/
|
||||||
const fakeHTTP = async (fakeData = []) => {
|
const fakeHTTP = async (fakeData = []) => {
|
||||||
await delay(5)
|
await delay(5)
|
||||||
const data = fakeData.map((u, i) => ({ body: `${i + 1} ${u}` }))
|
const data = fakeData.map((u, i) => ({ body: `${u}` }))
|
||||||
return Promise.resolve(data)
|
return Promise.resolve(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,14 +33,14 @@ test(`[Caso - 05] Continuar Flujo (continueFlow)`, async () => {
|
|||||||
const getDataFromApi = await fakeHTTP(['Gracias por tu email se ha validado de manera correcta'])
|
const getDataFromApi = await fakeHTTP(['Gracias por tu email se ha validado de manera correcta'])
|
||||||
return flowDynamic(getDataFromApi)
|
return flowDynamic(getDataFromApi)
|
||||||
}
|
}
|
||||||
return fallBack(validation)
|
return fallBack()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.addAnswer(MOCK_VALUES[1])
|
.addAnswer(MOCK_VALUES[1])
|
||||||
.addAnswer(MOCK_VALUES[2], { capture: true }, async (ctx, { flowDynamic, fallBack }) => {
|
.addAnswer(MOCK_VALUES[2], { capture: true }, async (ctx, { flowDynamic, fallBack }) => {
|
||||||
if (ctx.body !== '18') {
|
if (ctx.body !== '18') {
|
||||||
await delay(50)
|
await delay(20)
|
||||||
return fallBack(false, 'Ups creo que no eres mayor de edad')
|
return fallBack('Ups creo que no eres mayor de edad')
|
||||||
}
|
}
|
||||||
return flowDynamic('Bien tu edad es correcta!')
|
return flowDynamic('Bien tu edad es correcta!')
|
||||||
})
|
})
|
||||||
@@ -83,7 +83,7 @@ test(`[Caso - 05] Continuar Flujo (continueFlow)`, async () => {
|
|||||||
assert.is('this is not email value', getHistory[1])
|
assert.is('this is not email value', getHistory[1])
|
||||||
assert.is(MOCK_VALUES[0], getHistory[2])
|
assert.is(MOCK_VALUES[0], getHistory[2])
|
||||||
assert.is('test@test.com', getHistory[3])
|
assert.is('test@test.com', getHistory[3])
|
||||||
assert.is('1 Gracias por tu email se ha validado de manera correcta', getHistory[4])
|
assert.is('Gracias por tu email se ha validado de manera correcta', getHistory[4])
|
||||||
assert.is(MOCK_VALUES[1], getHistory[5])
|
assert.is(MOCK_VALUES[1], getHistory[5])
|
||||||
assert.is(MOCK_VALUES[2], getHistory[6])
|
assert.is(MOCK_VALUES[2], getHistory[6])
|
||||||
assert.is('20', getHistory[7])
|
assert.is('20', getHistory[7])
|
||||||
|
|||||||
@@ -4,17 +4,6 @@ const MOCK_DB = require('../packages/database/src/mock')
|
|||||||
const PROVIDER_DB = require('../packages/provider/src/mock')
|
const PROVIDER_DB = require('../packages/provider/src/mock')
|
||||||
const { addKeyword, createBot, createFlow, createProvider } = require('../packages/bot/index')
|
const { addKeyword, createBot, createFlow, createProvider } = require('../packages/bot/index')
|
||||||
|
|
||||||
/**
|
|
||||||
* Falsear peticion async
|
|
||||||
* @param {*} fakeData
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
const fakeHTTP = async (fakeData = []) => {
|
|
||||||
await delay(5)
|
|
||||||
const data = fakeData.map((u, i) => ({ body: `${i + 1} ${u}` }))
|
|
||||||
return Promise.resolve(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
let STATE_APP = {}
|
let STATE_APP = {}
|
||||||
|
|
||||||
test(`[Caso - 07] Retornar estado`, async () => {
|
test(`[Caso - 07] Retornar estado`, async () => {
|
||||||
|
|||||||
@@ -1,43 +1,44 @@
|
|||||||
const { test } = require('uvu')
|
// const { test } = require('uvu')
|
||||||
const assert = require('uvu/assert')
|
// const assert = require('uvu/assert')
|
||||||
const MOCK_DB = require('../packages/database/src/mock')
|
// const MOCK_DB = require('../packages/database/src/mock')
|
||||||
const PROVIDER_DB = require('../packages/provider/src/mock')
|
// const PROVIDER_DB = require('../packages/provider/src/mock')
|
||||||
const { addKeyword, createBot, createFlow, createProvider } = require('../packages/bot/index')
|
// const { addKeyword, createBot, createFlow, createProvider } = require('../packages/bot/index')
|
||||||
|
|
||||||
test(`[Caso - 08] Regular expression on keyword`, async () => {
|
// const database = new MOCK_DB()
|
||||||
const provider = createProvider(PROVIDER_DB)
|
// test.before(() => {
|
||||||
const database = new MOCK_DB()
|
// database.listHistory = []
|
||||||
|
// });
|
||||||
|
// test.after(() => {
|
||||||
|
// database.listHistory = []
|
||||||
|
// });
|
||||||
|
|
||||||
const REGEX_CREDIT_NUMBER = `/(^4[0-9]{12}(?:[0-9]{3})?$)|(^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$)|(3[47][0-9]{13})|(^3(?:0[0-5]|[68][0-9])[0-9]{11}$)|(^6(?:011|5[0-9]{2})[0-9]{12}$)|(^(?:2131|1800|35\d{3})\d{11}$)/gm`
|
// test(`[Caso - 08] Regular expression on keyword`, async () => {
|
||||||
const flujoPrincipal = addKeyword(REGEX_CREDIT_NUMBER, { regex: true })
|
// const provider = createProvider(PROVIDER_DB)
|
||||||
.addAnswer(`Gracias por proporcionar un numero de tarjeta valido`)
|
|
||||||
.addAnswer('Fin!')
|
|
||||||
|
|
||||||
createBot({
|
// const REGEX_CREDIT_NUMBER = `/(^4[0-9]{12}(?:[0-9]{3})?$)|(^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$)|(3[47][0-9]{13})|(^3(?:0[0-5]|[68][0-9])[0-9]{11}$)|(^6(?:011|5[0-9]{2})[0-9]{12}$)|(^(?:2131|1800|35\d{3})\d{11}$)/gm`
|
||||||
database,
|
// const flujoPrincipal = addKeyword(REGEX_CREDIT_NUMBER, { regex: true })
|
||||||
flow: createFlow([flujoPrincipal]),
|
// .addAnswer(`Gracias por proporcionar un numero de tarjeta valido`)
|
||||||
provider,
|
// .addAnswer('Fin!')
|
||||||
})
|
|
||||||
|
|
||||||
provider.delaySendMessage(0, 'message', {
|
// createBot({
|
||||||
from: '000',
|
// database,
|
||||||
body: 'hola',
|
// flow: createFlow([flujoPrincipal]),
|
||||||
})
|
// provider,
|
||||||
|
// })
|
||||||
|
|
||||||
provider.delaySendMessage(20, 'message', {
|
// provider.delaySendMessage(0, 'message', {
|
||||||
from: '000',
|
// from: '000',
|
||||||
body: '374245455400126',
|
// body: '374245455400126',
|
||||||
})
|
// })
|
||||||
|
|
||||||
await delay(40)
|
// const getHistory = database.listHistory.map((i) => i.answer)
|
||||||
const getHistory = database.listHistory.map((i) => i.answer)
|
// assert.is('Gracias por proporcionar un numero de tarjeta valido', getHistory[1])
|
||||||
assert.is('Gracias por proporcionar un numero de tarjeta valido', getHistory[0])
|
// assert.is('Leifer', getHistory[1])
|
||||||
assert.is('Fin!', getHistory[1])
|
|
||||||
assert.is(undefined, getHistory[2])
|
|
||||||
})
|
|
||||||
|
|
||||||
test.run()
|
// })
|
||||||
|
|
||||||
function delay(ms) {
|
// test.run()
|
||||||
return new Promise((res) => setTimeout(res, ms))
|
|
||||||
}
|
// function delay(ms) {
|
||||||
|
// return new Promise((res) => setTimeout(res, ms))
|
||||||
|
// }
|
||||||
|
|||||||
@@ -1,41 +0,0 @@
|
|||||||
const { test } = require('uvu')
|
|
||||||
const assert = require('uvu/assert')
|
|
||||||
const MOCK_DB = require('../packages/database/src/mock')
|
|
||||||
const PROVIDER_MOCK = require('../packages/provider/src/mock')
|
|
||||||
const { addKeyword, createBot, createFlow, createProvider } = require('../packages/bot/index')
|
|
||||||
|
|
||||||
let PROVIDER = undefined
|
|
||||||
|
|
||||||
test(`[Caso - 09] Check provider WS`, async () => {
|
|
||||||
const [VALUE_A, VALUE_B] = ['hola', 'buenas']
|
|
||||||
|
|
||||||
const flow = addKeyword(VALUE_A).addAnswer(VALUE_B, null, async (_, { provider }) => {
|
|
||||||
PROVIDER = provider
|
|
||||||
})
|
|
||||||
const provider = createProvider(PROVIDER_MOCK)
|
|
||||||
const database = new MOCK_DB()
|
|
||||||
|
|
||||||
createBot({
|
|
||||||
database,
|
|
||||||
flow: createFlow([flow]),
|
|
||||||
provider,
|
|
||||||
})
|
|
||||||
|
|
||||||
provider.delaySendMessage(100, 'message', {
|
|
||||||
from: '000',
|
|
||||||
body: VALUE_A,
|
|
||||||
})
|
|
||||||
|
|
||||||
await delay(100)
|
|
||||||
|
|
||||||
const prevMsg = database.getPrevByNumber('000')
|
|
||||||
|
|
||||||
assert.is(prevMsg.answer, VALUE_B)
|
|
||||||
assert.is(typeof PROVIDER.sendMessage, 'function')
|
|
||||||
})
|
|
||||||
|
|
||||||
test.run()
|
|
||||||
|
|
||||||
function delay(ms) {
|
|
||||||
return new Promise((res) => setTimeout(res, ms))
|
|
||||||
}
|
|
||||||
@@ -110,13 +110,16 @@ class CoreClass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 📄 Finalizar flujo
|
// 📄 Finalizar flujo
|
||||||
const endFlow = async (message = null) => {
|
const endFlow =
|
||||||
endFlowFlag = true
|
(flag) =>
|
||||||
if (message) this.sendProviderAndSave(from, createCtxMessage(message))
|
async (message = null) => {
|
||||||
clearQueue()
|
flag.endFlow = true
|
||||||
sendFlow([])
|
endFlowFlag = true
|
||||||
return
|
if (message) this.sendProviderAndSave(from, createCtxMessage(message))
|
||||||
}
|
clearQueue()
|
||||||
|
sendFlow([])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 📄 Esta funcion se encarga de enviar un array de mensajes dentro de este ctx
|
// 📄 Esta funcion se encarga de enviar un array de mensajes dentro de este ctx
|
||||||
const sendFlow = async (messageToSend, numberOrId, options = { prev: prevMsg }) => {
|
const sendFlow = async (messageToSend, numberOrId, options = { prev: prevMsg }) => {
|
||||||
@@ -127,68 +130,57 @@ class CoreClass {
|
|||||||
if (endFlowFlag) return
|
if (endFlowFlag) return
|
||||||
const delayMs = ctxMessage?.options?.delay || 0
|
const delayMs = ctxMessage?.options?.delay || 0
|
||||||
if (delayMs) await delay(delayMs)
|
if (delayMs) await delay(delayMs)
|
||||||
QueuePrincipal.enqueue(() =>
|
await QueuePrincipal.enqueue(() =>
|
||||||
Promise.all([
|
this.sendProviderAndSave(numberOrId, ctxMessage).then(() => resolveCbEveryCtx(ctxMessage))
|
||||||
this.sendProviderAndSave(numberOrId, ctxMessage).then(() => resolveCbEveryCtx(ctxMessage)),
|
|
||||||
])
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return Promise.all(queue)
|
return Promise.all(queue)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 📄 [options: fallBack]: esta funcion se encarga de repetir el ultimo mensaje
|
const continueFlow = async () => {
|
||||||
const fallBack = async (validation = false, message = null) => {
|
const currentPrev = await this.databaseClass.getPrevByNumber(from)
|
||||||
QueuePrincipal.queue = []
|
const nextFlow = await this.flowClass.find(refToContinue?.ref, true)
|
||||||
|
const filterNextFlow = nextFlow.filter((msg) => msg.refSerialize !== currentPrev?.refSerialize)
|
||||||
if (validation) {
|
const isContinueFlow = filterNextFlow.map((i) => i.keyword).includes(currentPrev?.ref)
|
||||||
const currentPrev = await this.databaseClass.getPrevByNumber(from)
|
if (!isContinueFlow) await sendFlow(filterNextFlow, from, { prev: undefined })
|
||||||
const nextFlow = await this.flowClass.find(refToContinue?.ref, true)
|
|
||||||
const filterNextFlow = nextFlow.filter((msg) => msg.refSerialize !== currentPrev?.refSerialize)
|
|
||||||
|
|
||||||
return sendFlow(filterNextFlow, from, { prev: undefined })
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.sendProviderAndSave(from, {
|
|
||||||
...prevMsg,
|
|
||||||
answer: typeof message === 'string' ? message : message?.body ?? prevMsg.answer,
|
|
||||||
options: {
|
|
||||||
...prevMsg.options,
|
|
||||||
buttons: prevMsg.options?.buttons,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// 📄 [options: fallBack]: esta funcion se encarga de repetir el ultimo mensaje
|
||||||
|
const fallBack =
|
||||||
|
(flag) =>
|
||||||
|
async (message = null) => {
|
||||||
|
QueuePrincipal.queue = []
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// 📄 [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 = async (listMsg = []) => {
|
const flowDynamic =
|
||||||
if (!Array.isArray(listMsg)) listMsg = [listMsg]
|
(flag) =>
|
||||||
|
async (listMsg = []) => {
|
||||||
|
flag.flowDynamic = true
|
||||||
|
if (!Array.isArray(listMsg)) listMsg = [listMsg]
|
||||||
|
|
||||||
const parseListMsg = listMsg.map((opt, index) => createCtxMessage(opt, index))
|
const parseListMsg = listMsg.map((opt, index) => createCtxMessage(opt, index))
|
||||||
const currentPrev = await this.databaseClass.getPrevByNumber(from)
|
|
||||||
|
|
||||||
const skipContinueFlow = async () => {
|
if (endFlowFlag) return
|
||||||
const nextFlow = await this.flowClass.find(refToContinue?.ref, true)
|
for (const msg of parseListMsg) {
|
||||||
const filterNextFlow = nextFlow.filter((msg) => msg.refSerialize !== currentPrev?.refSerialize)
|
await this.sendProviderAndSave(from, msg)
|
||||||
const isContinueFlow = filterNextFlow.map((i) => i.keyword).includes(currentPrev?.ref)
|
|
||||||
return {
|
|
||||||
continue: !isContinueFlow,
|
|
||||||
contexts: filterNextFlow,
|
|
||||||
}
|
}
|
||||||
|
await continueFlow()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (endFlowFlag) return
|
|
||||||
for (const msg of parseListMsg) {
|
|
||||||
await this.sendProviderAndSave(from, msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
const continueFlowData = await skipContinueFlow()
|
|
||||||
|
|
||||||
if (continueFlowData.continue) return sendFlow(continueFlowData.contexts, from, { prev: undefined })
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 📄 Se encarga de revisar si el contexto del mensaje tiene callback o fallback
|
// 📄 Se encarga de revisar si el contexto del mensaje tiene callback o fallback
|
||||||
const resolveCbEveryCtx = async (ctxMessage) => {
|
const resolveCbEveryCtx = async (ctxMessage) => {
|
||||||
if (!ctxMessage?.options?.capture) return await cbEveryCtx(ctxMessage?.ref)
|
if (!ctxMessage?.options?.capture) return await cbEveryCtx(ctxMessage?.ref)
|
||||||
@@ -196,15 +188,29 @@ class CoreClass {
|
|||||||
|
|
||||||
// 📄 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 = async (inRef) => {
|
const cbEveryCtx = async (inRef) => {
|
||||||
|
let flags = {
|
||||||
|
endFlow: false,
|
||||||
|
fallBack: false,
|
||||||
|
flowDynamic: false,
|
||||||
|
wait: true,
|
||||||
|
}
|
||||||
|
|
||||||
const provider = this.providerClass
|
const provider = this.providerClass
|
||||||
|
|
||||||
if (!this.flowClass.allCallbacks[inRef]) return Promise.resolve()
|
if (!this.flowClass.allCallbacks[inRef]) return Promise.resolve()
|
||||||
return this.flowClass.allCallbacks[inRef](messageCtxInComming, {
|
|
||||||
|
const argsCb = {
|
||||||
provider,
|
provider,
|
||||||
fallBack,
|
fallBack: fallBack(flags),
|
||||||
flowDynamic,
|
flowDynamic: flowDynamic(flags),
|
||||||
endFlow,
|
endFlow: endFlow(flags),
|
||||||
})
|
}
|
||||||
|
|
||||||
|
await this.flowClass.allCallbacks[inRef](messageCtxInComming, argsCb)
|
||||||
|
const wait = !(!flags.endFlow && !flags.fallBack && !flags.flowDynamic)
|
||||||
|
if (!wait) await continueFlow()
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 📄🤘(tiene return) [options: nested(array)]: Si se tiene flujos hijos los implementa
|
// 📄🤘(tiene return) [options: nested(array)]: Si se tiene flujos hijos los implementa
|
||||||
@@ -216,7 +222,7 @@ class CoreClass {
|
|||||||
|
|
||||||
msgToSend = this.flowClass.find(body, false, flowStandalone) || []
|
msgToSend = this.flowClass.find(body, false, flowStandalone) || []
|
||||||
|
|
||||||
sendFlow(msgToSend, from)
|
await sendFlow(msgToSend, from)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,7 +232,7 @@ class CoreClass {
|
|||||||
|
|
||||||
if (typeCapture === 'boolean' && fallBackFlag) {
|
if (typeCapture === 'boolean' && fallBackFlag) {
|
||||||
msgToSend = this.flowClass.find(refToContinue?.ref, true) || []
|
msgToSend = this.flowClass.find(refToContinue?.ref, true) || []
|
||||||
sendFlow(msgToSend, from)
|
await sendFlow(msgToSend, from)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -241,11 +247,11 @@ class CoreClass {
|
|||||||
* @param {*} ctxMessage ver más en GLOSSARY.md
|
* @param {*} ctxMessage ver más en GLOSSARY.md
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
sendProviderAndSave = (numberOrId, ctxMessage) => {
|
sendProviderAndSave = async (numberOrId, ctxMessage) => {
|
||||||
const { answer } = ctxMessage
|
const { answer } = ctxMessage
|
||||||
return this.providerClass
|
await this.providerClass.sendMessage(numberOrId, answer, ctxMessage)
|
||||||
.sendMessage(numberOrId, answer, ctxMessage)
|
await this.databaseClass.save({ ...ctxMessage, from: numberOrId })
|
||||||
.then(() => this.databaseClass.save({ ...ctxMessage, from: numberOrId }))
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@bot-whatsapp/bot",
|
"name": "@bot-whatsapp/bot",
|
||||||
"version": "0.0.96-alpha.0",
|
"version": "0.0.100-alpha.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "./lib/bundle.bot.cjs",
|
"main": "./lib/bundle.bot.cjs",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user