From 429fc202cdb0b0ba1be972d6552fb867d354914f Mon Sep 17 00:00:00 2001 From: Caio Agiani Date: Wed, 17 Aug 2022 00:55:46 -0300 Subject: [PATCH] feat(project): refact dispatch command (#92) --- .github/FUNDING.yml | 1 + .gitignore | 11 ++++------- package.json | 20 +++++++++++--------- src/app/utils/CommandDispatcher.ts | 17 +++++------------ src/data/.gitkeep | 0 src/services/whatsapp.ts | 12 ++++++++---- 6 files changed, 29 insertions(+), 32 deletions(-) create mode 100644 src/data/.gitkeep diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 10f8a5b..f72047e 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1 +1,2 @@ +github: [caioagiani] custom: ['https://nubank.com.br/pagar/3lf5k/hgTQsUWzua'] diff --git a/.gitignore b/.gitignore index 1fad437..2681c02 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,9 @@ .env - dist node_modules - -yarn.lock - +*.lock src/config/integrantes.json - .idea - -.wwebjs_auth \ No newline at end of file +.wwebjs_auth +src/data/* +!src/data/.gitkeep diff --git a/package.json b/package.json index 22e3d89..7a1fa73 100644 --- a/package.json +++ b/package.json @@ -2,18 +2,20 @@ "name": "whatsapp-web-bot", "main": "src/index.ts", "description": "WhatsApp Bot", - "version": "0.6.5", + "version": "0.7.0", "license": "MIT", "keywords": [ "whatsapp", "whatsapp-web", "whatsapp-bot", "whatsapp-api", + "whatsapp-multi-session", "whatsapp-multi-device", "api", "bot", "client", "node", + "nodejs", "typescript" ], "bugs": { @@ -54,21 +56,21 @@ "mobizon-node": "^0.5.0", "node-base64-image": "^2.0.1", "qrcode-terminal": "^0.12.0", - "whatsapp-web.js": "^1.16.7" + "whatsapp-web.js": "^1.17.1" }, "devDependencies": { - "@types/node": "16.11.41", - "@types/qrcode": "1.4.2", + "@types/node": "16.11.49", + "@types/qrcode": "1.4.3", "@types/qrcode-terminal": "0.12.0", - "@typescript-eslint/eslint-plugin": "5.28.0", - "@typescript-eslint/parser": "5.28.0", + "@typescript-eslint/eslint-plugin": "5.33.1", + "@typescript-eslint/parser": "5.33.1", "dotenv": "16.0.1", - "eslint": "8.18.0", + "eslint": "8.22.0", "eslint-config-prettier": "8.5.0", "eslint-loader": "4.0.2", - "eslint-plugin-prettier": "4.0.0", + "eslint-plugin-prettier": "4.2.1", "prettier": "2.7.1", - "ts-node-dev": "1.1.8", + "ts-node-dev": "2.0.0", "typescript": "4.7.4" } } diff --git a/src/app/utils/CommandDispatcher.ts b/src/app/utils/CommandDispatcher.ts index 98d0a3f..828078a 100644 --- a/src/app/utils/CommandDispatcher.ts +++ b/src/app/utils/CommandDispatcher.ts @@ -1,22 +1,15 @@ import Command from './Command'; class CommandDispatcher { - private commandsHandlers: Map> = new Map(); + private commandsHandlers: Map> = new Map(); - async register(name: string, command: Command) { + async register(name: string, command: Command) { this.commandsHandlers.set(name, command); } - async dispatch(name: string, message: any) { - for (const [key, _] of this.commandsHandlers) { - if (name.includes(key)) { - if (!this.commandsHandlers.has(key)) { - return; - } - - const command = this.commandsHandlers.get(key); - await command.execute(message); - } + async dispatch(name: string, message: string) { + for (const [key, command] of this.commandsHandlers) { + if (name.includes(key)) command.execute(message); } } } diff --git a/src/data/.gitkeep b/src/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/services/whatsapp.ts b/src/services/whatsapp.ts index c33a669..3b34e09 100644 --- a/src/services/whatsapp.ts +++ b/src/services/whatsapp.ts @@ -1,8 +1,12 @@ import { Client, MessageMedia, LocalAuth } from 'whatsapp-web.js'; import * as qrcode from 'qrcode-terminal'; +import { resolve } from 'path'; const client = new Client({ - authStrategy: new LocalAuth(), + authStrategy: new LocalAuth({ + clientId: 'wpp-bot', + dataPath: resolve(__dirname, '..', 'data'), + }), puppeteer: { headless: true, args: ['--no-sandbox'], @@ -10,13 +14,13 @@ const client = new Client({ }); client.on('qr', async (qr) => qrcode.generate(qr, { small: true })); -client.on('authenticated', () => console.log('authenticated')); -client.on('auth_failure', () => console.log('Authentication failed.')); +client.on('authenticated', () => console.log('WhatsApp authenticated.')); +client.on('auth_failure', () => console.log('WhatsApp authentication failed.')); client.on('disconnected', () => console.log('WhatsApp lost connection.')); client.on('ready', async () => { await client.sendMessage( '5511999865802@c.us', - `[${client.info.pushname}] - WhatsApp Online\n[x] Please, *like* project: https://github.com/caioagiani/whatsapp-bot`, + `[${client.info.pushname}] - WhatsApp Online\n\n[⭐] Please *like* this project: https://github.com/caioagiani/whatsapp-bot\n\n[💝] Sponsor this SourceCode: https://github.com/sponsors/caioagiani`, ); console.log('WhatsApp bot successfully connected!');