-
-
Notifications
You must be signed in to change notification settings - Fork 750
/
addKeyword.js
55 lines (47 loc) 路 1.46 KB
/
addKeyword.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const { generateRef } = require('../../utils/hash')
const { addAnswer } = require('./addAnswer')
const { toJson } = require('./toJson')
/**
*
* @param {*} message `string | string[]`
* @param {*} options {sensitive:boolean} default false
*/
const addKeyword = (keyword, options) => {
if (typeof keyword !== 'string' && !Array.isArray(keyword)) {
throw new Error('DEBE_SER_STRING_ARRAY_REGEX')
}
const parseOptions = () => {
const defaultProperties = {
sensitive: typeof options?.sensitive === 'boolean' ? options?.sensitive : false,
regex: typeof options?.regex === 'boolean' ? options?.regex : 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),
addAction: (cb = () => null, flagCb = () => null) => {
if (typeof cb === 'object') return addAnswer(ctx)('__capture_only_intended__', cb, flagCb)
return addAnswer(ctx)('__call_action__', null, cb)
},
toJson: toJson(ctx),
}
}
module.exports = { addKeyword }