Skip to content

Commit

Permalink
feat(project): refact dispatch command (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
caioagiani authored Aug 17, 2022
1 parent f497c42 commit 10a5035
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 33 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github: [caioagiani]
custom: ['https://nubank.com.br/pagar/3lf5k/hgTQsUWzua']
11 changes: 4 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
.env

dist
node_modules

yarn.lock

*.lock
src/config/integrantes.json

.idea

.wwebjs_auth
.wwebjs_auth
src/data/*
!src/data/.gitkeep
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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"
}
}
17 changes: 5 additions & 12 deletions src/app/utils/CommandDispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import Command from './Command';

class CommandDispatcher {
private commandsHandlers: Map<string, Command<any>> = new Map();
private commandsHandlers: Map<string, Command<string>> = new Map();

async register(name: string, command: Command<any>) {
async register(name: string, command: Command<string>) {
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);
}
}
}
Expand Down
Empty file added src/data/.gitkeep
Empty file.
14 changes: 9 additions & 5 deletions src/services/whatsapp.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
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,
headless: false,
args: ['--no-sandbox'],
},
});

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(
'[email protected]',
`[${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!');
Expand Down

0 comments on commit 10a5035

Please sign in to comment.