-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
109 lines (95 loc) · 2.72 KB
/
app.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const accountSid = process.env.TWILIO_ACCOUNT_SID;
if (!accountSid)
throw 'TWILIO_ACCOUNT_SID env var required.'
const authToken = process.env.TWILIO_AUTH_TOKEN;
if (!authToken)
throw 'TWILIO_AUTH_TOKEN env var required.'
// twilio FROM number:
const twsender = process.env.TWILIO_SENDER;
if (!twsender)
throw 'TWILIO_SENDER env var required.'
// Destination phone number, optional here
const twreceiver = (process.env.TWILIO_RECEIVER || null);
const twclient = require('twilio')(accountSid, authToken, {
autoRetry: true,
maxRetries: 3,
});
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.type('txt');
res.send('alert2twcall up\n');
});
app.post('/call', async (req, res) => {
res.type('application/json');
const body = req.body;
let { loop, receiver } = req.query;
const gentwiml = text => `
<Response>
<Say loop="${loop}"><break time="1s" />${text}</Say>
</Response>
`;
// loop indefinitely by default
if (loop === undefined) {
loop = "0";
} else if (!/^\d+$/.test(loop)) {
return res.status(400).send({ err: 'bad loop argument' })
}
if (receiver === undefined) {
receiver = twreceiver; // use default receiver
}
if (!receiver) {
return res.status(400).send({ err: 'no receiver set' });
}
if (!/^\+?\d+(,\+\d+)*$/.test(receiver)) {
return res.status(400).send({ err: 'bad receiver ' + receiver });
}
receivers = receiver.split(',');
if (!req.body || !req.body.status || !req.body.alerts)
return res.status(400).send({ err: 'bad body' });
let message = req.body.alerts
.filter(a => (a.annotations || {}).summary)
.map(a => a.annotations.summary)
.join('. ');
let twiml = gentwiml(message);
let ret = [];
let status = 200;
for (r of receivers) {
try {
const call = await twclient.calls.create({
to: r,
from: twsender,
twiml
})
ret.push({ sid: call.sid });
} catch (err) {
status = 400;
console.log('ERROR', err)
ret.push({ err });
}
}
return res.status(status).send(ret);
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;