forked from CivicTechAtlanta/courtbot
-
Notifications
You must be signed in to change notification settings - Fork 10
/
web.js
301 lines (266 loc) · 9.33 KB
/
web.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* eslint "no-console": "off" */
require('dotenv').config();
const MessagingResponse = require('twilio').twiml.MessagingResponse;
const express = require('express');
const cookieSession = require('cookie-session')
const bodyParser = require('body-parser')
const db = require('./db');
const emojiStrip = require('emoji-strip');
const messages = require('./utils/messages.js');
const moment = require("moment-timezone");
const onHeaders = require('on-headers');
const log = require('./utils/logger')
const web_log = require('./utils/logger/hit_log')
const web_api = require('./web_api/routes');
const action_symbol = Symbol.for('action');
const app = express();
/* Express Middleware */
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cookieSession({
name: 'session',
secret: process.env.COOKIE_SECRET,
signed: false, // causing problems with twilio -- investigating
}));
/* makes json print nicer for /cases */
app.set('json spaces', 2);
/* Serve testing page on which you can impersonate Twilio (but not in production) */
if (app.settings.env === 'development' || app.settings.env === 'test') {
app.use(express.static('public'));
}
/* Allows CORS */
app.all('*', (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Authorization, Content-Type');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,OPTIONS');
onHeaders(res, web_log)
next();
});
/* Enable CORS support for IE8. */
app.get('/proxy.html', (req, res) => {
res.send('<!DOCTYPE HTML>\n<script src="http://jpillora.com/xdomain/dist/0.6/xdomain.min.js" master="http://www.courtrecords.alaska.gov"></script>');
});
app.get('/', (req, res) => {
res.status(200).send(messages.iAmCourtBot());
});
/* Add routes for api access */
app.use('/api', web_api);
/* Fuzzy search that returns cases with a partial name match or
an exact citation match
*/
app.get('/cases', (req, res, next) => {
if (!req.query || !req.query.q) {
return res.sendStatus(400);
}
return db.fuzzySearch(req.query.q)
.then((data) => {
if (data) {
data.forEach((d) => {
d.readableDate = moment(d.date).format('dddd, MMM Do'); /* eslint "no-param-reassign": "off" */
});
}
return res.json(data);
})
.catch(err => next(err));
});
/**
* Twilio Hook for incoming text messages
*/
app.post('/sms',
cleanupTextMiddelWare,
stopMiddleware,
deleteMiddleware,
yesNoMiddleware,
currentRequestMiddleware,
caseIdMiddleware,
unservicableRequest
);
/* Middleware functions */
/**
* Strips line feeds, returns, and emojis from string and trims it
*
* @param {String} text incoming message to evaluate
* @return {String} cleaned up string
*/
function cleanupTextMiddelWare(req,res, next) {
let text = req.body.Body.replace(/[\r\n|\n].*/g, '');
req.body.Body = emojiStrip(text).trim().toUpperCase();
next()
}
/**
* Checks for 'STOP' text. We will recieve this if the user requests that twilio stop sending texts
* All further attempts to send a text (inlcuding responing to this text) will fail until the user restores this.
* This will delete any requests the user currently has (alternatively we could mark them inactive and reactiveate if they restart)
*/
function stopMiddleware(req, res, next){
const stop_words = ['STOP', 'STOPALL', 'UNSUBSCRIBE', 'CANCEL', 'END','QUIT']
const text = req.body.Body
if (!stop_words.includes(text)) return next()
db.deactivateRequestsFor(req.body.From)
.then(case_ids => {
res[action_symbol] = "stop"
return res.sendStatus(200); // once stopped replies don't make it to the user
})
.catch(err => next(err));
}
/**
* Handles cases when user has send a yes or no text.
*/
function yesNoMiddleware(req, res, next) {
// Yes or No resonses are only meaningful if we also know the citation ID.
if (!req.session.case_id) return next()
const twiml = new MessagingResponse();
if (isResponseYes(req.body.Body)) {
db.addRequest({
case_id: req.session.case_id,
phone: req.body.From,
known_case: req.session.known_case
})
.then(() => {
twiml.message(req.session.known_case ? messages.weWillRemindYou() : messages.weWillKeepLooking() );
res[action_symbol] = req.session.known_case? "schedule_reminder" : "schedule_unmatched"
req.session = null;
req.session = null;
res.send(twiml.toString());
})
.catch(err => next(err));
} else if (isResponseNo(req.body.Body)) {
res[action_symbol] = "decline_reminder"
twiml.message(req.session.known_case ? messages.repliedNo(): messages.repliedNoToKeepChecking());
req.session = null;
res.send(twiml.toString());
} else{
next()
}
}
/**
* Handles cases where user has entered a case they are already subscribed to
* and then type Delete
*/
function deleteMiddleware(req, res, next) {
// Delete response is only meaningful if we have a delete_case_id.
const case_id = req.session.delete_case_id
const phone = req.body.From
if (!case_id || req.body.Body !== "DELETE") return next()
res[action_symbol] = "delete_request"
const twiml = new MessagingResponse();
db.deactivateRequest(case_id, phone)
.then(() => {
req.session = null;
twiml.message(messages.weWillStopSending(case_id))
res.send(twiml.toString());
})
.catch(err => next(err));
}
/**
* Responds if the sending phone number is alreay subscribed to this case_id=
*/
function currentRequestMiddleware(req, res, next) {
const text = req.body.Body
const phone = req.body.From
if (!possibleCaseID(text)) return next()
db.findRequest(text, phone)
.then(results => {
if (!results || results.length === 0) return next()
const twiml = new MessagingResponse();
// looks like they're already subscribed
res[action_symbol] = "already_subscribed"
req.session.delete_case_id = text
twiml.message(messages.alreadySubscribed(text))
res.send(twiml.toString());
})
.catch(err => next(err));
}
/**
* If input looks like a case number handle it
*/
function caseIdMiddleware(req, res, next){
const text = req.body.Body
if (!possibleCaseID(text)) return next()
const twiml = new MessagingResponse();
db.findCitation(req.body.Body)
.then(results => {
if (!results || results.length === 0){
// Looks like it could be a citation that we don't know about yet
res[action_symbol] = "unmatched_case"
twiml.message(messages.notFoundAskToKeepLooking());
req.session.known_case = false;
req.session.case_id = text;
} else {
// They sent a known citation!
res[action_symbol] = "found_case"
twiml.message(messages.foundItAskForReminder(results[0]));
req.session.case_id = text;
req.session.known_case = true;
}
res.send(twiml.toString());
})
.catch(err => next(err));
}
/**
* None of our middleware could figure out what to do with the input
* [TODO: create a better message to help users use the service]
*/
function unservicableRequest(req, res, next){
// this would be a good place for some instructions to the user
res[action_symbol] = "unusable_input"
const twiml = new MessagingResponse();
twiml.message(messages.invalidCaseNumber());
res.send(twiml.toString());
}
/* Utility helper functions */
/**
* Test message to see if it looks like a case id.
* Currently alphan-numeric plus '-' between 6 and 25 characters
* @param {String} text
*/
function possibleCaseID(text) {
/* From AK Court System:
- A citation must start with an alpha letter (A-Z) and followed
by only alpha (A-Z) and numeric (0-9) letters with a length of 8-17.
- Case number must start with a number (1-4)
and have a length of 14 exactly with dashes.
*/
const citation_rx = /^[A-Za-z][A-Za-z0-9]{7,16}$/
const case_rx = /^[1-4][A-Za-z0-9-]{13}$/
return case_rx.test(text) || citation_rx.test(text);
}
/**
* Checks for an affirmative response
*
* @param {String} text incoming message to evaluate
* @return {Boolean} true if the message is an affirmative response
*/
function isResponseYes(text) {
return (text === 'YES' || text === 'YEA' || text === 'YUP' || text === 'Y');
}
/**
* Checks for negative or declined response
*
* @param {String} text incoming message to evaluate
* @return {Boolean} true if the message is a negative response
*/
function isResponseNo(text) {
return (text === 'NO' || text === 'N');
}
/* Error handling Middleware */
app.use((err, req, res, next) => {
if (!res.headersSent) {
log.error(err);
// during development, return the trace to the client for helpfulness
if (app.settings.env !== 'production') {
res.status(500).send(err.stack);
return;
}
res.status(500).send('Sorry, internal server error');
}
});
/* Send all uncaught exceptions to Rollbar??? */
const options = {
exitOnUncaughtException: true,
};
const port = Number(process.env.PORT || 5000);
app.listen(port, () => {
log.info(`Listening on port ${port}`);
});
module.exports = app;