-
Notifications
You must be signed in to change notification settings - Fork 6
/
events.js
38 lines (34 loc) · 1.34 KB
/
events.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
const { createEventAdapter } = require('@slack/events-api')
module.exports = (app, asyncEventHandler) => {
const slackSigningSecret = process.env.SLACK_SIGNING_SECRET
const slackEvents = createEventAdapter(slackSigningSecret, { includeBody: true, waitForResponse: true })
app.use('/events', (req, res, next) => {
// We must store the raw request body in rawBody, since createEventAdapter doesn't like
// when body is a Buffer (it expects undefined).
req.rawBody = req.body
next()
})
app.use('/events', slackEvents.requestListener())
slackEvents.on('app_home_opened', async (event, body, respond) => {
try {
if (event.tab === 'home') {
// Launch async event to populate this view.
await asyncEventHandler({
method: 'home',
instanceRef: body.team_id,
user: event.user
})
}
respond()
} catch (err) {
console.error(`Failed to launch async event handler. Error: ${err}, in JSON: ${JSON.stringify(err)}`)
respond({ status: 500 })
}
})
slackEvents.on('app_uninstalled', (event, body, respond) => {
// TODO: delete all data from database here, or at least disable things
// for example by setting announce channel to null so it doesn't try to post.
console.warn('App uninstalled event received, but not handled')
respond()
})
}