mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-18 03:29:15 +00:00
restore flow working!
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
const { validateCtx } = require('../io/methods')
|
||||
const { toCtx } = require('../io/methods')
|
||||
const { printer } = require('../utils/interactive')
|
||||
|
||||
/**
|
||||
@@ -51,8 +51,20 @@ class CoreClass {
|
||||
let msgToSend = []
|
||||
const prevMsg = await this.databaseClass.getPrevByNumber(from)
|
||||
|
||||
if (prevMsg?.ref && prevMsg?.options?.capture) {
|
||||
msgToSend = this.flowClass.find(prevMsg.ref, true) || []
|
||||
if (prevMsg?.ref) {
|
||||
const ctxByNumber = toCtx({
|
||||
body,
|
||||
from,
|
||||
prevRef: prevMsg.refSerialize,
|
||||
})
|
||||
this.databaseClass.save(ctxByNumber)
|
||||
}
|
||||
|
||||
if (prevMsg?.refSerialize && prevMsg?.options?.capture) {
|
||||
const refToContinue = this.flowClass.findBySerialize(
|
||||
prevMsg.refSerialize
|
||||
)
|
||||
msgToSend = this.flowClass.find(refToContinue?.ref, true) || []
|
||||
} else {
|
||||
msgToSend = this.flowClass.find(body) || []
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
const { toSerialize } = require('./methods')
|
||||
|
||||
class FlowClass {
|
||||
flow
|
||||
constructor(_flow) {
|
||||
this.flow = _flow
|
||||
this.flow = toSerialize(_flow)
|
||||
}
|
||||
|
||||
find = (keyOrWord, symbol = false) => {
|
||||
@@ -25,6 +27,9 @@ class FlowClass {
|
||||
findIn(keyOrWord, symbol)
|
||||
return messages
|
||||
}
|
||||
|
||||
findBySerialize = (refSerialize) =>
|
||||
this.flow.find((r) => r.refSerialize === refSerialize)
|
||||
}
|
||||
|
||||
module.exports = FlowClass
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const { addAnswer } = require('./addAnswer')
|
||||
const { addKeyword } = require('./addKeyword')
|
||||
const { validateCtx } = require('./validateCtx')
|
||||
const { toSerialize } = require('./toSerialize')
|
||||
const { toCtx } = require('./toCtx')
|
||||
const { toJson } = require('./toJson')
|
||||
|
||||
module.exports = { addAnswer, addKeyword, validateCtx, toJson }
|
||||
module.exports = { addAnswer, addKeyword, toCtx, toJson, toSerialize }
|
||||
|
||||
19
packages/bot/io/methods/toCtx.js
Normal file
19
packages/bot/io/methods/toCtx.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const { generateRef, generateRefSerialize } = require('../../utils/hash')
|
||||
/**
|
||||
* @deprecate
|
||||
* @param answer string
|
||||
* @param options {media:string, buttons:[], capture:true default false}
|
||||
* @returns
|
||||
*/
|
||||
const toCtx = ({ body, from, prevRef, index }) => {
|
||||
return {
|
||||
ref: generateRef(),
|
||||
keyword: prevRef,
|
||||
answer: body,
|
||||
options: {},
|
||||
from,
|
||||
refSerialize: generateRefSerialize({ index, answer: body }),
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { toCtx }
|
||||
22
packages/bot/io/methods/toSerialize.js
Normal file
22
packages/bot/io/methods/toSerialize.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const { generateRefSerialize } = require('../../utils/hash')
|
||||
|
||||
/**
|
||||
* Crear referencia serializada
|
||||
* @param {*} flowJson
|
||||
* @returns
|
||||
*/
|
||||
const toSerialize = (flowJson) => {
|
||||
if (!Array.isArray(flowJson)) throw new Error('Esto debe ser un ARRAY')
|
||||
|
||||
const jsonToSerialize = flowJson.map((row, index) => ({
|
||||
...row,
|
||||
refSerialize: `${generateRefSerialize({
|
||||
index,
|
||||
answer: row.answer,
|
||||
})}`,
|
||||
}))
|
||||
|
||||
return jsonToSerialize
|
||||
}
|
||||
|
||||
module.exports = { toSerialize }
|
||||
@@ -1,18 +0,0 @@
|
||||
const { generateRef } = require('../../utils/hash')
|
||||
/**
|
||||
*
|
||||
* @param answer string
|
||||
* @param options {media:string, buttons:[], capture:true default false}
|
||||
* @returns
|
||||
*/
|
||||
const validateCtx = ({ body, from }) => {
|
||||
return {
|
||||
ref: generateRef(),
|
||||
keyword: null,
|
||||
answer: body,
|
||||
options: {},
|
||||
from,
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { validateCtx }
|
||||
@@ -10,6 +10,7 @@ class MockFlow {
|
||||
class MockDB {
|
||||
listHistory = []
|
||||
save = () => {}
|
||||
getPrevByNumber = () => {}
|
||||
}
|
||||
|
||||
test(`[CoreClass] Probando instanciamiento de clase`, async () => {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const { test } = require('uvu')
|
||||
const assert = require('uvu/assert')
|
||||
const { addKeyword, addAnswer } = require('@bot-whatsapp/bot')
|
||||
const { generateRefSerialize } = require('../utils/hash')
|
||||
const { addKeyword, addAnswer, toSerialize } = require('../io/methods')
|
||||
|
||||
test('Debere probar las propeidades', () => {
|
||||
const ARRANGE = {
|
||||
@@ -21,6 +22,26 @@ test('Debere probar las propeidades array', () => {
|
||||
assert.is(MAIN_CTX.ctx.keyword, ARRANGE.keyword)
|
||||
})
|
||||
|
||||
test('Debere probar toSerialize', () => {
|
||||
const ARRANGE = {
|
||||
keyword: ['hola!', 'ole'],
|
||||
}
|
||||
const MAIN_CTX = addKeyword(ARRANGE.keyword)
|
||||
.addAnswer('Segundo!')
|
||||
.addAnswer('Segundo!')
|
||||
.toJson()
|
||||
|
||||
const [ANSWER_A] = MAIN_CTX
|
||||
|
||||
assert.is(
|
||||
toSerialize(MAIN_CTX)[0].refSerialize,
|
||||
generateRefSerialize({
|
||||
index: 0,
|
||||
answer: ANSWER_A.answer,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
test('Debere probar el paso de contexto', () => {
|
||||
const ARRANGE = {
|
||||
keyword: 'hola!',
|
||||
|
||||
@@ -4,4 +4,15 @@ const generateRef = () => {
|
||||
return crypto.randomUUID()
|
||||
}
|
||||
|
||||
module.exports = { generateRef }
|
||||
/**
|
||||
* Genera un HASH MD5
|
||||
* @param {*} param0
|
||||
* @returns
|
||||
*/
|
||||
const generateRefSerialize = ({ index, answer }) =>
|
||||
crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify({ index, answer }))
|
||||
.digest('hex')
|
||||
|
||||
module.exports = { generateRef, generateRefSerialize }
|
||||
|
||||
Reference in New Issue
Block a user