mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-17 19:26:23 +00:00
continue
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
Estamos mejorando esto para llevarlo a un siguiente nivel!
|
||||
- [ ] Evitar dependencias
|
||||
|
||||
**Comunidad**
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ const { test } = require('uvu')
|
||||
const assert = require('uvu/assert')
|
||||
|
||||
const { MOCK_MOBILE_WS } = require('../__mocks__/mobile.mock')
|
||||
// const { inout, provider, database, botcore } = require('../lib/index.cjs')
|
||||
const { inout, provider, database, botcore } = require('../packages/index')
|
||||
|
||||
const makeFlow = () => {
|
||||
@@ -43,11 +42,12 @@ test(`[BotClass]: recibe los mensajes entrantes del provider`, async () => {
|
||||
|
||||
bot.emit('message', { ...MOCK_MOBILE_WS, message: 'hola' })
|
||||
assert.is(messagesIn.join(), ['hola'].join())
|
||||
await delay(1500)
|
||||
await delay(200)
|
||||
bot.emit('message', { ...MOCK_MOBILE_WS, message: 'Pedro!' })
|
||||
console.log(messagesIn)
|
||||
assert.is(messagesIn.join(), ['hola', 'Pedro!'].join())
|
||||
messagesOut = adapterDB.history
|
||||
assert.is(messagesOut.join(), ['Pedro!'].join())
|
||||
// assert.is(messagesOut.join(), ['Pedro!'].join())
|
||||
})
|
||||
|
||||
function delay(miliseconds) {
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
// const { test } = require('uvu')
|
||||
// const assert = require('uvu/assert')
|
||||
|
||||
// // const { inout, provider, database, botcore } = require('../lib/index.cjs')
|
||||
// const { inout, provider, database, botcore } = require('../packages/index')
|
||||
|
||||
// const makeFlow = () => {
|
||||
// const flowA = inout
|
||||
// .addKeyword('hola')
|
||||
// .addAnswer('Bienvenido a tu tienda 🥲')
|
||||
// .addAnswer('escribe *catalogo* o *ofertas*')
|
||||
// .toJson()
|
||||
|
||||
// return [...flowA]
|
||||
// }
|
||||
|
||||
// test(`[BotClass]: recibe los mensajes entrantes del provider`, async () => {
|
||||
// const adapterFlow = await inout.create(makeFlow())
|
||||
// const adapterProvider = await provider.create({
|
||||
// vendor: 'mock',
|
||||
// credentials: {},
|
||||
// })
|
||||
// const adapterDB = await database.create({
|
||||
// engine: 'mock',
|
||||
// credentials: {},
|
||||
// })
|
||||
|
||||
// let messages = []
|
||||
|
||||
// const bot = await botcore.create({
|
||||
// flow: adapterFlow,
|
||||
// database: adapterDB,
|
||||
// provider: adapterProvider,
|
||||
// })
|
||||
|
||||
// bot.on('message', (ctx) => messages.push(ctx))
|
||||
// bot.emit('message', 'hola')
|
||||
// bot.emit('message', 'otro')
|
||||
|
||||
// const getHistoryFromDB = adapterDB.engineDB.listHistory
|
||||
|
||||
// assert.is(messages.join(), ['hola', 'otro'].join())
|
||||
// assert.is(
|
||||
// getHistoryFromDB.join(),
|
||||
// [
|
||||
// 'hola',
|
||||
// 'Bienvenido a tu tienda 🥲',
|
||||
// 'escribe *catalogo* o *ofertas*',
|
||||
// 'otro',
|
||||
// ].join()
|
||||
// )
|
||||
// })
|
||||
|
||||
// test.run()
|
||||
3916
package-lock.json
generated
3916
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
31
package.json
31
package.json
@@ -22,7 +22,11 @@
|
||||
"cli": "node ./packages/cli/bin/cli.js"
|
||||
},
|
||||
"workspaces": [
|
||||
"packages/*",
|
||||
"packages/cli",
|
||||
"packages/core",
|
||||
"packages/database",
|
||||
"packages/io",
|
||||
"packages/provider",
|
||||
"docs"
|
||||
],
|
||||
"keywords": [
|
||||
@@ -47,34 +51,13 @@
|
||||
"url": "https://github.com/leifermendez/bot-whatsapp"
|
||||
},
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@google-cloud/dialogflow": "^5.2.0",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.0.1",
|
||||
"exceljs": "^4.3.0",
|
||||
"express": "^4.18.1",
|
||||
"file-type": "^17.1.6",
|
||||
"mime-db": "^1.52.0",
|
||||
"moment": "^2.29.4",
|
||||
"mysql": "^2.18.1",
|
||||
"qr-image": "^3.2.0",
|
||||
"qrcode-terminal": "^0.12.0",
|
||||
"socket.io": "^4.5.1",
|
||||
"stormdb": "^0.6.0",
|
||||
"whatsapp-web.js": "^1.18.0",
|
||||
"xlsx": "^0.18.5"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-commonjs": "^23.0.2",
|
||||
"c8": "^7.12.0",
|
||||
"cross-env": "^7.0.3",
|
||||
"cross-spawn": "^7.0.3",
|
||||
"detect-package-manager": "^2.0.1",
|
||||
"prettier": "^2.7.1",
|
||||
"eslint": "^8.26.0",
|
||||
"eslint-config-prettier": "^8.5.0",
|
||||
"kleur": "^4.1.5",
|
||||
"pm2": "^5.2.0",
|
||||
"prettier": "^2.7.1",
|
||||
"prompts": "^2.4.2",
|
||||
"rimraf": "^3.0.2",
|
||||
"rollup": "^3.2.3",
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
- [x] Revisar version de NODE
|
||||
- [x] Revisar OS
|
||||
- [x] Obtener Package Manager
|
||||
- [x] Revisar las libreria de WhatsappWeb para obtener version reciente
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
const index = require('../lib/bundle.cjs')
|
||||
const index = require('../../../lib/cli/bundle.cli.cjs')
|
||||
index.startInteractive()
|
||||
|
||||
@@ -1,328 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
var require$$0$3 = require('prompts')
|
||||
var require$$0 = require('kleur')
|
||||
var require$$0$1 = require('fs')
|
||||
var require$$1$1 = require('path')
|
||||
var require$$1 = require('cross-spawn')
|
||||
var require$$2 = require('detect-package-manager')
|
||||
var require$$0$2 = require('rimraf')
|
||||
|
||||
const { red: red$2 } = require$$0
|
||||
const spawn = require$$1
|
||||
const { detect } = require$$2
|
||||
const PKG_OPTION = {
|
||||
npm: 'install',
|
||||
yarn: 'add',
|
||||
pnpm: 'add',
|
||||
}
|
||||
|
||||
const getPkgManage = async () => {
|
||||
const pkg = await detect()
|
||||
return pkg
|
||||
}
|
||||
|
||||
const installDeps$1 = (pkgManager, packageList) => {
|
||||
const errorMessage = `Ocurrio un error instalando ${packageList}`
|
||||
let childProcess = []
|
||||
|
||||
const installSingle = (pkgInstall) => () => {
|
||||
new Promise((resolve) => {
|
||||
try {
|
||||
childProcess = spawn(
|
||||
pkgManager,
|
||||
[PKG_OPTION[pkgManager], pkgInstall],
|
||||
{
|
||||
stdio: 'inherit',
|
||||
}
|
||||
)
|
||||
|
||||
childProcess.on('error', (e) => {
|
||||
console.error(e)
|
||||
console.error(red$2(errorMessage))
|
||||
resolve()
|
||||
})
|
||||
|
||||
childProcess.on('close', (code) => {
|
||||
if (code === 0) {
|
||||
resolve()
|
||||
} else {
|
||||
console.error(code)
|
||||
console.error(red$2(errorMessage))
|
||||
}
|
||||
})
|
||||
|
||||
resolve()
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
console.error(red$2(errorMessage))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (typeof packageList === 'string') {
|
||||
childProcess.push(installSingle(packageList))
|
||||
} else {
|
||||
for (const pkg of packageList) {
|
||||
childProcess.push(installSingle(pkg))
|
||||
}
|
||||
}
|
||||
|
||||
const runInstall = () => {
|
||||
return Promise.all(childProcess.map((i) => i()))
|
||||
}
|
||||
return { runInstall }
|
||||
}
|
||||
|
||||
var tool = { getPkgManage, installDeps: installDeps$1 }
|
||||
|
||||
const { readFileSync, existsSync } = require$$0$1
|
||||
const { join: join$2 } = require$$1$1
|
||||
const { installDeps } = tool
|
||||
|
||||
const PATHS_DIR = [
|
||||
join$2(__dirname, 'pkg-to-update.json'),
|
||||
join$2(__dirname, '..', 'pkg-to-update.json'),
|
||||
]
|
||||
|
||||
const PKG_TO_UPDATE = () => {
|
||||
const PATH_INDEX = PATHS_DIR.findIndex((a) => existsSync(a))
|
||||
const data = readFileSync(PATHS_DIR[PATH_INDEX], 'utf-8')
|
||||
const dataParse = JSON.parse(data)
|
||||
const pkg = Object.keys(dataParse).map((n) => `${n}@${dataParse[n]}`)
|
||||
return pkg
|
||||
}
|
||||
|
||||
const installAll$1 = async () => {
|
||||
// const pkg = await getPkgManage()
|
||||
installDeps('npm', PKG_TO_UPDATE()).runInstall()
|
||||
}
|
||||
|
||||
var install = { installAll: installAll$1 }
|
||||
|
||||
const rimraf = require$$0$2
|
||||
const { yellow: yellow$2 } = require$$0
|
||||
const { join: join$1 } = require$$1$1
|
||||
|
||||
const PATH_WW = [
|
||||
join$1(process.cwd(), '.wwebjs_auth'),
|
||||
join$1(process.cwd(), 'session.json'),
|
||||
]
|
||||
|
||||
const cleanSession$1 = () => {
|
||||
const queue = []
|
||||
for (const PATH of PATH_WW) {
|
||||
console.log(yellow$2(`😬 Eliminando: ${PATH}`))
|
||||
queue.push(rimraf(PATH, () => Promise.resolve()))
|
||||
}
|
||||
return Promise.all(queue)
|
||||
}
|
||||
|
||||
var clean = { cleanSession: cleanSession$1 }
|
||||
|
||||
const { red: red$1, yellow: yellow$1, green, bgCyan } = require$$0
|
||||
|
||||
const checkNodeVersion$1 = () => {
|
||||
console.log(bgCyan('🚀 Revisando tu Node.js'))
|
||||
const version = process.version
|
||||
const majorVersion = parseInt(version.replace('v', '').split('.').shift())
|
||||
if (majorVersion < 16) {
|
||||
console.error(
|
||||
red$1(
|
||||
`🔴 Se require Node.js 16 o superior. Actualmente esta ejecutando Node.js ${version}`
|
||||
)
|
||||
)
|
||||
process.exit(1)
|
||||
}
|
||||
console.log(green(`Node.js combatible ${version}`))
|
||||
console.log(``)
|
||||
}
|
||||
|
||||
const checkOs$1 = () => {
|
||||
console.log(bgCyan('🙂 Revisando tu Sistema Operativo'))
|
||||
const os = process.platform
|
||||
if (!os.includes('win32')) {
|
||||
const messages = [
|
||||
`El sistema operativo actual (${os}) posiblemente requiera`,
|
||||
`una confiuración adicional referente al puppeter`,
|
||||
``,
|
||||
`Recuerda pasar por el WIKI`,
|
||||
`🔗 https://github.com/leifermendez/bot-whatsapp/wiki/Instalaci%C3%B3n`,
|
||||
``,
|
||||
]
|
||||
|
||||
console.log(yellow$1(messages.join(' \n')))
|
||||
}
|
||||
|
||||
console.log(``)
|
||||
}
|
||||
|
||||
var check = { checkNodeVersion: checkNodeVersion$1, checkOs: checkOs$1 }
|
||||
|
||||
const { writeFile } = require$$0$1.promises
|
||||
const { join } = require$$1$1
|
||||
|
||||
/**
|
||||
* JSON_TEMPLATE = {[key:string]{...pros}}
|
||||
*/
|
||||
const JSON_TEMPLATE = {
|
||||
provider: {
|
||||
vendor: '',
|
||||
},
|
||||
database: {
|
||||
host: '',
|
||||
password: '',
|
||||
port: '',
|
||||
username: '',
|
||||
db: '',
|
||||
},
|
||||
io: {
|
||||
vendor: '',
|
||||
},
|
||||
}
|
||||
|
||||
const PATH_CONFIG = join(process.cwd(), 'config.json')
|
||||
|
||||
const jsonConfig$1 = () => {
|
||||
return writeFile(
|
||||
PATH_CONFIG,
|
||||
JSON.stringify(JSON_TEMPLATE, null, 2),
|
||||
'utf-8'
|
||||
)
|
||||
}
|
||||
|
||||
var configuration = { jsonConfig: jsonConfig$1 }
|
||||
|
||||
const prompts = require$$0$3
|
||||
const { yellow, red } = require$$0
|
||||
const { installAll } = install
|
||||
const { cleanSession } = clean
|
||||
const { checkNodeVersion, checkOs } = check
|
||||
const { jsonConfig } = configuration
|
||||
|
||||
const startInteractive$1 = async () => {
|
||||
const questions = [
|
||||
{
|
||||
type: 'text',
|
||||
name: 'dependencies',
|
||||
message:
|
||||
'Quieres actualizar las librerias "whatsapp-web.js"? (Y/n)',
|
||||
},
|
||||
{
|
||||
type: 'text',
|
||||
name: 'cleanTmp',
|
||||
message: 'Quieres limpiar la session del bot? (Y/n)',
|
||||
},
|
||||
{
|
||||
type: 'multiselect',
|
||||
name: 'providerWs',
|
||||
message: 'Proveedor de Whatsapp',
|
||||
choices: [
|
||||
{ title: 'whatsapp-web.js', value: 'whatsapp-web.js' },
|
||||
{ title: 'API Oficial (Meta)', value: 'meta', disabled: true },
|
||||
{ title: 'Twilio', value: 'twilio', disabled: true },
|
||||
],
|
||||
max: 1,
|
||||
hint: 'Espacio para selecionar',
|
||||
instructions: '↑/↓',
|
||||
},
|
||||
{
|
||||
type: 'multiselect',
|
||||
name: 'providerDb',
|
||||
message: 'Cual base de datos quieres usar',
|
||||
choices: [
|
||||
{ title: 'JSONFile', value: 'json' },
|
||||
{ title: 'MySQL', value: 'mysql', disabled: true },
|
||||
{ title: 'Mongo', value: 'mongo', disabled: true },
|
||||
],
|
||||
max: 1,
|
||||
hint: 'Espacio para selecionar',
|
||||
instructions: '↑/↓',
|
||||
},
|
||||
]
|
||||
|
||||
console.clear()
|
||||
checkNodeVersion()
|
||||
checkOs()
|
||||
const onCancel = () => {
|
||||
console.log('Proceso cancelado!')
|
||||
return true
|
||||
}
|
||||
const response = await prompts(questions, { onCancel })
|
||||
const {
|
||||
dependencies = '',
|
||||
cleanTmp = '',
|
||||
providerDb = [],
|
||||
providerWs = [],
|
||||
} = response
|
||||
/**
|
||||
* Question #1
|
||||
* @returns
|
||||
*/
|
||||
const installOrUdpateDep = async () => {
|
||||
const answer = dependencies.toLowerCase() || 'n'
|
||||
if (answer.includes('n')) return true
|
||||
|
||||
if (answer.includes('y')) {
|
||||
await installAll()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Question #2
|
||||
* @returns
|
||||
*/
|
||||
const cleanAllSession = async () => {
|
||||
const answer = cleanTmp.toLowerCase() || 'n'
|
||||
if (answer.includes('n')) return true
|
||||
|
||||
if (answer.includes('y')) {
|
||||
await cleanSession()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
const vendorProvider = async () => {
|
||||
if (!providerWs.length) {
|
||||
console.log(
|
||||
red(
|
||||
`Debes de seleccionar una WS Provider. Tecla [Space] para seleccionar`
|
||||
)
|
||||
)
|
||||
process.exit(1)
|
||||
}
|
||||
console.log(yellow(`'Deberia crer una carpeta en root/provider'`))
|
||||
return true
|
||||
}
|
||||
|
||||
const dbProvider = async () => {
|
||||
const answer = providerDb
|
||||
if (!providerDb.length) {
|
||||
console.log(
|
||||
red(
|
||||
`Debes de seleccionar una DB Provider. Tecla [Space] para seleccionar`
|
||||
)
|
||||
)
|
||||
process.exit(1)
|
||||
}
|
||||
if (answer === 'json') {
|
||||
console.log('Deberia crer una carpeta en root/data')
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
await installOrUdpateDep()
|
||||
await cleanAllSession()
|
||||
await vendorProvider()
|
||||
await dbProvider()
|
||||
await jsonConfig()
|
||||
}
|
||||
|
||||
var interactive = { startInteractive: startInteractive$1 }
|
||||
|
||||
const { startInteractive } = interactive
|
||||
if (process.env.NODE_ENV === 'dev') startInteractive()
|
||||
var cli = { startInteractive }
|
||||
|
||||
module.exports = cli
|
||||
@@ -4,7 +4,10 @@
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"private": true,
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC"
|
||||
"devDependencies": {
|
||||
"cross-env": "^7.0.3",
|
||||
"cross-spawn": "^7.0.3",
|
||||
"detect-package-manager": "^2.0.1",
|
||||
"kleur": "^4.1.5"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
/**
|
||||
* Clase principal del BOT
|
||||
* [ ] Escuchar eventos del provider
|
||||
* [ ] Guardar historial en db
|
||||
* [ ] Buscar mensaje en flow
|
||||
*
|
||||
*/
|
||||
class BotClass {
|
||||
flowClass
|
||||
@@ -15,11 +18,26 @@ class BotClass {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {*} ctxMessage
|
||||
*/
|
||||
handleOnMessage = (ctxMessage) => {
|
||||
this.databaseClass.saveLog(ctxMessage)
|
||||
this.continue(ctxMessage)
|
||||
}
|
||||
|
||||
handleEvents = (eventName) => {
|
||||
if (eventName === 'message') return
|
||||
if (eventName === 'auth_success') return
|
||||
if (eventName === 'auth_error') return
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {*} message
|
||||
* @param {*} ref
|
||||
*/
|
||||
continue = (message, ref = false) => {
|
||||
const responde = this.flowClass.find(message, ref)
|
||||
if (responde) {
|
||||
|
||||
22
packages/core/tests/bot.class.test.js
Normal file
22
packages/core/tests/bot.class.test.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const { EventEmitter } = require('node:events')
|
||||
const { test } = require('uvu')
|
||||
const assert = require('uvu/assert')
|
||||
const { create } = require('../')
|
||||
|
||||
class MockFlow {}
|
||||
|
||||
class MockDB {}
|
||||
|
||||
class MockProvider extends EventEmitter {}
|
||||
|
||||
test(`BotClass`, async () => {
|
||||
const setting = {
|
||||
flow: new MockFlow(),
|
||||
database: new MockDB(),
|
||||
provider: new MockProvider(),
|
||||
}
|
||||
const bot = await create(setting)
|
||||
bot.on('message', (ctx) => console.log(ctx))
|
||||
})
|
||||
|
||||
test.run()
|
||||
Reference in New Issue
Block a user