Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (44 loc) · 1.45 KB
/
Copy pathapp.js
File metadata and controls
53 lines (44 loc) · 1.45 KB
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
const twilio = require('twilio');
const express = require('express');
const bodyParser = require('body-parser');
let config;
try {
config = require('./config/config.json');
} catch (ex) {
console.error('Failed to load config/config.json!');
console.error('Make sure the file exists.');
console.error('If you need help, check out the config.example.json file.');
process.exit(1);
}
const plugins = require('./plugins/index.js');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.post(config.twilio.webhook_path, function(request, response) {
const twilioSignature = request.header('X-Twilio-Signature');
const validTwilioRequest = twilio.validateRequest(
config.twilio.authToken,
twilioSignature,
config.twilio.webhookUrl,
request.body
);
if (validTwilioRequest) {
response.set('Content-Type', 'text/xml');
if (!config.twilio.allowedNumbers.includes(request.body.From)) {
console.log(
`Received command from disallowed number ${
request.From
}. Not responding.`
);
const twiml = new MessagingResponse();
response.send(twiml.toString());
return;
}
plugins.handle(request.body, response);
} else {
console.log('Received a potentially spoofed request - dropping silently.');
response.sendStatus(403);
}
});
app.listen(config.express.port, function() {
console.log(`TextEverything listening on port ${config.express.port}.`);
});