mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-20 20:49:15 +00:00
i need remove ctx stranger
This commit is contained in:
6
TODO.md
6
TODO.md
@@ -1 +1,5 @@
|
|||||||
- [ ] Cuando Envian Sticket devuelve mensaje raro
|
- [ ] Cuando Envian Sticket devuelve mensaje raro
|
||||||
|
- [ ] Posibilidad de en el capture meter todo un nuevo CTX de FLOW .addAnswer('Marca la opcion',{capture:true, join:CTX})
|
||||||
|
- [ ] .addKeyword('1') no funciona con 1 caracter
|
||||||
|
- [ ] sensitivy viene activado por defecto
|
||||||
|
- [ ] colocar mensaje esperando conectando whatsapp (provider)
|
||||||
@@ -47,7 +47,8 @@ class CoreClass {
|
|||||||
* @private
|
* @private
|
||||||
* @param {*} ctxMessage
|
* @param {*} ctxMessage
|
||||||
*/
|
*/
|
||||||
handleMsg = async ({ body, from }) => {
|
handleMsg = async (messageInComming) => {
|
||||||
|
const { body, from } = messageInComming
|
||||||
let msgToSend = []
|
let msgToSend = []
|
||||||
const prevMsg = await this.databaseClass.getPrevByNumber(from)
|
const prevMsg = await this.databaseClass.getPrevByNumber(from)
|
||||||
|
|
||||||
@@ -64,6 +65,17 @@ class CoreClass {
|
|||||||
const refToContinue = this.flowClass.findBySerialize(
|
const refToContinue = this.flowClass.findBySerialize(
|
||||||
prevMsg.refSerialize
|
prevMsg.refSerialize
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (refToContinue && prevMsg?.options?.callback) {
|
||||||
|
const indexFlow = this.flowClass.findIndexByRef(
|
||||||
|
refToContinue?.ref
|
||||||
|
)
|
||||||
|
|
||||||
|
this.flowClass.allCallbacks[indexFlow].callback(
|
||||||
|
messageInComming
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
msgToSend = this.flowClass.find(refToContinue?.ref, true) || []
|
msgToSend = this.flowClass.find(refToContinue?.ref, true) || []
|
||||||
} else {
|
} else {
|
||||||
msgToSend = this.flowClass.find(body) || []
|
msgToSend = this.flowClass.find(body) || []
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const CoreClass = require('./core/core.class')
|
const CoreClass = require('./core/core.class')
|
||||||
const ProviderClass = require('./provider/provider.class')
|
const ProviderClass = require('./provider/provider.class')
|
||||||
const FlowClass = require('./io/flow.class')
|
const FlowClass = require('./io/flow.class')
|
||||||
const { addKeyword, addAnswer } = require('./io/methods')
|
const { addKeyword, addAnswer, toSerialize } = require('./io/methods')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Crear instancia de clase Bot
|
* Crear instancia de clase Bot
|
||||||
@@ -38,6 +38,7 @@ module.exports = {
|
|||||||
createProvider,
|
createProvider,
|
||||||
addKeyword,
|
addKeyword,
|
||||||
addAnswer,
|
addAnswer,
|
||||||
|
toSerialize,
|
||||||
ProviderClass,
|
ProviderClass,
|
||||||
CoreClass,
|
CoreClass,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,55 @@
|
|||||||
const { toSerialize } = require('./methods')
|
const { toSerialize } = require('./methods')
|
||||||
|
|
||||||
class FlowClass {
|
class FlowClass {
|
||||||
flow
|
allContexts = []
|
||||||
|
allCallbacks = []
|
||||||
|
flowSerialize = []
|
||||||
|
flowRaw = []
|
||||||
constructor(_flow) {
|
constructor(_flow) {
|
||||||
this.flow = toSerialize(_flow)
|
if (!Array.isArray(_flow)) throw new Error('Esto debe ser un ARRAY')
|
||||||
|
_flow.forEach((ctxFlow, parentIndex) => {
|
||||||
|
const callbacks = ctxFlow.ctx?.callbacks || []
|
||||||
|
const contexts = ctxFlow.ctx?.contexts || []
|
||||||
|
|
||||||
|
callbacks.forEach((deepCallbacks) => {
|
||||||
|
if (deepCallbacks && contexts[parentIndex]) {
|
||||||
|
const ctxChild = contexts[parentIndex]
|
||||||
|
deepCallbacks.callback(null, ctxChild)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
this.flowRaw = _flow
|
||||||
|
this.allContexts = _flow
|
||||||
|
.map((ctxs) => ctxs.ctx.contexts)
|
||||||
|
.flat(2)
|
||||||
|
.map((c, i) => ({ getCtx: c?.getCtx, index: i }))
|
||||||
|
|
||||||
|
this.allCallbacks = _flow
|
||||||
|
.map((cbIn) => cbIn.ctx.callbacks)
|
||||||
|
.flat(2)
|
||||||
|
.map((c, i) => ({ callback: c?.callback, index: i }))
|
||||||
|
|
||||||
|
const mergeToJsonSerialize = Object.keys(_flow)
|
||||||
|
.map((indexObjectFlow) => _flow[indexObjectFlow].toJson())
|
||||||
|
.flat(2)
|
||||||
|
|
||||||
|
this.flowSerialize = toSerialize(mergeToJsonSerialize)
|
||||||
}
|
}
|
||||||
|
|
||||||
find = (keyOrWord, symbol = false) => {
|
find = (keyOrWord, symbol = false) => {
|
||||||
let capture = false
|
let capture = false
|
||||||
let messages = []
|
let messages = []
|
||||||
let refSymbol
|
let refSymbol = null
|
||||||
|
|
||||||
const findIn = (keyOrWord, symbol = false, flow = this.flow) => {
|
const findIn = (
|
||||||
|
keyOrWord,
|
||||||
|
symbol = false,
|
||||||
|
flow = this.flowSerialize
|
||||||
|
) => {
|
||||||
capture = refSymbol?.options?.capture || false
|
capture = refSymbol?.options?.capture || false
|
||||||
if (capture) return messages
|
if (capture) return messages
|
||||||
|
|
||||||
if (symbol) {
|
if (symbol) {
|
||||||
refSymbol = flow.find((c) => c.keyword === keyOrWord)
|
refSymbol = flow.find((c) => c.keyword === keyOrWord)
|
||||||
if (refSymbol?.answer) messages.push(refSymbol)
|
if (refSymbol?.answer) messages.push(refSymbol)
|
||||||
@@ -29,7 +65,9 @@ class FlowClass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
findBySerialize = (refSerialize) =>
|
findBySerialize = (refSerialize) =>
|
||||||
this.flow.find((r) => r.refSerialize === refSerialize)
|
this.flowSerialize.find((r) => r.refSerialize === refSerialize)
|
||||||
|
|
||||||
|
findIndexByRef = (ref) => this.flowSerialize.findIndex((r) => r.ref === ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = FlowClass
|
module.exports = FlowClass
|
||||||
|
|||||||
@@ -6,43 +6,78 @@ const { toJson } = require('./toJson')
|
|||||||
* @param options {media:string, buttons:[], capture:true default false}
|
* @param options {media:string, buttons:[], capture:true default false}
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
const addAnswer = (inCtx) => (answer, options) => {
|
const addAnswer =
|
||||||
const getAnswerOptions = () => ({
|
(inCtx) =>
|
||||||
media: typeof options?.media === 'string' ? `${options?.media}` : null,
|
(answer, options, cb = null) => {
|
||||||
buttons: Array.isArray(options?.buttons) ? options.buttons : [],
|
const getAnswerOptions = () => ({
|
||||||
capture:
|
media:
|
||||||
typeof options?.capture === 'boolean' ? options?.capture : false,
|
typeof options?.media === 'string' ? `${options?.media}` : null,
|
||||||
})
|
buttons: Array.isArray(options?.buttons) ? options.buttons : [],
|
||||||
|
capture:
|
||||||
|
typeof options?.capture === 'boolean'
|
||||||
|
? options?.capture
|
||||||
|
: false,
|
||||||
|
child:
|
||||||
|
typeof options?.child === 'string' ? `${options?.child}` : null,
|
||||||
|
})
|
||||||
|
|
||||||
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx
|
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx
|
||||||
const ctxAnswer = () => {
|
const ctxAnswer = () => {
|
||||||
const ref = `ans_${generateRef()}`
|
const ref = `ans_${generateRef()}`
|
||||||
|
|
||||||
const options = {
|
const callback =
|
||||||
...getAnswerOptions(),
|
typeof cb === 'function'
|
||||||
keyword: {},
|
? cb
|
||||||
|
: () => console.log('Callback no definida')
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
...getAnswerOptions(),
|
||||||
|
keyword: {},
|
||||||
|
callback: !!cb,
|
||||||
|
}
|
||||||
|
|
||||||
|
const json = [].concat(inCtx.json).concat([
|
||||||
|
{
|
||||||
|
ref,
|
||||||
|
keyword: lastCtx.ref,
|
||||||
|
answer,
|
||||||
|
options,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
const callbacks = [].concat(inCtx.callbacks).concat([
|
||||||
|
{
|
||||||
|
ref: lastCtx.ref,
|
||||||
|
callback,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
const contexts = [].concat(inCtx.contexts).concat([
|
||||||
|
{
|
||||||
|
ref: lastCtx.ref,
|
||||||
|
getCtx: () => lastCtx,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
return {
|
||||||
|
...lastCtx,
|
||||||
|
ref,
|
||||||
|
answer,
|
||||||
|
json,
|
||||||
|
options,
|
||||||
|
callbacks,
|
||||||
|
contexts,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const json = [].concat(inCtx.json).concat([
|
const ctx = ctxAnswer()
|
||||||
{
|
|
||||||
ref,
|
|
||||||
keyword: lastCtx.ref,
|
|
||||||
answer,
|
|
||||||
options,
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
return { ...lastCtx, ref, answer, json, options }
|
return {
|
||||||
|
ctx,
|
||||||
|
ref: ctx.ref,
|
||||||
|
addAnswer: addAnswer(ctx),
|
||||||
|
toJson: toJson(ctx),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const ctx = ctxAnswer()
|
|
||||||
|
|
||||||
return {
|
|
||||||
ctx,
|
|
||||||
ref: ctx.ref,
|
|
||||||
addAnswer: addAnswer(ctx),
|
|
||||||
toJson: toJson(ctx),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { addAnswer }
|
module.exports = { addAnswer }
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
const { generateRef } = require('../../utils/hash')
|
const { generateRef } = require('../../utils/hash')
|
||||||
const { addAnswer } = require('./addAnswer')
|
const { addAnswer } = require('./addAnswer')
|
||||||
const { toJson } = require('./toJson')
|
const { toJson } = require('./toJson')
|
||||||
/**
|
|
||||||
* addKeyword:
|
|
||||||
* Es necesario que genere id|hash
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -12,11 +8,6 @@ const { toJson } = require('./toJson')
|
|||||||
* @param {*} options {sensitive:boolean} default false
|
* @param {*} options {sensitive:boolean} default false
|
||||||
*/
|
*/
|
||||||
const addKeyword = (keyword, options) => {
|
const addKeyword = (keyword, options) => {
|
||||||
/**
|
|
||||||
* Esta funcion deberia parsear y validar las opciones
|
|
||||||
* del keyword
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
const parseOptions = () => {
|
const parseOptions = () => {
|
||||||
const defaultProperties = {
|
const defaultProperties = {
|
||||||
sensitive:
|
sensitive:
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ test('Debere probar toSerialize', () => {
|
|||||||
.addAnswer('Segundo!')
|
.addAnswer('Segundo!')
|
||||||
.toJson()
|
.toJson()
|
||||||
|
|
||||||
|
console.log(MAIN_CTX)
|
||||||
|
|
||||||
const [ANSWER_A] = MAIN_CTX
|
const [ANSWER_A] = MAIN_CTX
|
||||||
|
|
||||||
assert.is(
|
assert.is(
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
const crypto = require('crypto')
|
const crypto = require('crypto')
|
||||||
|
|
||||||
const generateRef = () => {
|
/**
|
||||||
return crypto.randomUUID()
|
* Generamos un UUID unico con posibilidad de tener un prefijo
|
||||||
|
* @param {*} prefix
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const generateRef = (prefix = false) => {
|
||||||
|
const id = crypto.randomUUID()
|
||||||
|
return prefix ? `${prefix}_${id}` : id
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user