From 368bf29e6331b9aae989b66d7ad873ff8525d726 Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Tue, 22 Nov 2022 21:45:00 +0100 Subject: [PATCH] almost work it --- packages/bot/core/core.class.js | 38 +++++++++++++++++--------- packages/bot/io/flow.class.js | 11 +++----- packages/bot/io/methods/addAnswer.js | 28 +++++++++++++++---- packages/bot/io/methods/toSerialize.js | 2 +- 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/packages/bot/core/core.class.js b/packages/bot/core/core.class.js index 224c3c7..3d497dc 100644 --- a/packages/bot/core/core.class.js +++ b/packages/bot/core/core.class.js @@ -50,7 +50,13 @@ class CoreClass { handleMsg = async (messageInComming) => { const { body, from } = messageInComming let msgToSend = [] + + //Consultamos mensaje previo en DB const prevMsg = await this.databaseClass.getPrevByNumber(from) + //Consultamos for refSerializada en el flow actual + const refToContinue = this.flowClass.findBySerialize( + prevMsg?.refSerialize + ) if (prevMsg?.ref) { const ctxByNumber = toCtx({ @@ -61,26 +67,32 @@ class CoreClass { this.databaseClass.save(ctxByNumber) } - if (prevMsg?.refSerialize && prevMsg?.options?.capture) { - const refToContinue = this.flowClass.findBySerialize( - prevMsg.refSerialize - ) + //Si se tiene un callback se ejecuta + if (refToContinue && prevMsg?.options?.callback) { + const indexFlow = this.flowClass.findIndexByRef(refToContinue?.ref) + this.flowClass.allCallbacks[indexFlow].callback(messageInComming) + } - if (refToContinue && prevMsg?.options?.callback) { - const indexFlow = this.flowClass.findIndexByRef( - refToContinue?.ref - ) + //Si se tiene anidaciones de flows, si tienes anidados obligatoriamente capture:true + if (prevMsg?.options?.nested?.length) { + const nestedRef = prevMsg.options.nested + const flowStandalone = nestedRef.map((f) => ({ + ...this.flowClass.findBySerialize(f), + })) - this.flowClass.allCallbacks[indexFlow].callback( - messageInComming - ) - } + msgToSend = this.flowClass.find(body, false, flowStandalone) || [] + this.sendFlow(msgToSend, from) + return + } + //Consultamos si se espera respuesta por parte de cliente "Ejemplo: Dime tu nombre" + if (!prevMsg?.options?.nested?.length && prevMsg?.options?.capture) { msgToSend = this.flowClass.find(refToContinue?.ref, true) || [] } else { msgToSend = this.flowClass.find(body) || [] } - if (Array.isArray(msgToSend)) this.sendFlow(msgToSend, from) + + this.sendFlow(msgToSend, from) } sendProviderAndSave = (numberOrId, ctxMessage) => { diff --git a/packages/bot/io/flow.class.js b/packages/bot/io/flow.class.js index 14cbd28..bb0d79d 100644 --- a/packages/bot/io/flow.class.js +++ b/packages/bot/io/flow.class.js @@ -1,4 +1,4 @@ -const { toSerialize } = require('./methods') +const { toSerialize } = require('./methods/toSerialize') class FlowClass { allCallbacks = [] @@ -20,16 +20,13 @@ class FlowClass { this.flowSerialize = toSerialize(mergeToJsonSerialize) } - find = (keyOrWord, symbol = false) => { + find = (keyOrWord, symbol = false, overFlow = null) => { let capture = false let messages = [] let refSymbol = null + overFlow = overFlow ?? this.flowSerialize - const findIn = ( - keyOrWord, - symbol = false, - flow = this.flowSerialize - ) => { + const findIn = (keyOrWord, symbol = false, flow = overFlow) => { capture = refSymbol?.options?.capture || false if (capture) return messages diff --git a/packages/bot/io/methods/addAnswer.js b/packages/bot/io/methods/addAnswer.js index 8947015..2300ff4 100644 --- a/packages/bot/io/methods/addAnswer.js +++ b/packages/bot/io/methods/addAnswer.js @@ -1,5 +1,6 @@ const { generateRef } = require('../../utils/hash') const { toJson } = require('./toJson') +const { toSerialize } = require('./toSerialize') /** * * @param answer string @@ -8,7 +9,11 @@ const { toJson } = require('./toJson') */ const addAnswer = (inCtx) => - (answer, options, cb = null) => { + (answer, options, cb = null, nested = []) => { + /** + * Todas las opciones referentes a el mensaje en concreto options:{} + * @returns + */ const getAnswerOptions = () => ({ media: typeof options?.media === 'string' ? `${options?.media}` : null, @@ -21,17 +26,28 @@ const addAnswer = typeof options?.child === 'string' ? `${options?.child}` : null, }) + const getNested = () => ({ + nested: Array.isArray(nested) ? nested : [], + }) + + const callback = + typeof cb === 'function' + ? cb + : () => console.log('Callback no definida') + const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx + + /** + * Esta funcion se encarga de mapear y transformar todo antes + * de retornar + * @returns + */ const ctxAnswer = () => { const ref = `ans_${generateRef()}` - const callback = - typeof cb === 'function' - ? cb - : () => console.log('Callback no definida') - const options = { ...getAnswerOptions(), + ...getNested(), keyword: {}, callback: !!cb, } diff --git a/packages/bot/io/methods/toSerialize.js b/packages/bot/io/methods/toSerialize.js index 108cf3e..940604e 100644 --- a/packages/bot/io/methods/toSerialize.js +++ b/packages/bot/io/methods/toSerialize.js @@ -3,7 +3,7 @@ const { generateRefSerialize } = require('../../utils/hash') /** * Crear referencia serializada * @param {*} flowJson - * @returns + * @returns array[] */ const toSerialize = (flowJson) => { if (!Array.isArray(flowJson)) throw new Error('Esto debe ser un ARRAY')