Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bot): ✨ new goto event #647

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions __test__/0.0.2-case.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { suite } = require('uvu')
const assert = require('uvu/assert')
const { addKeyword, createBot, createFlow } = require('../packages/bot/index')
const { setup, clear, delay } = require('../__mocks__/env')

const suiteCase = suite('Flujo: manejo de goto')

suiteCase.before.each(setup)
suiteCase.after.each(clear)

suiteCase(`Debe saltar de flujo`, async ({ database, provider }) => {
const flujoUsuarioRegistrado = addKeyword(['user_register'])
.addAnswer('Hola usuario registrado')
.addAnswer('como estas usuario registrado')

// console.log('ARBOL', flujoUsuarioRegistrado.toJson())

const flujoBienvenida = addKeyword(['hola'])
.addAnswer('Buenas', null, async (_, { gotoFlow, flowDynamic, endFlow }) => {
await delay(10)
await flowDynamic('Usuario registrado DEMO')
await gotoFlow(flujoUsuarioRegistrado)
})
.addAnswer('este mensaje no deberia existir')

createBot({
database,
flow: createFlow([flujoBienvenida]),
provider,
})

await provider.delaySendMessage(0, 'message', {
from: '000',
body: 'hola',
})

await delay(100)
const getHistory = database.listHistory.map((i) => i.answer)
assert.is('Buenas', getHistory[0])
assert.is('Usuario registrado DEMO', getHistory[1])
assert.is('Hola usuario registrado', getHistory[2])
assert.is('como estas usuario registrado', getHistory[3])
assert.is(undefined, getHistory[4])
})

suiteCase.run()
23 changes: 20 additions & 3 deletions packages/bot/core/core.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ class CoreClass {
return
}

const gotoFlow =
(flag) =>
async (flowInstance, step = 0) => {
flag.gotoFlow = true
const flowTree = flowInstance.toJson()
const flowParentId = flowTree[step]
const parseListMsg = await this.flowClass.find(flowParentId?.ref, true, flowTree)
if (endFlowFlag) return
for (const msg of parseListMsg) {
await this.sendProviderAndSave(from, msg)
}
await endFlow(flag)()
return
}

// 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes
// para evitar bloque de whatsapp

Expand Down Expand Up @@ -203,7 +218,7 @@ class CoreClass {
endFlow: false,
fallBack: false,
flowDynamic: false,
wait: true,
gotoFlow: false,
}

const provider = this.providerClass
Expand All @@ -215,11 +230,13 @@ class CoreClass {
fallBack: fallBack(flags),
flowDynamic: flowDynamic(flags),
endFlow: endFlow(flags),
gotoFlow: gotoFlow(flags),
}

await this.flowClass.allCallbacks[inRef](messageCtxInComming, argsCb)
const wait = !(!flags.endFlow && !flags.fallBack && !flags.flowDynamic)
if (!wait) await continueFlow()
//Si no hay llamado de fallaback y no hay llamado de flowDynamic y no hay llamado de enflow EL flujo continua
const ifContinue = !flags.endFlow && !flags.fallBack && !flags.flowDynamic
if (ifContinue) await continueFlow()

return
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bot-whatsapp/bot",
"version": "0.0.105-alpha.0",
"version": "0.0.107-alpha.0",
"description": "",
"main": "./lib/bundle.bot.cjs",
"scripts": {
Expand Down
16 changes: 16 additions & 0 deletions packages/bot/tests/flow.class.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,20 @@ test(`[FlowClass] Probando findBySerialize`, async () => {
assert.is(flowClass instanceof FlowClass, true)
})

test(`[FlowClass] Probando getRefToContinueChild `, async () => {
const MOCK_FLOW = addKeyword('hola').addAnswer('Buenas!')
const flowClass = new FlowClass([MOCK_FLOW])

flowClass.getRefToContinueChild('')
assert.is(flowClass instanceof FlowClass, true)
})

test(`[FlowClass] Probando getFlowsChild `, async () => {
const MOCK_FLOW = addKeyword('hola').addAnswer('Buenas!')
const flowClass = new FlowClass([MOCK_FLOW])

flowClass.getFlowsChild('')
assert.is(flowClass instanceof FlowClass, true)
})

test.run()
2 changes: 0 additions & 2 deletions packages/provider/src/baileys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ class BaileysProvider extends ProviderClass {
}
}

console.log(messageCtx.message)

//Detectar media
if (messageCtx.message?.imageMessage) {
payload = { ...payload, body: generateRefprovider('_event_media_') }
Expand Down