forked from haskellcamargo/gemidao-do-zap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemidao.js
53 lines (44 loc) · 1.63 KB
/
gemidao.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import Bluebird, { reject } from 'bluebird';
import agent from 'superagent';
import promisifyAgent from 'superagent-promise';
const request = promisifyAgent(agent, Bluebird);
const route = path => `https://api.totalvoice.com.br${path}`;
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'))
.set('Access-Token', token)
.set('Accept', 'application/json')
.send({ numero_destino: to, mensagem: gemidaoInText });
const call = (from, to, token) => request.post(route('/composto'))
.set('Access-Token', token)
.set('Accept', 'application/json')
.send({
numero_destino: to,
dados: [
{
acao: 'audio',
acao_dados: {
url_audio: 'https://github.com/haskellcamargo/gemidao-do-zap/raw/master/resources/gemidao.mp3'
}
}
],
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'));
}
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);
return action
.catch(err => {
if (err.status === 405 || err.status === 403) {
return reject(new Error((err.body || err.response.body).mensagem));
}
return reject(err);
});
}