mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-17 19:26:23 +00:00
add class
This commit is contained in:
143
__tests__/basic.test.js
Normal file
143
__tests__/basic.test.js
Normal file
@@ -0,0 +1,143 @@
|
||||
const { test } = require('uvu')
|
||||
const assert = require('uvu/assert')
|
||||
const { EventEmitter } = require('node:events')
|
||||
const { addKeyword } = require('../packages/io')
|
||||
const database = require('mime-db')
|
||||
|
||||
const flow = addKeyword('hola')
|
||||
.addAnswer('bienvenido')
|
||||
.addAnswer('chao')
|
||||
.toJson()
|
||||
|
||||
const provider = {
|
||||
sendMessage: (ctx) => {
|
||||
console.log('Enviando...', ctx)
|
||||
},
|
||||
}
|
||||
//// DataBaseMock ---------------------------------------------
|
||||
class DataBaseMock {
|
||||
flow
|
||||
provider
|
||||
constructor(_flow, _provider) {
|
||||
this.flow = _flow
|
||||
this.provider = _provider
|
||||
}
|
||||
|
||||
continue = (message, ref = false) => {
|
||||
let keyRef = ref
|
||||
let ansRef = null
|
||||
if (!keyRef) {
|
||||
keyRef = this.flow.find((n) => n.keyword.includes(message)).ref
|
||||
}
|
||||
ansRef = this.flow.find((n) => n.keyword === keyRef)
|
||||
|
||||
if (ansRef) {
|
||||
this.provider.sendMessage(ansRef.answer)
|
||||
this.continue(null, ansRef.ref)
|
||||
}
|
||||
}
|
||||
}
|
||||
//// ProviderMock ---------------------------------------------
|
||||
class ProviderMock {
|
||||
constructor() {
|
||||
//twilio ...
|
||||
}
|
||||
|
||||
sendMessage = (ctx) => {
|
||||
console.log('Enviando...', ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// const bot = {
|
||||
// start: ({ flow, database, provider }) => {
|
||||
// // console.log(database instanceof DataBaseMock)
|
||||
// const flowCtx = database
|
||||
// const botEmitter = new MyEmitter()
|
||||
|
||||
// botEmitter.on('message', (ctx) => flowCtx.continue(ctx.body))
|
||||
// return botEmitter
|
||||
// },
|
||||
// }
|
||||
|
||||
//// BotMock ---------------------------------------------
|
||||
|
||||
// test(`[Flow Basico]: Saludar y Responder`, () => {
|
||||
// let messages = []
|
||||
|
||||
// const botBasic = new BotMock(
|
||||
// flow,
|
||||
// new DataBaseMock(flow, provider),
|
||||
// provider
|
||||
// )
|
||||
|
||||
// botBasic.on('message', (ctx) => messages.push(ctx.body))
|
||||
|
||||
// // Esta linea emula el llegar un mensaje!
|
||||
// botBasic.emit('message', { body: 'hola' })
|
||||
|
||||
// assert.is(messages.join(','), 'hola')
|
||||
// })
|
||||
|
||||
// test.run()
|
||||
|
||||
/***
|
||||
* NEW
|
||||
*/
|
||||
|
||||
class BotClass extends EventEmitter {
|
||||
/**
|
||||
* Emitter para tener on and emit
|
||||
*/
|
||||
|
||||
flowClass
|
||||
databaseClass
|
||||
providerClass
|
||||
constructor(_flow, _database, _provider) {
|
||||
super()
|
||||
this.flowClass = _flow
|
||||
this.databaseClass = _database
|
||||
this.providerClass = _provider
|
||||
}
|
||||
|
||||
continue = () => {
|
||||
const r = this.flowClass.find()
|
||||
if (r) {
|
||||
this.provider.sendMessage(r.answer)
|
||||
this.continue(null, r.ref)
|
||||
console.log(r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FlowClass {
|
||||
flow
|
||||
constructor(_flow) {
|
||||
this.flow = _flow
|
||||
}
|
||||
|
||||
find = (message, ref = false) => {
|
||||
let keyRef = ref
|
||||
let ansRef = null
|
||||
if (!keyRef) {
|
||||
keyRef = this.flow.find((n) => n.keyword.includes(message)).ref
|
||||
}
|
||||
ansRef = this.flow.find((n) => n.keyword === keyRef)
|
||||
if (ansRef) return ansRef
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
test(`[Flow Basico]: Saludar y Responder`, () => {
|
||||
let messages = []
|
||||
|
||||
const botBasic = new BotClass(new FlowClass(flow), null, null)
|
||||
|
||||
botBasic.on('message', (ctx) => messages.push(ctx.body))
|
||||
|
||||
// Esta linea emula el llegar un mensaje!
|
||||
botBasic.emit('message', { body: 'hola' })
|
||||
|
||||
assert.is(messages.join(','), 'hola')
|
||||
})
|
||||
|
||||
test.run()
|
||||
@@ -14,17 +14,48 @@ const { generateRef: generateRef$2 } = hash;
|
||||
|
||||
var utils = { generateRef: generateRef$2 };
|
||||
|
||||
const { generateRef: generateRef$1 } = utils;
|
||||
const toJson$3 = (inCtx) => () => {
|
||||
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx;
|
||||
return lastCtx.json
|
||||
};
|
||||
|
||||
var toJson_1 = { toJson: toJson$3 };
|
||||
|
||||
const { generateRef: generateRef$1 } = utils;
|
||||
const { toJson: toJson$2 } = toJson_1;
|
||||
/**
|
||||
*
|
||||
* @param answer string
|
||||
* @param options {media:string, buttons:[], capture:true default false}
|
||||
* @returns
|
||||
*/
|
||||
const addAnswer$3 = (inCtx) => (answer, options) => {
|
||||
const getAnswerOptions = () => ({
|
||||
media: typeof options?.media === 'string' ? `${options?.media}` : null,
|
||||
buttons: Array.isArray(options?.buttons) ? options.buttons : [],
|
||||
capture:
|
||||
typeof options?.capture === 'boolean' ? options?.capture : false,
|
||||
});
|
||||
|
||||
const addAnswer$3 = (inCtx) => (message, options) => {
|
||||
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx;
|
||||
const ctxAnswer = () => {
|
||||
const ref = generateRef$1();
|
||||
/**
|
||||
* Se guarda en db
|
||||
*/
|
||||
const ref = `ans_${generateRef$1()}`;
|
||||
|
||||
return { ...lastCtx, ref, message }
|
||||
const options = {
|
||||
...getAnswerOptions(),
|
||||
keyword: {},
|
||||
};
|
||||
|
||||
const json = [].concat(inCtx.json).concat([
|
||||
{
|
||||
ref,
|
||||
keyword: lastCtx.ref,
|
||||
answer,
|
||||
options,
|
||||
},
|
||||
]);
|
||||
|
||||
return { ...lastCtx, ref, answer, json, options }
|
||||
};
|
||||
|
||||
const ctx = ctxAnswer();
|
||||
@@ -33,6 +64,7 @@ const addAnswer$3 = (inCtx) => (message, options) => {
|
||||
ctx,
|
||||
ref: ctx.ref,
|
||||
addAnswer: addAnswer$3(ctx),
|
||||
toJson: toJson$2(ctx),
|
||||
}
|
||||
};
|
||||
|
||||
@@ -40,6 +72,7 @@ var addAnswer_1 = { addAnswer: addAnswer$3 };
|
||||
|
||||
const { generateRef } = utils;
|
||||
const { addAnswer: addAnswer$2 } = addAnswer_1;
|
||||
const { toJson: toJson$1 } = toJson_1;
|
||||
/**
|
||||
* addKeyword:
|
||||
* Es necesario que genere id|hash
|
||||
@@ -48,16 +81,40 @@ const { addAnswer: addAnswer$2 } = addAnswer_1;
|
||||
/**
|
||||
*
|
||||
* @param {*} message `string | string[]`
|
||||
* @param {*} options {sensitivy:boolean} defaulta false
|
||||
* @param {*} options {sensitive:boolean} default false
|
||||
*/
|
||||
const addKeyword$2 = (message, options) => {
|
||||
const addKeyword$2 = (keyword, options) => {
|
||||
/**
|
||||
* Esta funcion deberia parsear y validar las opciones
|
||||
* del keyword
|
||||
* @returns
|
||||
*/
|
||||
const parseOptions = () => {
|
||||
const defaultProperties = {
|
||||
sensitive:
|
||||
typeof options?.sensitive === 'boolean'
|
||||
? options?.sensitive
|
||||
: false,
|
||||
};
|
||||
|
||||
return defaultProperties
|
||||
};
|
||||
|
||||
const ctxAddKeyword = () => {
|
||||
const ref = generateRef();
|
||||
const ref = `key_${generateRef()}`;
|
||||
const options = parseOptions();
|
||||
const json = [
|
||||
{
|
||||
ref,
|
||||
keyword,
|
||||
options,
|
||||
},
|
||||
];
|
||||
/**
|
||||
* Se guarda en db
|
||||
*/
|
||||
|
||||
return { ref, keyword: message }
|
||||
return { ref, keyword, options, json }
|
||||
};
|
||||
|
||||
const ctx = ctxAddKeyword();
|
||||
@@ -66,6 +123,7 @@ const addKeyword$2 = (message, options) => {
|
||||
ctx,
|
||||
ref: ctx.ref,
|
||||
addAnswer: addAnswer$2(ctx),
|
||||
toJson: toJson$1(ctx),
|
||||
}
|
||||
};
|
||||
|
||||
@@ -73,8 +131,9 @@ var addKeyword_1 = { addKeyword: addKeyword$2 };
|
||||
|
||||
const { addAnswer: addAnswer$1 } = addAnswer_1;
|
||||
const { addKeyword: addKeyword$1 } = addKeyword_1;
|
||||
const { toJson } = toJson_1;
|
||||
|
||||
var methods = { addAnswer: addAnswer$1, addKeyword: addKeyword$1 };
|
||||
var methods = { addAnswer: addAnswer$1, addKeyword: addKeyword$1, toJson };
|
||||
|
||||
const { addKeyword, addAnswer } = methods;
|
||||
var io = { addKeyword, addAnswer };
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
"cli:rollup": "rollup ./packages/cli/index.js --config ./packages/cli/rollup-cli.config.js",
|
||||
"io:rollup": "rollup ./packages/io/index.js --config ./packages/io/rollup-cli.config.js",
|
||||
"test.unit": "node ./node_modules/uvu/bin.js packages test",
|
||||
"test.e2e": "node ./node_modules/uvu/bin.js __tests__ test",
|
||||
"test.coverage": "node ./node_modules/c8/bin/c8.js --check-coverage --lines=90 npm run test.unit"
|
||||
},
|
||||
"workspaces": [
|
||||
|
||||
@@ -3,13 +3,15 @@ const { toJson } = require('./toJson')
|
||||
/**
|
||||
*
|
||||
* @param answer string
|
||||
* @param options {media:string, buttons:[]}
|
||||
* @param options {media:string, buttons:[], capture:true default false}
|
||||
* @returns
|
||||
*/
|
||||
const addAnswer = (inCtx) => (answer, options) => {
|
||||
const getAnswerOptions = () => ({
|
||||
media: typeof options?.media === 'string' ? `${options?.media}` : null,
|
||||
buttons: Array.isArray(options?.buttons) ? options.buttons : [],
|
||||
capture:
|
||||
typeof options?.capture === 'boolean' ? options?.capture : false,
|
||||
})
|
||||
|
||||
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx
|
||||
|
||||
Reference in New Issue
Block a user