-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (137 loc) · 5.13 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require('dotenv').config()
const { WebClient } = require('@slack/web-api')
const { createEventAdapter } = require('@slack/events-api')
const { createMessageAdapter } = require('@slack/interactive-messages')
const express = require('express')
const bodyParser = require('body-parser')
const wc = new WebClient(process.env.SLACK_OAUTH_TOKEN)
const slackEvents = createEventAdapter(process.env.SLACK_SIGNING_SECRET)
const slackInteractions = createMessageAdapter(process.env.SLACK_SIGNING_SECRET)
const app = express()
app.use('/slack/events', slackEvents.expressMiddleware())
app.use('/slack/interactions', slackInteractions.expressMiddleware())
app.use(bodyParser.urlencoded({ extended: true }))
//pull in blocks from blocks files
const blocks = require('./blocks.js')
const utils = require('./utils.js')
//regexes
const checkChannelsRegex = /checkOldChannels/
const checkOnlineRegex = /checkIfOnline/
/**
* Listens for POST from slack slash command
*/
app.post('/slack/archive-init', async (req, res) => {
//use body-parser to grab userid and triggerid
let { user_id, trigger_id } = await req.body
console.log(req.body)
console.log(
`User ${user_id} opened the slash command modal with trigger ID ${trigger_id}`
)
/*if (!utils.checkSigningSecret(req)) {
//checks request to see that the HMAC is correct
res.status(401).send('Unauthorized')
throw 'UnauthedAttempt'
}*/
//send an immediate 200 to slack so they don't complain about timeouts
await res.status(200).send('')
//opens a modal with the requestInit blocks - asking if they want to archive or unarchive
await wc.views.open({
token: process.env.SLACK_OAUTH_TOKEN,
trigger_id: trigger_id,
view: blocks.requestInit(user_id)
})
})
/**
* Detects on any new slack message that it can see
*/
slackEvents.on('message', async event => {
//check if subtype deleted or changed
if (event.hasOwnProperty('subtype')) {
const t = event.subtype
if (t === 'message_edited' || t === 'message_deleted') {
return
}
}
//to check if the bot's online
if (event.text.match(checkOnlineRegex)) {
await utils.react(event.channel, 'heavy_check_mark', event.ts) //react with a thumbs up to confirm that the bot's online and alive
//the check for outdated channels one
} else if (event.text.match(checkChannelsRegex)) {
console.log('Checking for outdated channels!')
await utils.react(event.channel, 'beachball', event.ts)
const outdatedChannels = await utils.getOldChannels(7776000) // 7776000 seconds = 3 months
for await (const i of outdatedChannels) {
await utils.renameDeadChannel(i) //renames channel with zzz-
await utils.chat(
process.env.SLACK_ADMIN_CHANNEL,
`Archived <#${i}> due to inactivity`
)
//exlains the archive
await utils.chat(
i,
`<#${i}> has been archived due to inactivity. If you'd like to unarchive it, run the command \`/unarchive\`.`
)
await utils.archiveChannel(i)
}
console.log('archive job done')
await utils.unreact(event.channel, 'beachball', event.ts)
await utils.react(event.channel, 'heavy_check_mark', event.ts)
}
})
/*
* If the user requests an archive in the init menu
*/
slackInteractions.action({ actionId: 'request_archive' }, async payload => {
console.log(payload)
console.log('user requested archive of a channel, sending to new payload')
await wc.views.push({
trigger_id: payload.trigger_id,
view: blocks.requestArchive()
})
})
/*
* If the user requests an unarchive in the init menu
*/
slackInteractions.action({ actionId: 'request_unarchive' }, async payload => {
console.log('user requested a channel unarchive')
//push unarchive payload
await wc.views.push({
trigger_id: payload.trigger_id,
view: blocks.requestUnarchive()
})
})
/*
* Checks for a view/submission of multiple types - currently just unarchive but will be more later
*/
slackInteractions.viewSubmission(
{ type: 'view_submission' },
async (payload, respond) => {
const block_id = await Object.keys(payload.view.state.values)[0] //dumb code for dumb api
switch (block_id) {
case 'archive_channel_select_block': {
//do stuff - we have to figure out implemetation here before we can write the code
throw 'NotYetImplemented'
}
case 'unarchive_channel_input_block': {
const inputChannel = await payload.view.state.values[
block_id
].unarchive_channel_input_action.value.replace('#', '') //removes "#" from the channel selector, if people put it in
const channelId = await utils.getChannelId(inputChannel)
await utils.unarchiveChannel(await channelId) //TODO - check if channel exists before unarchiving
await utils.chat(
channelId,
`<@${payload.user.id}> unarchived this channel! Welcome back, everyone :wave:`
)
return {
response_action: 'clear' //after unarchiving it will close the modal
}
}
default: {
return { response_action: 'clear' } //if the user submits with nothing in, the modal closes
}
}
}
)
app.listen(process.env.PORT || 3000, () => {
console.log('listening')
})