-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlunchbot.js
76 lines (65 loc) · 2.14 KB
/
lunchbot.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
var restify = require('restify')
var logger = require('morgan')
var request = require('request');
var VERIFY_TOKEN = process.env.SLACK_VERIFY_TOKEN;
var msqId = process.env.MSQ_ID;
var wsId = process.env.WS_ID;
var ebtId = process.env.EBT_ID;
if (!VERIFY_TOKEN) {
console.error('SLACK_VERIFY_TOKEN is required')
process.exit(1)
}
var PORT = process.env.PORT || 8080
var server = restify.createServer()
server.use(logger('tiny'))
server.use(restify.queryParser());
var getDay = function() {
var d = new Date();
var weekday = new Array(7);
weekday[0]= "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
console.log(weekday[d.getDay()]);
return weekday[d.getDay()];
}
var spreadSheetId = '';
var url = "https://spreadsheets.google.com/feeds/list/" + spreadSheetId + "/od6/public/values?alt=json";
var dayNameVar = 'gsx$' + getDay().toLowerCase();
var areaAmounts;
server.get('/lunch', function (req, res) {
switch (req.query.text) {
case 'ebt': areaAmounts = 6; spreadSheetId = ebtId;
break;
case 'msq': areaAmounts = 9; spreadSheetId = msqId;
break;
case 'ws': areaAmounts = 9; spreadSheetId = wsId;
}
url = "https://spreadsheets.google.com/feeds/list/" + spreadSheetId + "/od6/public/values?alt=json";
request(url, function(error, response, body) {
if(!error && response.statusCode == 200) {
var parsedBody = JSON.parse(body);
var menuReturned = "";
for (var i = 1; i < areaAmounts; i++) {
var lunchItem = parsedBody.feed.entry[i][dayNameVar].$t;
var selectionType = parsedBody.feed.entry[i].gsx$type.$t;
console.log(selectionType);
var returnMessage = getDay() + "'s " + selectionType + " is " + lunchItem;
menuReturned = menuReturned + "\n \n" + returnMessage;
}
sendMenu(menuReturned, res);
}
});
});
function sendMenu(message,res ) {
res.send({response_type: 'ephemeral', text: message})
};
server.listen(PORT, function (err) {
if (err) {
return console.error('Error starting server: ', err)
}
console.log('Server successfully started on port %s', PORT)
})