diff --git a/packages/bot/core/core.class.js b/packages/bot/core/core.class.js index e1e16e4..3f20b7b 100644 --- a/packages/bot/core/core.class.js +++ b/packages/bot/core/core.class.js @@ -117,6 +117,7 @@ class CoreClass { // 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes // para evitar bloque de whatsapp + const flowDynamic = async ( listMsg = [], optListMsg = { limit: 5, fallback: false } @@ -126,15 +127,21 @@ class CoreClass { fallBackFlag = optListMsg.fallback const parseListMsg = listMsg - .map(({ body }, index) => - toCtx({ + .map((opt, index) => { + const body = typeof opt === 'string' ? opt : opt.body + const media = opt?.media ?? null + const buttons = opt?.buttons ?? [] + + return toCtx({ body, from, keyword: null, index, + options: { media, buttons }, }) - ) + }) .slice(0, optListMsg.limit) + for (const msg of parseListMsg) { await this.sendProviderAndSave(from, msg) } @@ -212,5 +219,24 @@ class CoreClass { this.continue(null, responde.ref) } } + + /** + * Funcion dedicada a enviar el mensaje sin pasar por el flow + * (dialogflow) + * @param {*} messageToSend + * @param {*} numberOrId + * @returns + */ + sendFlowSimple = async (messageToSend, numberOrId) => { + const queue = [] + for (const ctxMessage of messageToSend) { + const delayMs = ctxMessage?.options?.delay || 0 + if (delayMs) await delay(delayMs) + QueuePrincipal.enqueue(() => + this.sendProviderAndSave(numberOrId, ctxMessage) + ) + } + return Promise.all(queue) + } } module.exports = CoreClass diff --git a/packages/bot/io/methods/toCtx.js b/packages/bot/io/methods/toCtx.js index d29295e..980cbfb 100644 --- a/packages/bot/io/methods/toCtx.js +++ b/packages/bot/io/methods/toCtx.js @@ -5,12 +5,12 @@ const { generateRef, generateRefSerialize } = require('../../utils/hash') * @param options {media:string, buttons:[], capture:true default false} * @returns */ -const toCtx = ({ body, from, prevRef, index }) => { +const toCtx = ({ body, from, prevRef, options = {}, index }) => { return { ref: generateRef(), keyword: prevRef, answer: body, - options: {}, + options: options ?? {}, from, refSerialize: generateRefSerialize({ index, answer: body }), } diff --git a/packages/bot/tests/flow.class.test.js b/packages/bot/tests/flow.class.test.js new file mode 100644 index 0000000..b9c55e2 --- /dev/null +++ b/packages/bot/tests/flow.class.test.js @@ -0,0 +1,29 @@ +const { test } = require('uvu') +const assert = require('uvu/assert') +const FlowClass = require('../io/flow.class') +const MockProvider = require('../../../__mocks__/mock.provider') +const { addKeyword } = require('../index') + +test(`[FlowClass] Probando instanciamiento de clase`, async () => { + const MOCK_FLOW = addKeyword('hola').addAnswer('Buenas!') + const flowClass = new FlowClass([MOCK_FLOW]) + assert.is(flowClass instanceof FlowClass, true) +}) + +test(`[FlowClass] Probando find`, async () => { + const MOCK_FLOW = addKeyword('hola').addAnswer('Buenas!') + const flowClass = new FlowClass([MOCK_FLOW]) + + flowClass.find('hola') + assert.is(flowClass instanceof FlowClass, true) +}) + +test(`[FlowClass] Probando findBySerialize`, async () => { + const MOCK_FLOW = addKeyword('hola').addAnswer('Buenas!') + const flowClass = new FlowClass([MOCK_FLOW]) + + flowClass.findBySerialize('') + assert.is(flowClass instanceof FlowClass, true) +}) + +test.run() diff --git a/packages/contexts/src/dialogflow-cx/dialogflow-cx.class.js b/packages/contexts/src/dialogflow-cx/dialogflow-cx.class.js index 25bd2a6..2d0ebd2 100644 --- a/packages/contexts/src/dialogflow-cx/dialogflow-cx.class.js +++ b/packages/contexts/src/dialogflow-cx/dialogflow-cx.class.js @@ -117,7 +117,7 @@ class DialogFlowCXContext extends CoreClass { } }) - this.sendFlow(listMessages, from) + this.sendFlowSimple(listMessages, from) } } diff --git a/packages/contexts/src/dialogflow/dialogflow.class.js b/packages/contexts/src/dialogflow/dialogflow.class.js index ebd92bc..6a7cda0 100644 --- a/packages/contexts/src/dialogflow/dialogflow.class.js +++ b/packages/contexts/src/dialogflow/dialogflow.class.js @@ -107,7 +107,7 @@ class DialogFlowContext extends CoreClass { ...customPayload, answer: fields?.answer?.stringValue, } - this.sendFlow([ctxFromDX], from) + this.sendFlowSimple([ctxFromDX], from) return } @@ -115,7 +115,7 @@ class DialogFlowContext extends CoreClass { answer: queryResult?.fulfillmentText, } - this.sendFlow([ctxFromDX], from) + this.sendFlowSimple([ctxFromDX], from) } }