This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
95 lines (81 loc) · 1.71 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
const express = require('express')
const app = express()
const lunch = require('./lunch.js')
const bodyParser = require('body-parser')
const request = require('request')
const weekdayMapping = {
'monday': 1,
'tuesday': 2,
'wednesday': 3,
'thursday': 4,
'friday': 5
}
app.use(bodyParser.urlencoded({ extended: false }))
app.get('/', (req, res) => {
lunch.today()
.then(lunch => {
res.send(lunch)
})
.catch(err => {
console.log("Error: " + err)
res.status(500).send(err)
})
})
app.get('/:weekday', (req, res) => {
const weekday = req.params['weekday']
const weekdayNumber = weekdayMapping[weekday.toLowerCase()]
if (!weekdayNumber) {
console.log("Unknown weekday: " + weekday)
res.status(500)
return
}
lunch.forDay(weekdayNumber)
.then(lunch => {
res.send(lunch)
})
.catch(err => {
console.log("Error: " + err)
res.status(500).send(err)
})
})
app.set('port', process.env.PORT || 3000)
app.listen(app.get('port'), () => {
console.log("Running on port " + app.get('port'))
})
app.post('/slack/slash/lunch', (req, res) => {
if (req.body['token'] != process.env.SLACK_SLASH_LUNCH) {
res.send()
return
}
const handler = lunch => {
request({
method: 'POST',
uri: req.body['response_url'],
json: {
response_type: "ephemeral",
text: lunch.main,
attachments: [{
text: lunch.side
}]
}
})
}
const errorHandler = error => {
console.log("Error: " + err)
return
}
const text = req.body['text']
if (text) {
const weekdayNumber = weekdayMapping[text.toLowerCase()]
if (weekdayNumber) {
lunch.forDay(weekdayNumber)
.then(handler)
.catch(errorHandler)
}
} else {
lunch.today()
.then(handler)
.catch(errorHandler)
}
res.send()
})