-
Notifications
You must be signed in to change notification settings - Fork 1
/
smsApp.js
56 lines (46 loc) · 1.46 KB
/
smsApp.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
var express = require('express');
var twilio = require('twilio');
var query = require('./lib/query.js').query;
var app = express();
/*
* Format a response into TwiML to be understood by Twilio
*/
var wrapResponse = function(data) {
var resp = new twilio.TwimlResponse();
resp.message(data);
return resp.toString();
}
/*
* Debug endpoint
*/
app.get('/up', function (req, res) {
res.send('I am up!');
});
/*
* The primary endpoint for SMS hooks. Request should send a "Body" param, which we
* will use to query for results.
*/
app.get('/respond', function (req, res) {
var body = req.query.Body;
var response = '';
// TODO: sanitize the body before querying
query({'query': body}, function (result) {
var restaurants = result.results;
if (result.error) {
res.status(500).send('Something broke back here. Maybe try another search.');
} else if (restaurants.restaurants.length == 0) {
response = '0 restaurants found. Try a simpler search like "Sakura" instead of "Sakura Sushi"';
} else {
var lastGraded = restaurants.restaurants[0].lastGradedInspection();
if (lastGraded) {
response = lastGraded.toString();
} else {
response = 'This restaurant has not yet been graded. Visit the website for more info.';
}
}
res.send(wrapResponse(response));
});
});
module.exports = {
'app': app
};