mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-20 04:29:16 +00:00
refactor(bot): ⚡ working fallBack
This commit is contained in:
@@ -2,12 +2,7 @@ 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 {
|
const { addKeyword, createBot, createFlow, createProvider } = require('../packages/bot/index')
|
||||||
addKeyword,
|
|
||||||
createBot,
|
|
||||||
createFlow,
|
|
||||||
createProvider,
|
|
||||||
} = require('../packages/bot/index')
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Falsear peticion async
|
* Falsear peticion async
|
||||||
@@ -21,11 +16,7 @@ const fakeHTTP = async (fakeData = []) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test(`[Caso - 05] Continuar Flujo (continueFlow)`, async () => {
|
test(`[Caso - 05] Continuar Flujo (continueFlow)`, async () => {
|
||||||
const MOCK_VALUES = [
|
const MOCK_VALUES = ['¿CUal es tu email?', 'Continuamos....', '¿Cual es tu edad?']
|
||||||
'¿CUal es tu email?',
|
|
||||||
'Continuamos....',
|
|
||||||
'¿Cual es tu edad?',
|
|
||||||
]
|
|
||||||
const provider = createProvider(PROVIDER_DB)
|
const provider = createProvider(PROVIDER_DB)
|
||||||
const database = new MOCK_DB()
|
const database = new MOCK_DB()
|
||||||
|
|
||||||
@@ -39,26 +30,20 @@ test(`[Caso - 05] Continuar Flujo (continueFlow)`, async () => {
|
|||||||
const validation = ctx.body.includes('@')
|
const validation = ctx.body.includes('@')
|
||||||
|
|
||||||
if (validation) {
|
if (validation) {
|
||||||
const getDataFromApi = await fakeHTTP([
|
const getDataFromApi = await fakeHTTP(['Gracias por tu email se ha validado de manera correcta'])
|
||||||
'Gracias por tu email se ha validado de manera correcta',
|
|
||||||
])
|
|
||||||
return flowDynamic(getDataFromApi)
|
return flowDynamic(getDataFromApi)
|
||||||
}
|
}
|
||||||
return fallBack(validation)
|
return fallBack(validation)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.addAnswer(MOCK_VALUES[1])
|
.addAnswer(MOCK_VALUES[1])
|
||||||
.addAnswer(
|
.addAnswer(MOCK_VALUES[2], { capture: true }, async (ctx, { flowDynamic, fallBack }) => {
|
||||||
MOCK_VALUES[2],
|
if (ctx.body !== '18') {
|
||||||
{ capture: true },
|
await delay(50)
|
||||||
async (ctx, { flowDynamic, fallBack }) => {
|
return fallBack(false, 'Ups creo que no eres mayor de edad')
|
||||||
if (ctx.body !== '18') {
|
|
||||||
await delay(50)
|
|
||||||
return fallBack('Ups creo que no eres mayor de edad')
|
|
||||||
}
|
|
||||||
return flowDynamic('Bien tu edad es correcta!')
|
|
||||||
}
|
}
|
||||||
)
|
return flowDynamic('Bien tu edad es correcta!')
|
||||||
|
})
|
||||||
.addAnswer('Puedes pasar')
|
.addAnswer('Puedes pasar')
|
||||||
|
|
||||||
createBot({
|
createBot({
|
||||||
@@ -98,10 +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(
|
assert.is('1 Gracias por tu email se ha validado de manera correcta', getHistory[4])
|
||||||
'1 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])
|
||||||
|
|||||||
@@ -43,8 +43,7 @@ class CoreClass {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
event: 'require_action',
|
event: 'require_action',
|
||||||
func: ({ instructions, title = '⚡⚡ ACCIÓN REQUERIDA ⚡⚡' }) =>
|
func: ({ instructions, title = '⚡⚡ ACCIÓN REQUERIDA ⚡⚡' }) => printer(instructions, title),
|
||||||
printer(instructions, title),
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
event: 'ready',
|
event: 'ready',
|
||||||
@@ -52,8 +51,7 @@ class CoreClass {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
event: 'auth_failure',
|
event: 'auth_failure',
|
||||||
func: ({ instructions }) =>
|
func: ({ instructions }) => printer(instructions, '⚡⚡ ERROR AUTH ⚡⚡'),
|
||||||
printer(instructions, '⚡⚡ ERROR AUTH ⚡⚡'),
|
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -78,9 +76,7 @@ class CoreClass {
|
|||||||
if (!body.length) return
|
if (!body.length) return
|
||||||
|
|
||||||
let prevMsg = await this.databaseClass.getPrevByNumber(from)
|
let prevMsg = await this.databaseClass.getPrevByNumber(from)
|
||||||
const refToContinue = this.flowClass.findBySerialize(
|
const refToContinue = this.flowClass.findBySerialize(prevMsg?.refSerialize)
|
||||||
prevMsg?.refSerialize
|
|
||||||
)
|
|
||||||
|
|
||||||
if (prevMsg?.ref) {
|
if (prevMsg?.ref) {
|
||||||
const ctxByNumber = toCtx({
|
const ctxByNumber = toCtx({
|
||||||
@@ -93,10 +89,7 @@ class CoreClass {
|
|||||||
|
|
||||||
// 📄 Crar CTX de mensaje (uso private)
|
// 📄 Crar CTX de mensaje (uso private)
|
||||||
const createCtxMessage = (payload = {}, index = 0) => {
|
const createCtxMessage = (payload = {}, index = 0) => {
|
||||||
const body =
|
const body = typeof payload === 'string' ? payload : payload?.body ?? payload?.answer
|
||||||
typeof payload === 'string'
|
|
||||||
? payload
|
|
||||||
: payload?.body ?? payload?.answer
|
|
||||||
const media = payload?.media ?? null
|
const media = payload?.media ?? null
|
||||||
const buttons = payload?.buttons ?? []
|
const buttons = payload?.buttons ?? []
|
||||||
const capture = payload?.capture ?? false
|
const capture = payload?.capture ?? false
|
||||||
@@ -119,21 +112,15 @@ class CoreClass {
|
|||||||
// 📄 Finalizar flujo
|
// 📄 Finalizar flujo
|
||||||
const endFlow = async (message = null) => {
|
const endFlow = async (message = null) => {
|
||||||
endFlowFlag = true
|
endFlowFlag = true
|
||||||
if (message)
|
if (message) this.sendProviderAndSave(from, createCtxMessage(message))
|
||||||
this.sendProviderAndSave(from, createCtxMessage(message))
|
|
||||||
clearQueue()
|
clearQueue()
|
||||||
sendFlow([])
|
sendFlow([])
|
||||||
return
|
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 (
|
const sendFlow = async (messageToSend, numberOrId, options = { prev: prevMsg }) => {
|
||||||
messageToSend,
|
if (options.prev?.options?.capture) await cbEveryCtx(options.prev?.ref)
|
||||||
numberOrId,
|
|
||||||
options = { prev: prevMsg }
|
|
||||||
) => {
|
|
||||||
if (options.prev?.options?.capture)
|
|
||||||
await cbEveryCtx(options.prev?.ref)
|
|
||||||
|
|
||||||
const queue = []
|
const queue = []
|
||||||
for (const ctxMessage of messageToSend) {
|
for (const ctxMessage of messageToSend) {
|
||||||
@@ -142,9 +129,7 @@ class CoreClass {
|
|||||||
if (delayMs) await delay(delayMs)
|
if (delayMs) await delay(delayMs)
|
||||||
QueuePrincipal.enqueue(() =>
|
QueuePrincipal.enqueue(() =>
|
||||||
Promise.all([
|
Promise.all([
|
||||||
this.sendProviderAndSave(numberOrId, ctxMessage).then(
|
this.sendProviderAndSave(numberOrId, ctxMessage).then(() => resolveCbEveryCtx(ctxMessage)),
|
||||||
() => resolveCbEveryCtx(ctxMessage)
|
|
||||||
),
|
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -152,19 +137,26 @@ class CoreClass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 📄 [options: fallBack]: esta funcion se encarga de repetir el ultimo mensaje
|
// 📄 [options: fallBack]: esta funcion se encarga de repetir el ultimo mensaje
|
||||||
const fallBack = async (message = null) => {
|
const fallBack = async (validation = false, message = null) => {
|
||||||
QueuePrincipal.queue = []
|
QueuePrincipal.queue = []
|
||||||
return this.sendProviderAndSave(from, {
|
|
||||||
|
if (validation) {
|
||||||
|
const currentPrev = await this.databaseClass.getPrevByNumber(from)
|
||||||
|
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,
|
...prevMsg,
|
||||||
answer:
|
answer: typeof message === 'string' ? message : message?.body ?? prevMsg.answer,
|
||||||
typeof message === 'string'
|
|
||||||
? message
|
|
||||||
: message?.body ?? prevMsg.answer,
|
|
||||||
options: {
|
options: {
|
||||||
...prevMsg.options,
|
...prevMsg.options,
|
||||||
buttons: prevMsg.options?.buttons,
|
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
|
||||||
@@ -173,9 +165,7 @@ class CoreClass {
|
|||||||
const flowDynamic = async (listMsg = []) => {
|
const flowDynamic = async (listMsg = []) => {
|
||||||
if (!Array.isArray(listMsg)) listMsg = [listMsg]
|
if (!Array.isArray(listMsg)) listMsg = [listMsg]
|
||||||
|
|
||||||
const parseListMsg = listMsg.map((opt, index) =>
|
const parseListMsg = listMsg.map((opt, index) => createCtxMessage(opt, index))
|
||||||
createCtxMessage(opt, index)
|
|
||||||
)
|
|
||||||
const currentPrev = await this.databaseClass.getPrevByNumber(from)
|
const currentPrev = await this.databaseClass.getPrevByNumber(from)
|
||||||
|
|
||||||
if (endFlowFlag) return
|
if (endFlowFlag) return
|
||||||
@@ -184,17 +174,14 @@ class CoreClass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const nextFlow = await this.flowClass.find(refToContinue?.ref, true)
|
const nextFlow = await this.flowClass.find(refToContinue?.ref, true)
|
||||||
const filterNextFlow = nextFlow.filter(
|
const filterNextFlow = nextFlow.filter((msg) => msg.refSerialize !== currentPrev?.refSerialize)
|
||||||
(msg) => msg.refSerialize !== currentPrev?.refSerialize
|
|
||||||
)
|
|
||||||
|
|
||||||
return sendFlow(filterNextFlow, from, { prev: undefined })
|
return sendFlow(filterNextFlow, from, { prev: undefined })
|
||||||
}
|
}
|
||||||
|
|
||||||
// 📄 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)
|
if (!ctxMessage?.options?.capture) return await cbEveryCtx(ctxMessage?.ref)
|
||||||
return await cbEveryCtx(ctxMessage?.ref)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 📄 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
|
||||||
@@ -245,9 +232,7 @@ class CoreClass {
|
|||||||
const { answer } = ctxMessage
|
const { answer } = ctxMessage
|
||||||
return this.providerClass
|
return this.providerClass
|
||||||
.sendMessage(numberOrId, answer, ctxMessage)
|
.sendMessage(numberOrId, answer, ctxMessage)
|
||||||
.then(() =>
|
.then(() => this.databaseClass.save({ ...ctxMessage, from: numberOrId }))
|
||||||
this.databaseClass.save({ ...ctxMessage, from: numberOrId })
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -277,9 +262,7 @@ class CoreClass {
|
|||||||
for (const ctxMessage of messageToSend) {
|
for (const ctxMessage of messageToSend) {
|
||||||
const delayMs = ctxMessage?.options?.delay || 0
|
const delayMs = ctxMessage?.options?.delay || 0
|
||||||
if (delayMs) await delay(delayMs)
|
if (delayMs) await delay(delayMs)
|
||||||
QueuePrincipal.enqueue(() =>
|
QueuePrincipal.enqueue(() => this.sendProviderAndSave(numberOrId, ctxMessage))
|
||||||
this.sendProviderAndSave(numberOrId, ctxMessage)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return Promise.all(queue)
|
return Promise.all(queue)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user