-
Notifications
You must be signed in to change notification settings - Fork 6
/
routes.js
107 lines (98 loc) · 4.02 KB
/
routes.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
// const button = require('./button')
const { click: clickCommand } = require('./click')
const helpCommand = require('./help')
const installCommand = require('./install')
const settings = require('./settings')
const { statsCommand } = require('./stats')
const usageCommand = require('./usage')
const { slackVerifyUrlencoded } = require('./slack-request-verifier')
const verifyUrlencoded = slackVerifyUrlencoded({ extended: true })
module.exports = (app, asyncEventHandler) => {
app.get('/', (req, res) => {
res.send('A wild BUTTON appears: API is ready.')
})
// Will receive /wildbutton slash command from Slack.
app.post('/commands', verifyUrlencoded, async (req, res) => {
// extract slash command text from payload
const body = req.body
const instanceRef = req.body.team_id
// create the dialog payload - includes the dialog structure, Slack API token,
// and trigger ID
console.debug('Got slash command with text:', body.text)
try {
if (body.text === 'stats') {
await statsCommand(res, instanceRef)
} else if (body.text === 'help') {
await helpCommand(res)
} else {
await usageCommand(res)
}
} catch (e) {
console.error('Handling slash command with text:', body.text, 'failed with error:', e)
res.sendStatus(500)
}
})
// Will receive response when user clicks button or interacts in some other way, e.g., settings.
app.post('/interactive', verifyUrlencoded, async (req, res) => {
try {
const payload = JSON.parse(req.body.payload)
const instanceRef = payload.team.id
const userRef = payload.user.id
if (payload.type === 'block_actions') {
for (const action of payload.actions) {
switch (action.action_id) {
case 'admin_channel':
await settings.setChannel(res, instanceRef, action, asyncEventHandler)
break
case 'admin_timezone':
await settings.setTimezone(res, instanceRef, action, asyncEventHandler)
break
case 'admin_weekdays':
await settings.setWeekdays(res, instanceRef, action, asyncEventHandler)
break
case 'admin_starttime':
await settings.setStartTime(res, instanceRef, action, asyncEventHandler)
break
case 'admin_endtime':
await settings.setEndTime(res, instanceRef, action, asyncEventHandler)
break
case 'user_stats_interval':
await settings.setUserSetting(res, instanceRef, action, userRef, 'statsInterval', asyncEventHandler)
break
case 'wild_button':
await clickCommand(res, payload, asyncEventHandler)
break
default:
console.debug(`Got unknown action_id ${action.action_id}`)
res.sendStatus(400)
break
}
}
} else {
console.debug(`Got unknown interactive response payload: ${req.body.payload}`)
res.sendStatus(400)
}
} catch (e) {
console.error(`Failed to register interactive. Got error: ${e}, in JSON: ${JSON.stringify(e)}`)
res.sendStatus(500)
}
})
// Will handle direct installation of app to workspace
app.get('/install', async (req, res) => {
const clientId = process.env.SLACK_CLIENT_ID
const scopes = 'channels:read,channels:join,groups:read,im:read,chat:write,chat:write.public,im:write,commands'
const redirectUri = process.env.SLACK_REDIRECT_URI
res.redirect(`https://slack.com/oauth/v2/authorize?client_id=${clientId}&scope=${scopes}&redirect_uri=${redirectUri}`)
})
// Will handle the OAuth 2.0 redirect after user has authorized app install.
app.get('/auth', async (req, res) => {
const { code } = req.query
try {
await installCommand(code)
res.send('App installed successfully! Visit Slack and check the message you just received from wildbutton.')
} catch (e) {
console.debug('Failed to install app, got error:', e)
res.sendStatus(400)
}
})
}