Skip to content

Commit

Permalink
feat(bot): ✨ new goto event
Browse files Browse the repository at this point in the history
feat(bot): ✨ new goto event
  • Loading branch information
leifermendez authored Feb 21, 2023
2 parents 0cf3d76 + 58d43a7 commit 8d7a948
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
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

0 comments on commit 8d7a948

Please sign in to comment.