-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact(project): adjusted dispatch commands (#37)
* chore(gitignore): removed .ide * chore(package): update version and upgrade packages * refact(project): adjusted dispatch commands
- Loading branch information
1 parent
aa58aa8
commit 9378e10
Showing
10 changed files
with
60 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ src/data/session.json | |
!src/data/.gitkeep | ||
|
||
src/config/integrantes.json | ||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,23 @@ | ||
export { default as EconomyCommand } from './EconomyCommand'; | ||
export { default as QuoteCommand } from './QuoteCommand'; | ||
export { default as CepCommand } from './CepCommand'; | ||
export { default as ProfileCommand } from './ProfileCommand'; | ||
export { default as SmsCommand } from './SmsCommand'; | ||
import { commandDispatcher } from '../utils/CommandDispatcher'; | ||
|
||
import EconomyCommand from './EconomyCommand'; | ||
import QuoteCommand from './QuoteCommand'; | ||
import CepCommand from './CepCommand'; | ||
import ProfileCommand from './ProfileCommand'; | ||
import SmsCommand from './SmsCommand'; | ||
|
||
export default async (message) => { | ||
const economyCommand = new EconomyCommand(); | ||
const quoteCommand = new QuoteCommand(); | ||
const cepCommand = new CepCommand(message.body); | ||
const profileCommand = new ProfileCommand(message.body); | ||
const smsCommand = new SmsCommand(message.body); | ||
|
||
await commandDispatcher.register('cotacao', economyCommand); | ||
await commandDispatcher.register('mencionar', quoteCommand); | ||
await commandDispatcher.register('cep', cepCommand); | ||
await commandDispatcher.register('perfil', profileCommand); | ||
await commandDispatcher.register('sms', smsCommand); | ||
|
||
return commandDispatcher.dispatch(message.body.slice(1), message); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,7 @@ | ||
import { client } from './services/whatsapp'; | ||
import { | ||
EconomyCommand, | ||
QuoteCommand, | ||
CepCommand, | ||
ProfileCommand, | ||
SmsCommand, | ||
} from './app/commands'; | ||
import { commandDispatcher } from './app/utils/CommandDispatcher'; | ||
import type { Message } from 'whatsapp-web.js'; | ||
import dispatchCommand from './app/commands'; | ||
|
||
client.on('message', (message: Message) => { | ||
const economyCommand = new EconomyCommand(); | ||
const quoteCommand = new QuoteCommand(); | ||
const cepCommand = new CepCommand(message.body); | ||
const profileCommand = new ProfileCommand(message.body); | ||
const smsCommand = new SmsCommand(message.body); | ||
|
||
commandDispatcher.register('cotacao', economyCommand); | ||
commandDispatcher.register('mencionar', quoteCommand); | ||
commandDispatcher.register('cep', cepCommand); | ||
commandDispatcher.register('perfil', profileCommand); | ||
commandDispatcher.register('sms', smsCommand); | ||
|
||
if (message.body.startsWith('!')) { | ||
commandDispatcher.dispatch(message.body.slice(1), message); | ||
} | ||
if (message.body.startsWith('!')) return dispatchCommand(message); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,7 @@ import { generate } from 'qrcode-terminal'; | |
import { existsSync, writeFile, unlink } from 'fs'; | ||
import { resolve } from 'path'; | ||
|
||
interface INotification { | ||
reply: (args: string) => void; | ||
recipientIds: any[]; | ||
} | ||
|
||
const sessionFile = resolve('src', 'data', 'session.json'); | ||
|
||
const session = existsSync(sessionFile) ? require(sessionFile) : null; | ||
|
||
const client = new Client({ | ||
|
@@ -31,37 +25,27 @@ client.on('authenticated', (session: any) => { | |
}); | ||
|
||
client.on('ready', async () => { | ||
console.log('WhatsApp bot conectado com sucesso!'); | ||
console.log('WhatsApp bot successfully connected!'); | ||
|
||
const { pushname } = client.info; | ||
|
||
client.sendMessage( | ||
await client.sendMessage( | ||
'[email protected]', | ||
`[${pushname}] - WhatsApp Online\n\n[x] Star on project: https://github.com/caioagiani/whatsapp-bot`, | ||
`[${client.info.pushname}] - WhatsApp Online\n\n[x] Star project: https://github.com/caioagiani/whatsapp-bot`, | ||
); | ||
}); | ||
|
||
client.on('auth_failure', () => { | ||
unlink(sessionFile, () => { | ||
console.log('Autenticação falhou, tente novamente.'); | ||
console.log('Authentication failed, try again.'); | ||
process.exit(1); | ||
}); | ||
}); | ||
|
||
client.on('disconnected', () => { | ||
unlink(sessionFile, () => | ||
console.log('Sessão WhatsApp perdeu a conexão, tente novamente.'), | ||
console.log('WhatsApp session lost connection, try again.'), | ||
); | ||
}); | ||
|
||
client.on('group_join', (notification: INotification) => { | ||
notification.reply(`${notification.recipientIds[0]} entrou no grupo.`); | ||
}); | ||
|
||
client.on('group_leave', (notification: INotification) => { | ||
notification.reply(`${notification.recipientIds[0]} saiu do grupo.`); | ||
}); | ||
|
||
client.initialize(); | ||
|
||
export { client, MessageMedia }; |