-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
53 lines (43 loc) · 1.36 KB
/
main.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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const ngrok = require('ngrok');
const { MessagingResponse } = require('twilio').twiml;
const { CronJob } = require('cron');
const { detect } = require('./detect');
const app = express();
const twiml = new MessagingResponse();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(`${__dirname}/images`));
const detectionJob = new CronJob('*/30 * * * * *', () => {
detect();
});
app.post('/incoming', (req, res) => {
switch (req.body.Body.toLowerCase()) {
case 'start':
detectionJob.start();
twiml.message('Watchdog is keeping your laptop safe!');
break;
case 'pause':
detectionJob.stop();
twiml.message('Watchdog is no longer keeping your laptop safe!');
break;
default:
twiml.message('This command is invalid.');
}
res.writeHead(200, { 'Content-Type': 'text/xml' });
res.end(twiml.toString());
});
app.post('/status', (req, res) => {
res.status(200).send('Available');
});
app.listen(process.env.SERVER_PORT);
ngrok.connect({
proto: 'http',
inspect: false,
addr: process.env.SERVER_PORT,
subdomain: process.env.NGROK_SUBDOMAIN,
authtoken: process.env.NGROK_AUTH_TOKEN,
region: process.env.NGROK_REGION,
auth: `${process.env.NGROK_USERNAME}:${process.env.NGROK_PASSWORD}`
});