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

fix: ⚡ delay after gotoFlow #877 #922

Merged
merged 1 commit into from
Nov 17, 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
28 changes: 28 additions & 0 deletions __test__/0.0.2-goto-flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,32 @@ testSuite(`Debe de continuar el el encadenamiento con procesos async`, async ({
assert.is(undefined, history[96])
})

//Issue https://github.com/codigoencasa/bot-whatsapp/issues/877
testSuite(`Debe respectar el delay del node previo`, async ({ database, provider }) => {
const flowPing = addKeyword(['hi']).addAction(async (_, { flowDynamic, gotoFlow }) => {
await flowDynamic('Buenas ping debe espera 1segundo')
return gotoFlow(flowBye)
})

const flowBye = addKeyword('ping').addAnswer(`Pong con delay 1 segundo`, { delay: 1000 })

await createBot({
database,
flow: createFlow([flowPing, flowBye]),
provider,
})

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

await delay(2000)
const history = database.listHistory.map((item) => item.answer)
assert.is('__call_action__', history[0])
assert.is('Buenas ping debe espera 1segundo', history[1])
assert.is('Pong con delay 1 segundo', history[2])
assert.is(undefined, history[3])
})

testSuite.run()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"build": "pnpm run cli:rollup && pnpm run bot:rollup && pnpm run provider:rollup && pnpm run database:rollup && pnpm run contexts:rollup && pnpm run create-bot-whatsapp:rollup && pnpm run portal:rollup && pnpm run eslint-plugin:rollup",
"copy.lib": "node ./scripts/move.js",
"test.unit": "node ./node_modules/uvu/bin.js packages test",
"test.e2e": "node ./node_modules/uvu/bin.js __test__",
"test.e2e": "node ./node_modules/uvu/bin.js __test__ ",
"test.coverage": "node ./node_modules/c8/bin/c8.js npm run test.unit",
"test": "npm run test.coverage",
"cli": "node ./packages/cli/bin/cli.js",
Expand Down
4 changes: 4 additions & 0 deletions packages/bot/core/core.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ class CoreClass extends EventEmitter {
async (flowInstance, step = 0) => {
const promises = []
flag.gotoFlow = true

if (!flowInstance?.toJson) {
printer([
`[POSSIBLE_CIRCULAR_DEPENDENCY]: Se ha detectado una dependencia circular.`,
Expand All @@ -302,6 +303,8 @@ class CoreClass extends EventEmitter {
return
}

await delay(flowInstance?.ctx?.options?.delay ?? 0)

const flowTree = flowInstance.toJson()

const flowParentId = flowTree[step]
Expand All @@ -316,6 +319,7 @@ class CoreClass extends EventEmitter {
// Enviar el mensaje al proveedor y guardarlo
await this.sendProviderAndSave(from, ctxMessage).then(() => promises.push(ctxMessage))
}

await endFlow(flag)(promises)

return
Expand Down