move io into bot

This commit is contained in:
Leifer Mendez
2022-11-13 14:41:25 +01:00
parent 3946c88ed7
commit 03eed5131a
19 changed files with 44 additions and 49 deletions

View File

@@ -0,0 +1,48 @@
const { generateRef } = require('../../utils/hash')
const { toJson } = require('./toJson')
/**
*
* @param answer string
* @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
const ctxAnswer = () => {
const ref = `ans_${generateRef()}`
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()
return {
ctx,
ref: ctx.ref,
addAnswer: addAnswer(ctx),
toJson: toJson(ctx),
}
}
module.exports = { addAnswer }

View File

@@ -0,0 +1,58 @@
const { generateRef } = require('../../utils/hash')
const { addAnswer } = require('./addAnswer')
const { toJson } = require('./toJson')
/**
* addKeyword:
* Es necesario que genere id|hash
*/
/**
*
* @param {*} message `string | string[]`
* @param {*} options {sensitive:boolean} default false
*/
const addKeyword = (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 = `key_${generateRef()}`
const options = parseOptions()
const json = [
{
ref,
keyword,
options,
},
]
/**
* Se guarda en db
*/
return { ref, keyword, options, json }
}
const ctx = ctxAddKeyword()
return {
ctx,
ref: ctx.ref,
addAnswer: addAnswer(ctx),
toJson: toJson(ctx),
}
}
module.exports = { addKeyword }

View File

@@ -0,0 +1,5 @@
const { addAnswer } = require('./addAnswer')
const { addKeyword } = require('./addKeyword')
const { toJson } = require('./toJson')
module.exports = { addAnswer, addKeyword, toJson }

View File

@@ -0,0 +1,6 @@
const toJson = (inCtx) => () => {
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx
return lastCtx.json
}
module.exports = { toJson }