forked from navikt/helsesjekk-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands-handler.ts
164 lines (144 loc) · 6.45 KB
/
commands-handler.ts
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
155
156
157
158
159
160
161
162
163
164
import { App } from '../app'
import { createSettingsModal } from '../events/settings/settings-modal-builder'
import { postToTeam, remindTeam, revealTeam, updateResponseCount } from '../messages/message-poster'
import { createTeam, getPreviousAsk, getTeam, hasActiveUnnaggedAsk, prisma, updateTeamGroupAssociation } from '../../db'
import { scoreAsked } from '../../metrics/metrics'
import { createScoreBlocks } from '../messages/message-builder'
import { botLogger } from '../bot-logger'
export function configureCommandsHandler(app: App): void {
// Handles the /helsesjekk command, it opens the settings modal
app.command(/(.*)/, async ({ command, ack, client, respond }) => {
botLogger.info(`User used /helsesjekk command`)
if (command.text.trim().startsWith('assign')) {
const groupId = command.text.replace('assign', '').trim()
if (groupId.length === 0) {
await ack()
await respond({
text: 'Du må skrive inn en ad-gruppe etter kommandoen. :bonk:',
})
return
}
botLogger.info(`User wants to connect ${command.channel_id} to ${groupId}`)
await ack()
await updateTeamGroupAssociation(command.channel_id, groupId)
await respond({
text: `Denne kanalen har blitt koblet til ad-gruppe "${groupId}"`,
})
return
}
const isBotInChannel = await isBotAddedToChannel(command.channel_id, client)
if (isBotInChannel !== true) {
botLogger.warn(
`Someone used /helsesjekk in a DM or a channel where it hasn't been added. Type: ${isBotInChannel} Channel ID: ${command.channel_id}`,
)
await ack()
await respond({
text: 'Ser ut som du prøver å ta i bruk helsesjekk i en kanal hvor jeg ikke er lagt til. :meow_shocked: Kan du legge meg til som en integrasjon først? :smile:',
})
return
}
await ack()
const team = (await getTeam(command.channel_id)) ?? (await createTeam(command.channel_id, '[Ditt Team]'))
await client.views.open({
trigger_id: command.trigger_id,
view: createSettingsModal(team),
})
})
// TODO inn i settings slash command?
app.event('app_mention', async ({ event, say }) => {
botLogger.info(`User mentioned the bot in ${process.env.NODE_ENV}`)
try {
if (process.env.NODE_ENV !== 'production') {
// dev tool for forcing the bot to post questionnaire
if (event.text.endsWith('post')) {
const team = await getTeam(event.channel)
if (team != null) {
await postToTeam(team, app.client)
}
}
// dev tool for forcing the bot to reveal the answers
if (event.text.endsWith('lock')) {
const team = await getTeam(event.channel)
if (team != null) {
await revealTeam(team, app.client)
}
}
// dev tool for forcing the bot to reveal the answers
if (event.text.endsWith('unlock')) {
const team = await getTeam(event.channel)
if (team != null) {
const toUpdate = await prisma().asked.findFirst({
where: { teamId: team.id },
orderBy: { timestamp: 'desc' },
})
if (!toUpdate) return
await prisma().asked.update({
data: { revealed: false },
where: { id: toUpdate.id },
})
botLogger.info(`Unlocked ${team.name} (${team.id})`)
await updateResponseCount(team, app.client)
}
}
if (event.text.endsWith('remind')) {
const team = await getTeam(event.channel)
if (team != null) {
botLogger.info(`Would team have been nagged? ${await hasActiveUnnaggedAsk(team.id)}`)
await remindTeam(team, app.client)
}
}
if (event.text.endsWith('debug')) {
const team = await getTeam(event.channel)
if (team == null) {
return
}
const ask = await prisma().asked.findFirst({
where: { teamId: team.id },
orderBy: { timestamp: 'desc' },
include: { answers: true },
})
if (ask == null) {
return
}
const previousAsked = await getPreviousAsk(ask)
const scoredAsk = scoreAsked(ask)
const previousScoredAsk = previousAsked ? scoreAsked(previousAsked) : null
await app.client.chat.postMessage({
channel: team.id,
text: `Svar på ukentlig helsesjekk for ${team.name}`,
blocks: createScoreBlocks(team, ask, scoredAsk, previousScoredAsk),
reply_broadcast: true,
})
}
}
} catch (e) {
botLogger.error(e)
await say('Oi! Noe gikk galt i botten. :( Dersom det skjer igjen, ta kontakt i #helsesjekk-bot.')
}
})
}
async function isBotAddedToChannel(
channel: string,
client: App['client'],
): Promise<true | 'not_in_private' | 'not_in_public' | 'unknown'> {
try {
const channelInfo = await client.conversations.info({
channel: channel,
})
if (channelInfo.ok && channelInfo.channel && !channelInfo.channel.is_member) {
return 'not_in_public'
}
if (!channelInfo.ok) {
botLogger.info(
`Unable to get channel info, integration not in channel, or is a DM?: ${
channelInfo.error ?? 'No error'
}`,
)
return 'not_in_private'
}
return true
} catch (e) {
botLogger.error(new Error("Couldn't get channel info", { cause: e }))
return 'unknown'
}
}