mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-20 12:39:24 +00:00
refactor(io): added new method addChild
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
const CoreClass = require('./core/core.class')
|
||||
const ProviderClass = require('./provider/provider.class')
|
||||
const FlowClass = require('./io/flow.class')
|
||||
const { addKeyword, addAnswer, toSerialize } = require('./io/methods')
|
||||
const { addKeyword, addAnswer, addChild, toSerialize } = require('./io/methods')
|
||||
|
||||
/**
|
||||
* Crear instancia de clase Bot
|
||||
@@ -38,6 +38,7 @@ module.exports = {
|
||||
createProvider,
|
||||
addKeyword,
|
||||
addAnswer,
|
||||
addChild,
|
||||
toSerialize,
|
||||
ProviderClass,
|
||||
CoreClass,
|
||||
|
||||
@@ -10,6 +10,7 @@ const { toSerialize } = require('./toSerialize')
|
||||
const addAnswer =
|
||||
(inCtx) =>
|
||||
(answer, options, cb = null, nested = []) => {
|
||||
answer = Array.isArray(answer) ? answer.join('\n') : answer
|
||||
/**
|
||||
* Todas las opciones referentes a el mensaje en concreto options:{}
|
||||
* @returns
|
||||
|
||||
15
packages/bot/io/methods/addChild.js
Normal file
15
packages/bot/io/methods/addChild.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const { toSerialize } = require('./toSerialize')
|
||||
/**
|
||||
* @deprecate
|
||||
* @param answer string
|
||||
* @param options {media:string, buttons:[], capture:true default false}
|
||||
* @returns
|
||||
*/
|
||||
const addChild = (flowIn = null) => {
|
||||
if (!flowIn?.toJson) {
|
||||
throw new Error('DEBE SER UN FLOW CON toJSON()')
|
||||
}
|
||||
return toSerialize(flowIn.toJson())
|
||||
}
|
||||
|
||||
module.exports = { addChild }
|
||||
@@ -1,7 +1,8 @@
|
||||
const { addAnswer } = require('./addAnswer')
|
||||
const { addKeyword } = require('./addKeyword')
|
||||
const { addChild } = require('./addChild')
|
||||
const { toSerialize } = require('./toSerialize')
|
||||
const { toCtx } = require('./toCtx')
|
||||
const { toJson } = require('./toJson')
|
||||
|
||||
module.exports = { addAnswer, addKeyword, toCtx, toJson, toSerialize }
|
||||
module.exports = { addAnswer, addKeyword, addChild, toCtx, toJson, toSerialize }
|
||||
|
||||
@@ -22,6 +22,15 @@ test('Debere probar las propeidades array', () => {
|
||||
assert.is(MAIN_CTX.ctx.keyword, ARRANGE.keyword)
|
||||
})
|
||||
|
||||
test('Debere probar las propeidades array en answer', () => {
|
||||
const ARRANGE = {
|
||||
keyword: ['hola!', 'ole'],
|
||||
}
|
||||
const MAIN_CTX = addKeyword(ARRANGE.keyword).addAnswer(['hola', 'chao'])
|
||||
|
||||
assert.is(MAIN_CTX.ctx.keyword, ARRANGE.keyword)
|
||||
})
|
||||
|
||||
test('Debere probar toSerialize', () => {
|
||||
const ARRANGE = {
|
||||
keyword: ['hola!', 'ole'],
|
||||
|
||||
0
packages/cli/bin/cli.js
Normal file → Executable file
0
packages/cli/bin/cli.js
Normal file → Executable file
@@ -25,6 +25,28 @@ const main = async () => {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
#### CTX
|
||||
```json
|
||||
{
|
||||
ref: 'ans_7d9981e5-5019-422c-a19a-565cbb021391',
|
||||
keyword: 'ans_cfdad31b-ff6d-475f-873a-4ed6f8a79a43',
|
||||
answer: 'Esperando respuesta...',
|
||||
options: {
|
||||
media: null,
|
||||
buttons: [],
|
||||
capture: true,
|
||||
child: null,
|
||||
nested: [Array],
|
||||
keyword: {},
|
||||
callback: true
|
||||
},
|
||||
refSerialize: '81f18f563fd26a6c6d12c62aed98095f',
|
||||
from: 'NUMERO_PERSONA_QUE_ESCRIBE'
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
#### Video
|
||||
|
||||
> Video explicando como debes de agregar nuevos adaptadores
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
/**
|
||||
* Si necesitas saber que trae el "ctx"
|
||||
* Puedes ver el README.md dentro packages/database
|
||||
*/
|
||||
|
||||
class MockDatabase {
|
||||
db
|
||||
listHistory = []
|
||||
|
||||
constructor() {
|
||||
/**
|
||||
* Se debe cargar listHistory con historial de mensajes
|
||||
* para que se pueda continuar el flow
|
||||
*/
|
||||
constructor() {}
|
||||
|
||||
getPrevByNumber = (from) => {
|
||||
const history = this.listHistory.slice().reverse()
|
||||
return history.find((a) => a.from === from)
|
||||
}
|
||||
|
||||
save = (ctx) => {
|
||||
console.log('Guardando DB...', ctx)
|
||||
this.listHistory.push(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user