-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
108 lines (88 loc) · 2.78 KB
/
index.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
'use strict'
// 3rd party library imports
var express = require('express')
var body_parser = require('body-parser')
var rollbar = require("rollbar")
var request = require("request")
// project imports
var text = require('./message_text.json')
var cron_job = require('./cron_job').job
var db = require('./db')
var ZIPCODES = require('./zipcodes')
// Slack Webhook Token
var SLACK_WEBHOOK = process.env.SLACK_WEBHOOK
var app = express() // instantiate express
// serve files from the public dir for testing via web
app.get('/', express.static(__dirname + '/public'))
// parse POST bodies
app.use(body_parser.urlencoded({ extended: true }))
// Twilio hits this endpoint
app.post('/', function(req, res, next) {
var message = req.body.Body
var phone_number = req.body.From
var zipcode_regex = /^\d{5}(?:[-\s]\d{4})?$/
var zip = null
// this is necessary
res.set('Content-Type', 'text/plain');
var match = message.match(zipcode_regex)
if (match) zip = match[0]
var is_subscriber = db('subscribers').find(function(item) {
return item.phone == phone_number
})
// if they sent a zipcode
if (zip) {
// if they are a subscriber, delete any existing zips first
if (is_subscriber) {
db('subscribers').remove(function(item) {
return item.phone == phone_number
})
}
if (ZIPCODES.indexOf(zip) > -1) {
db('subscribers').push({
phone: phone_number,
zip: zip,
})
return res.send(text.CONFIRMATION)
}
else {
return res.send(text.BAD_ZIP)
}
}
// handle unsubscriptions
else if (is_subscriber && message.toLowerCase().trim() === 'stop') {
db('subscribers').remove(function(item) {
return item.phone == phone_number
})
return res.send(text.GOODBYE)
}
// if we know this number, what the hell are they trying to tell us?
else if (is_subscriber) {
// log the message, maybe it's interesting
db('unknown_commands').push({
phone: phone_number,
message: message,
})
request.post(SLACK_WEBHOOK).form(JSON.stringify({
"username":"citizen",
"text":message
}))
return res.send(text.INSTRUCTIONS)
}
// else just say Hello
else {
return res.send(text.WELCOME)
}
});
// error handlers
app.use(rollbar.errorHandler(process.env.ROLLBAR_TOKEN));
rollbar.handleUncaughtExceptionsAndRejections(
process.env.ROLLBAR_TOKEN,
{exitOnUncaughtException: true}
);
// start the server
var port = process.env.PORT || 3000
app.listen(port, function () {
console.log('plug-at-20 app running on port', port);
});
// start the cron job
cron_job.start()