Merge pull request #536 from codigoencasa/fix/flowdynamic-buttons

Fix/flowdynamic buttons
This commit is contained in:
Leifer Mendez
2023-01-23 11:54:13 +01:00
committed by GitHub
5 changed files with 63 additions and 8 deletions

View File

@@ -117,6 +117,7 @@ class CoreClass {
// 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes // 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes
// para evitar bloque de whatsapp // para evitar bloque de whatsapp
const flowDynamic = async ( const flowDynamic = async (
listMsg = [], listMsg = [],
optListMsg = { limit: 5, fallback: false } optListMsg = { limit: 5, fallback: false }
@@ -126,15 +127,21 @@ class CoreClass {
fallBackFlag = optListMsg.fallback fallBackFlag = optListMsg.fallback
const parseListMsg = listMsg const parseListMsg = listMsg
.map(({ body }, index) => .map((opt, index) => {
toCtx({ const body = typeof opt === 'string' ? opt : opt.body
const media = opt?.media ?? null
const buttons = opt?.buttons ?? []
return toCtx({
body, body,
from, from,
keyword: null, keyword: null,
index, index,
options: { media, buttons },
}) })
) })
.slice(0, optListMsg.limit) .slice(0, optListMsg.limit)
for (const msg of parseListMsg) { for (const msg of parseListMsg) {
await this.sendProviderAndSave(from, msg) await this.sendProviderAndSave(from, msg)
} }
@@ -212,5 +219,24 @@ class CoreClass {
this.continue(null, responde.ref) 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 module.exports = CoreClass

View File

@@ -5,12 +5,12 @@ const { generateRef, generateRefSerialize } = require('../../utils/hash')
* @param options {media:string, buttons:[], capture:true default false} * @param options {media:string, buttons:[], capture:true default false}
* @returns * @returns
*/ */
const toCtx = ({ body, from, prevRef, index }) => { const toCtx = ({ body, from, prevRef, options = {}, index }) => {
return { return {
ref: generateRef(), ref: generateRef(),
keyword: prevRef, keyword: prevRef,
answer: body, answer: body,
options: {}, options: options ?? {},
from, from,
refSerialize: generateRefSerialize({ index, answer: body }), refSerialize: generateRefSerialize({ index, answer: body }),
} }

View File

@@ -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()

View File

@@ -117,7 +117,7 @@ class DialogFlowCXContext extends CoreClass {
} }
}) })
this.sendFlow(listMessages, from) this.sendFlowSimple(listMessages, from)
} }
} }

View File

@@ -107,7 +107,7 @@ class DialogFlowContext extends CoreClass {
...customPayload, ...customPayload,
answer: fields?.answer?.stringValue, answer: fields?.answer?.stringValue,
} }
this.sendFlow([ctxFromDX], from) this.sendFlowSimple([ctxFromDX], from)
return return
} }
@@ -115,7 +115,7 @@ class DialogFlowContext extends CoreClass {
answer: queryResult?.fulfillmentText, answer: queryResult?.fulfillmentText,
} }
this.sendFlow([ctxFromDX], from) this.sendFlowSimple([ctxFromDX], from)
} }
} }