Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suporte ao DirectCall #25

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ Você terá disponível globalmente o comando `gemidao-do-zap`.

| Parâmetro | Obrigatório | Descrição |
|-----------|--------------------|-----------------------------------------------------------|
| `--token` | :white_check_mark: | Seu token de acesso do TotalVoice |
| `--token` | :white_check_mark: | Seu token de acesso do TotalVoice ou DirectCall |
| `--de` | | Quem está enviando o gemidão? Qualquer número telefônico! |
| `--para` | :white_check_mark: | Quem é a vítima do gemidão do zap? |
| `--sms` | | Se definido, será enviado um SMS ao invés de uma chamada |
| `--api`   | :white_check_mark: | Escolha entre TotalVoice ou DirectCall |

### Exemplo

`gemidao-do-zap --de=47998569631 --para=47996326548 --token=ade6a19ecee14577634f66af105eb68c`
`gemidao-do-zap --de=47998569631 --para=47996326548 --api=TotalVoice --token=ade6a19ecee14577634f66af105eb68c`

Observações:

Expand Down
7 changes: 6 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const emitError = message => console.log(red(` ✗ Erro: ${message}`));

function cli(args) {
gemidao(args)
.then(() => {
.then(res => {
console.log(res);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

De repente tirar esse console.log? rsrs

Morgan ou winston seria uma boa, não?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Como falei, esse código não estava pronto, o PR foi mais de fix do anterior. Deixei o log mais para deixar claro onde pegar o resultado da requisição. Além disso para um possivel PR ter mensagens de erro de acordo com a resposta da API

emitSuccess(args.sms ? 'sms enviado!' : 'chamada efetuada!');
})
.catch(pipe(prop('message'), emitError));
Expand All @@ -34,6 +35,10 @@ cli(yargs
describe: 'Se definido, será enviado um SMS ao invés de uma chamada',
type: 'boolean'
})
.option('api', {
describe: 'Escolha entre TotalVoice ou DirectCall (Padrão:TotalVoice)',
type: 'string'
})
.demandOption(['para', 'token'])
.locale('pt_BR')
.strict()
Expand Down
58 changes: 46 additions & 12 deletions src/gemidao.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import agent from 'superagent';
import promisifyAgent from 'superagent-promise';

const request = promisifyAgent(agent, Bluebird);
const route = path => `https://api.totalvoice.com.br${path}`;
const routeDirectCall = pathDirectCall => `https://api.directcallsoft.com${pathDirectCall}`;
const routeTotalVoice = pathTotalVoice => `https://api.totalvoice.com.br${pathTotalVoice}`;

const gemidaoInText = 'OOOWH AHHHWN WOOOO AAAAHN WAAAAA AAAAAAHN ANN WAAA!\n'
+ 'Voce caiu no gemidao do zap';

const sms = (to, token) => request.post(route('/sms'))
// TotalVoice
const smsTotalVoice = (to, token) => request.post(routeTotalVoice('/sms'))
.set('Access-Token', token)
.set('Accept', 'application/json')
.send({ numero_destino: to, mensagem: gemidaoInText });

const call = (from, to, token) => request.post(route('/composto'))
const callTotalVoice = (from, to, token) => request.post(routeTotalVoice('/composto'))
.set('Access-Token', token)
.set('Accept', 'application/json')
.send({
Expand All @@ -29,25 +31,57 @@ const call = (from, to, token) => request.post(route('/composto'))
bina: from
});

export default function gemidao(args) {
if (!/^[a-f0-9]{32}$/.test(args.token)) {
return reject(new Error('Token inválido. Obtenha um em https://totalvoice.com.br'));
}
// DirectCall
const smsDirectCall = (from, to, token) => request.post(routeDirectCall('/sms/send'))
.set('Accept', 'application/json')
.send({
origem: from,
access_token: token,
destino: to,
texto: gemidaoInText
});

const callDirectCall = (from, to, token) => request.post(routeDirectCall('/sms/audio'))
.set('Accept', 'application/json')
.send({
access_token: token,
audio: 'https://github.com/haskellcamargo/gemidao-do-zap/raw/master/resources/gemidao.mp3'
});

// Comum
export default function gemidao(args) {
if (!/^[0-9]{10,11}$/.test(args.para)) {
return reject(new Error('Número de telefone inválido'));
}

const action = args.sms
? sms(args.para, args.token)
: call(args.de, args.para, args.token);

const action
= (() => {
if (args.api.length === 0 || args.api === 'TotalVoice') {
if (!/^[a-f0-9]{32}$/.test(args.token)) {
return reject(new Error('Token inválido. Obtenha um em https://totalvoice.com.br'));
}
return args.sms
? smsTotalVoice(args.para, args.token)
: callTotalVoice(args.de, args.para, args.token);
} else if (args.api === 'DirectCall') {
// O token da DirectCall possui mais de 250 carecteres
// e nao tem tamanho definido, além de usar simbolos
//
// if (!/^[a-f0-9]$/.test(args.token)) {
// console.log(args.token);
// return reject(new Error('Token inválido. Obtenha um em https://www.directcallsoft.com'));
// }
return args.sms
? smsDirectCall(args.de, args.para, args.token)
: callDirectCall(args.de, args.para, args.token);
}
return reject(new Error('Escolha uma das APIs Suportadas: DirectCall ou TotalVoice.'));
})();
return action
.catch(err => {
if (err.status === 405 || err.status === 403) {
return reject(new Error((err.body || err.response.body).mensagem));
}

return reject(err);
});
}