mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-19 03:59:17 +00:00
io improvement
This commit is contained in:
9
package-lock.json
generated
9
package-lock.json
generated
@@ -1161,6 +1161,15 @@
|
|||||||
"integrity": "sha512-/f6gpQuxDaqXu+1kwQYSckUglPaOrHdbIlBAu0YuW8/Cdb45XwXYNUBXg3r/9Mo6n540Kn/smKcZWko5x99KrQ==",
|
"integrity": "sha512-/f6gpQuxDaqXu+1kwQYSckUglPaOrHdbIlBAu0YuW8/Cdb45XwXYNUBXg3r/9Mo6n540Kn/smKcZWko5x99KrQ==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"cross-env": {
|
||||||
|
"version": "7.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz",
|
||||||
|
"integrity": "sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"cross-spawn": "^7.0.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"cross-fetch": {
|
"cross-fetch": {
|
||||||
"version": "3.1.5",
|
"version": "3.1.5",
|
||||||
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.5.tgz",
|
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.5.tgz",
|
||||||
|
|||||||
@@ -54,16 +54,17 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-commonjs": "^23.0.2",
|
"@rollup/plugin-commonjs": "^23.0.2",
|
||||||
|
"cross-env": "^7.0.3",
|
||||||
|
"cross-spawn": "^7.0.3",
|
||||||
|
"detect-package-manager": "^2.0.1",
|
||||||
"eslint": "^8.26.0",
|
"eslint": "^8.26.0",
|
||||||
"eslint-config-prettier": "^8.5.0",
|
"eslint-config-prettier": "^8.5.0",
|
||||||
|
"kleur": "^4.1.5",
|
||||||
"pm2": "^5.2.0",
|
"pm2": "^5.2.0",
|
||||||
"prettier": "^2.7.1",
|
"prettier": "^2.7.1",
|
||||||
"rollup": "^3.2.3",
|
|
||||||
"cross-spawn": "^7.0.3",
|
|
||||||
"kleur": "^4.1.5",
|
|
||||||
"prompts": "^2.4.2",
|
"prompts": "^2.4.2",
|
||||||
"rimraf": "^3.0.2",
|
"rimraf": "^3.0.2",
|
||||||
"detect-package-manager": "^2.0.1"
|
"rollup": "^3.2.3"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "16.x"
|
"node": "16.x"
|
||||||
|
|||||||
@@ -3,9 +3,9 @@
|
|||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"private":true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"cli:dev": "NODE_ENV=dev node ./index.js",
|
"cli:dev": "cross-env NODE_ENV=dev node ./index.js",
|
||||||
"cli:rollup": "rollup index.js --config ./rollup-cli.config.js",
|
"cli:rollup": "rollup index.js --config ./rollup-cli.config.js",
|
||||||
"format:check": "prettier --check .",
|
"format:check": "prettier --check .",
|
||||||
"format:write": "prettier --write .",
|
"format:write": "prettier --write .",
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
# @bot-whatsapp/io
|
# @bot-whatsapp/io
|
||||||
|
|
||||||
|
|
||||||
### Caso de uso
|
### Caso de uso
|
||||||
|
|
||||||
> Una persona escribe `hola`
|
> Una persona escribe `hola`
|
||||||
|
|
||||||
__addKeyword__ recibe `string | string[]`
|
**addKeyword** recibe `string | string[]`
|
||||||
|
|
||||||
> `sensitivy` false _default_
|
> `sensitivy` false _default_
|
||||||
|
|
||||||
- [] addKeyword
|
- [ ] addKeyword
|
||||||
- [] addAnswer
|
- [ ] addAnswer
|
||||||
- [] Si ha contactado previamente. Seguir continuida
|
- [ ] Si ha contactado previamente. Seguir continuida
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// bootstrap.js Como iniciar el provider
|
// bootstrap.js Como iniciar el provider
|
||||||
@@ -41,55 +42,57 @@ const bootstrap = async () => {
|
|||||||
// flow.js Como agregar keywords y respuestas
|
// flow.js Como agregar keywords y respuestas
|
||||||
const { inout, provider, database } = require('@bot-whatsapp')
|
const { inout, provider, database } = require('@bot-whatsapp')
|
||||||
|
|
||||||
await inout.addKeyword('hola')
|
await inout
|
||||||
.addAnswer('Bienvenido a tu tienda 🥲')
|
.addKeyword('hola')
|
||||||
.addAnswer('escribe *catalogo* o *ofertas*')
|
.addAnswer('Bienvenido a tu tienda 🥲')
|
||||||
|
.addAnswer('escribe *catalogo* o *ofertas*')
|
||||||
await inout.addKeyword(['catalogo','ofertas'])
|
|
||||||
.addAnswer('Este es nuestro CATALOGO mas reciente!',{buttons:[
|
|
||||||
{"body":"Xiaomi"},
|
|
||||||
{"body":"Samsung"}
|
|
||||||
]})
|
|
||||||
|
|
||||||
await inout.addKeyword('Xiaomi')
|
|
||||||
.addAnswer('Estos son nuestro productos XIAOMI ....',{media:'https://....'})
|
|
||||||
.addAnswer('Si quieres mas info escrbie *info*')
|
|
||||||
|
|
||||||
|
|
||||||
await inout
|
await inout
|
||||||
.addKeyword('chao!')
|
.addKeyword(['catalogo', 'ofertas'])
|
||||||
.addAnswer('bye!')
|
.addAnswer('Este es nuestro CATALOGO mas reciente!', {
|
||||||
.addAnswer('Recuerda que tengo esta promo',{
|
buttons: [{ body: 'Xiaomi' }, { body: 'Samsung' }],
|
||||||
media:'https://media2.giphy.com/media/VQJu0IeULuAmCwf5SL/giphy.gif',
|
})
|
||||||
})
|
|
||||||
|
|
||||||
await inout.addKeyword('Modelo C',{sensitivy:false})
|
await inout
|
||||||
.addAnswer('100USD', {media:'http//:...'})
|
.addKeyword('Xiaomi')
|
||||||
|
.addAnswer('Estos son nuestro productos XIAOMI ....', {
|
||||||
|
media: 'https://....',
|
||||||
|
})
|
||||||
|
.addAnswer('Si quieres mas info escrbie *info*')
|
||||||
|
|
||||||
await inout.addKeyword('hola!',{sensitivy:false})
|
await inout
|
||||||
.addAnswer('Bievenido Escribe *productos*')
|
.addKeyword('chao!')
|
||||||
|
.addAnswer('bye!')
|
||||||
|
.addAnswer('Recuerda que tengo esta promo', {
|
||||||
|
media: 'https://media2.giphy.com/media/VQJu0IeULuAmCwf5SL/giphy.gif',
|
||||||
|
})
|
||||||
|
|
||||||
await inout.addKeyword('productos',{sensitivy:false})
|
await inout
|
||||||
.addAnswer('Esto son los mas vendidos')
|
.addKeyword('Modelo C', { sensitivy: false })
|
||||||
.addAnswer('*PC1* Precio 10USD',{media:'https://....'})
|
.addAnswer('100USD', { media: 'http//:...' })
|
||||||
.addAnswer('*PC2* Precio 10USD',{media:'https://....'})
|
|
||||||
|
|
||||||
await inout.addKeyword('PC1',{sensitivy:false})
|
await inout
|
||||||
.addAnswer('Bievenido Escribe *productos*')
|
.addKeyword('hola!', { sensitivy: false })
|
||||||
|
.addAnswer('Bievenido Escribe *productos*')
|
||||||
|
|
||||||
|
await inout
|
||||||
|
.addKeyword('productos', { sensitivy: false })
|
||||||
|
.addAnswer('Esto son los mas vendidos')
|
||||||
|
.addAnswer('*PC1* Precio 10USD', { media: 'https://....' })
|
||||||
|
.addAnswer('*PC2* Precio 10USD', { media: 'https://....' })
|
||||||
|
|
||||||
|
await inout
|
||||||
|
.addKeyword('PC1', { sensitivy: false })
|
||||||
|
.addAnswer('Bievenido Escribe *productos*')
|
||||||
|
|
||||||
const answerOne = await inout.addAnswer({
|
const answerOne = await inout.addAnswer({
|
||||||
message:'Como estas!',
|
message: 'Como estas!',
|
||||||
media:'https://media2.giphy.com/media/VQJu0IeULuAmCwf5SL/giphy.gif',
|
media: 'https://media2.giphy.com/media/VQJu0IeULuAmCwf5SL/giphy.gif',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
const otherAnswer = await inout.addAnswer('Aprovecho para decirte!')
|
const otherAnswer = await inout.addAnswer('Aprovecho para decirte!')
|
||||||
|
|
||||||
answerOne.push(otherAnswer)
|
answerOne.push(otherAnswer)
|
||||||
|
|
||||||
|
inout.addKeywords(['hola', 'hi', 'ola'])
|
||||||
inout.addKeywords(['hola','hi','ola'])
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
const { addKeyword, addAnswer } = require('./methods')
|
||||||
|
|
||||||
|
const test = async () => {
|
||||||
|
const cxtA = addKeyword('hola')
|
||||||
|
console.log({ cxtA: cxtA.ctx.keyword, ref: cxtA.ref })
|
||||||
|
const cxtB = addAnswer(cxtA)('b')
|
||||||
|
console.log({ cxtB: cxtB.ctx.message, ref: cxtB.ref })
|
||||||
|
const cxtC = addAnswer(cxtB)('c')
|
||||||
|
console.log({ cxtC: cxtC.ctx.keyword, ref: cxtC.ref })
|
||||||
|
}
|
||||||
|
|
||||||
|
const test1 = async () => {
|
||||||
|
const cxtAB = addKeyword('hola').addAnswer('b').addAnswer('c')
|
||||||
|
|
||||||
|
console.log({
|
||||||
|
keyword: cxtAB.ctx.keyword,
|
||||||
|
anwser: cxtAB.ctx.message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const test2 = async () => {
|
||||||
|
const cxtABB = addKeyword('hola')
|
||||||
|
.addAnswer('Bienvenido a tu tienda 🥲')
|
||||||
|
.addAnswer('escribe *catalogo* o *ofertas*')
|
||||||
|
|
||||||
|
console.log({
|
||||||
|
pregunta: cxtABB.ctx.keyword,
|
||||||
|
ultimasrespuesta: cxtABB.ctx.message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
test2().then()
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
/**
|
|
||||||
*
|
|
||||||
* @param {*} message `string | string[]`
|
|
||||||
* @param {*} options {sensitivy:boolean} defaulta false
|
|
||||||
*/
|
|
||||||
const addKeyword = (message, options) => {
|
|
||||||
if (typeof message === 'string') return 1
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { addKeyword }
|
|
||||||
// await inout.addKeyword('hola')
|
|
||||||
// .addAnswer('Bienvenido a tu tienda 🥲')
|
|
||||||
// .addAnswer('escribe *catalogo* o *ofertas*')
|
|
||||||
|
|
||||||
// await inout.addKeyword(['catalogo','ofertas'])
|
|
||||||
28
packages/io/methods/addAnswer.js
Normal file
28
packages/io/methods/addAnswer.js
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
const { generateRef } = require('../utils')
|
||||||
|
|
||||||
|
const addAnswer = (inCtx) => (message, options) => {
|
||||||
|
const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx
|
||||||
|
const ctxAnswer = () => {
|
||||||
|
const ref = generateRef()
|
||||||
|
/**
|
||||||
|
* Se guarda en db
|
||||||
|
*/
|
||||||
|
|
||||||
|
return { ...lastCtx, ref, message }
|
||||||
|
}
|
||||||
|
|
||||||
|
const ctx = ctxAnswer()
|
||||||
|
|
||||||
|
return {
|
||||||
|
ctx,
|
||||||
|
ref: ctx.ref,
|
||||||
|
addAnswer: addAnswer(ctx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { addAnswer }
|
||||||
|
|
||||||
|
// await inout
|
||||||
|
// .addKeyword('hola')
|
||||||
|
// .addAnswer('Bienvenido a tu tienda 🥲')
|
||||||
|
// .addAnswer('escribe *catalogo* o *ofertas*')
|
||||||
32
packages/io/methods/addKeyword.js
Normal file
32
packages/io/methods/addKeyword.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
const { generateRef } = require('../utils')
|
||||||
|
const { addAnswer } = require('./addAnswer')
|
||||||
|
/**
|
||||||
|
* addKeyword:
|
||||||
|
* Es necesario que genere id|hash
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {*} message `string | string[]`
|
||||||
|
* @param {*} options {sensitivy:boolean} defaulta false
|
||||||
|
*/
|
||||||
|
const addKeyword = (message, options) => {
|
||||||
|
const ctxAddKeyword = () => {
|
||||||
|
const ref = generateRef()
|
||||||
|
/**
|
||||||
|
* Se guarda en db
|
||||||
|
*/
|
||||||
|
|
||||||
|
return { ref, keyword: message }
|
||||||
|
}
|
||||||
|
|
||||||
|
const ctx = ctxAddKeyword()
|
||||||
|
|
||||||
|
return {
|
||||||
|
ctx,
|
||||||
|
ref: ctx.ref,
|
||||||
|
addAnswer: addAnswer(ctx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { addKeyword }
|
||||||
4
packages/io/methods/index.js
Normal file
4
packages/io/methods/index.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
const { addAnswer } = require('./addAnswer')
|
||||||
|
const { addKeyword } = require('./addKeyword')
|
||||||
|
|
||||||
|
module.exports = { addAnswer, addKeyword }
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"io:dev": "NODE_ENV=dev node ./index.js",
|
"io:dev": "node ./index.js",
|
||||||
"io:rollup": "rollup index.js --config ./rollup-cli.config.js",
|
"io:rollup": "rollup index.js --config ./rollup-cli.config.js",
|
||||||
"format:check": "prettier --check .",
|
"format:check": "prettier --check .",
|
||||||
"format:write": "prettier --write .",
|
"format:write": "prettier --write .",
|
||||||
@@ -16,7 +16,5 @@
|
|||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {}
|
||||||
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
7
packages/io/utils/hash.js
Normal file
7
packages/io/utils/hash.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
const crypto = require('crypto')
|
||||||
|
|
||||||
|
const generateRef = () => {
|
||||||
|
return crypto.randomUUID()
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { generateRef }
|
||||||
3
packages/io/utils/index.js
Normal file
3
packages/io/utils/index.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
const { generateRef } = require('./hash')
|
||||||
|
|
||||||
|
module.exports = { generateRef }
|
||||||
Reference in New Issue
Block a user