-
-
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.
feat(project): added command sms & refactored project (#26)
* feat(project): added command sms & refactored project * chore(package): updated versions
- Loading branch information
1 parent
32f2418
commit 00d4822
Showing
20 changed files
with
145 additions
and
59 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
MOBIZON_URL_SRV=https://api.mobizon.com.br | ||
MOBIZON_API_KEY= |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Lint | ||
|
||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
eslint: | ||
name: eslint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: install node v12 | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: 12 | ||
- name: npm install | ||
run: npm install | ||
- name: eslint | ||
uses: icrawl/action-eslint@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
job-name: eslint |
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 |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
"semi": true, | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"trailingComma": "es5" | ||
} | ||
"trailingComma": "all" | ||
} |
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,2 +1 @@ | ||
// declare module 'whatsapp-web.js'; | ||
declare module 'qrcode-terminal'; |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { Message } from 'whatsapp-web.js'; | ||
import mobizon from '../../services/mobizon'; | ||
|
||
export default class ProfileCommand { | ||
mention: string; | ||
constructor(mention: string) { | ||
this.mention = mention; | ||
} | ||
|
||
async execute(msg: Message) { | ||
const chat = await msg.getChat(); | ||
|
||
const [contact] = await msg.getMentions(); | ||
|
||
chat.sendStateTyping(); | ||
|
||
if (!chat.isGroup) { | ||
return msg.reply('Comando apenas para grupos!'); | ||
} | ||
|
||
if (!contact) return msg.reply('Contato não localizado.'); | ||
|
||
const sendSms = await mobizon.sendSms({ | ||
recipient: contact.number, | ||
from: '', | ||
text: 'Sms enviado via BOT.', | ||
}); | ||
|
||
if (sendSms.code !== 0) { | ||
return msg.reply('Oops, houve um erro ao enviar SMS, tente novamente.'); | ||
} | ||
|
||
return msg.reply('SMS enviado com sucesso!'); | ||
} | ||
} |
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,13 +1,11 @@ | ||
interface IResponse { | ||
export interface IResponse { | ||
data: IServerData; | ||
} | ||
|
||
interface IServerData { | ||
export interface IServerData { | ||
cep: string; | ||
state: string; | ||
city: string; | ||
neighborhood: string; | ||
street: string; | ||
} | ||
|
||
export { IResponse, IServerData }; |
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,22 +1,28 @@ | ||
import { client } from './server'; | ||
import { client } from './services/whatsapp'; | ||
import { | ||
EconomyCommand, | ||
QuoteCommand, | ||
CepCommand, | ||
ProfileCommand, | ||
SmsCommand, | ||
} from './app/commands'; | ||
import CommandDispatcher from './app/utils/CommandDispatcher'; | ||
import { commandDispatcher } from './app/utils/CommandDispatcher'; | ||
import type { Message } from 'whatsapp-web.js'; | ||
|
||
const dispatcher = new CommandDispatcher(); | ||
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); | ||
|
||
client.on('message', async (message: Message) => { | ||
dispatcher.register('cotacao', new EconomyCommand()); | ||
dispatcher.register('mencionar', new QuoteCommand()); | ||
dispatcher.register('cep', new CepCommand(message.body)); | ||
dispatcher.register('perfil', new ProfileCommand(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('!')) { | ||
dispatcher.dispatch(message.body.slice(1), message); | ||
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { mobizon } from 'mobizon-node'; | ||
|
||
mobizon.setConfig({ | ||
apiServer: process.env.MOBIZON_URL_SRV, | ||
apiKey: process.env.MOBIZON_API_KEY, | ||
format: 'json', | ||
}); | ||
|
||
export default mobizon; |
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,31 +1,31 @@ | ||
import { Client, MessageMedia } from 'whatsapp-web.js'; | ||
import qrcode from 'qrcode-terminal'; | ||
import fs from 'fs'; | ||
import dir from 'path'; | ||
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 = dir.resolve('src', 'data', 'session.json'); | ||
const sessionFile = resolve('src', 'data', 'session.json'); | ||
|
||
const session = fs.existsSync(sessionFile) ? require(sessionFile) : null; | ||
const session = existsSync(sessionFile) ? require(sessionFile) : null; | ||
|
||
const client = new Client({ | ||
puppeteer: { | ||
headless: true, | ||
args: ['--no-sandbox'], | ||
}, | ||
session, | ||
}); | ||
} as any); | ||
|
||
client.on('qr', (qr: string) => { | ||
qrcode.generate(qr, { small: true }); | ||
generate(qr, { small: true }); | ||
}); | ||
|
||
client.on('authenticated', (session: any) => { | ||
fs.writeFile(sessionFile, JSON.stringify(session), (err) => { | ||
writeFile(sessionFile, JSON.stringify(session), (err) => { | ||
if (err) console.log(err); | ||
}); | ||
}); | ||
|
@@ -35,19 +35,22 @@ client.on('ready', async () => { | |
|
||
const { pushname } = client.info; | ||
|
||
client.sendMessage('[email protected]', `[${pushname}] - WhatsApp Online`); | ||
client.sendMessage( | ||
'[email protected]', | ||
`[${pushname}] - WhatsApp Online\n\n[x] Star on project: https://github.com/caioagiani/whatsapp-bot`, | ||
); | ||
}); | ||
|
||
client.on('auth_failure', () => { | ||
fs.unlink(sessionFile, () => { | ||
unlink(sessionFile, () => { | ||
console.log('Autenticação falhou, tente novamente.'); | ||
process.exit(1); | ||
}); | ||
}); | ||
|
||
client.on('disconnected', () => { | ||
fs.unlink(sessionFile, () => | ||
console.log('Sessão WhatsApp perdeu a conexão, tente novamente.') | ||
unlink(sessionFile, () => | ||
console.log('Sessão WhatsApp perdeu a conexão, tente novamente.'), | ||
); | ||
}); | ||
|
||
|
Oops, something went wrong.