This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.ts
149 lines (129 loc) · 5.18 KB
/
events.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
import { BlockAction, ButtonAction } from '@slack/bolt';
import type { NextApiRequest, NextApiResponse } from 'next';
import { getParticipantAirtableLink } from '../../../lib/airtableLink';
import { getGlobalSettings } from '../../../lib/api/globalSettings';
import { apiRoute } from '../../../lib/api/apiRoute';
import db, {
installationsTable, meetingFeedbacksTable, meetingsTable, participantsTableFor,
} from '../../../lib/api/db';
import {
acknowledgeSlackButton, ACTION_IDS, getSlackData, makeMessage,
} from '../../../lib/api/slack';
import { now } from '../../../lib/timestamp';
import { app, appRunner } from './_runner';
export const config = {
api: {
bodyParser: false,
},
};
export default apiRoute(async (
req: NextApiRequest,
res: NextApiResponse,
) => {
if (req.method !== 'POST') {
res.status(405).json({ error: 'Method not allowed' });
return;
}
await appRunner.handleEvents(req, res);
}, 'insecure_no_auth');
app.action<BlockAction<ButtonAction>>(
ACTION_IDS.CONFIRM_MEETING_BUTTON,
async (args) => {
const meeting = await db.update(meetingsTable, {
id: args.action.value,
lastModifiedAt: now(),
state: 'CONFIRMED',
});
const text = (await getGlobalSettings()).confirmMeetingGroupMessage;
await args.say({
text,
blocks: makeMessage(text, [
{ text: ':raised_hands: We met!', id: ACTION_IDS.COMPLETE_MEETING_BUTTON, value: meeting.id },
{ text: ':x: We cancelled our meeting', id: ACTION_IDS.DECLINE_MEETING_BUTTON, value: meeting.id },
]),
});
await acknowledgeSlackButton(args);
},
);
app.action<BlockAction<ButtonAction>>(
ACTION_IDS.COMPLETE_MEETING_BUTTON,
async (args) => {
const meeting = await db.update(meetingsTable, {
id: args.action.value,
lastModifiedAt: now(),
state: 'COMPLETED',
});
const groupMessage = (await getGlobalSettings()).completeMeetingGroupMessage;
await args.say({ text: groupMessage, blocks: makeMessage(groupMessage) });
const installation = await db.get(installationsTable, meeting.installationId);
const participantsTable = participantsTableFor(installation);
const { members } = await args.client.users.list();
if (members === undefined) {
throw new Error(`Failed to get Slack members for installation ${installation.id}`);
}
const participantIds: string[] = JSON.parse(meeting.participantIdsJson);
const participants = await Promise.all(participantIds.map(async (participantId) => {
const participant = await db.get(participantsTable, participantId);
return {
...participant,
...getSlackData(participant, members),
};
}));
await Promise.all(participants.map(async (participant) => {
const individualMessage = (await getGlobalSettings())
.completeMeetingIndividualMessage
.replaceAll(
'{{otherParticipants}}',
participants.filter((p) => p !== participant).map((p) => `<@${p.slackId}>`).join(' and '),
);
return args.client.chat.postMessage({
channel: participant.slackId,
text: individualMessage,
blocks: makeMessage(individualMessage, [
{ text: ':one: Not at all', id: ACTION_IDS.RATE_MEETING_BUTTON_1, value: JSON.stringify([meeting.id, participant.id, 1]) },
{ text: ':two: Slightly', id: ACTION_IDS.RATE_MEETING_BUTTON_2, value: JSON.stringify([meeting.id, participant.id, 2]) },
{ text: ':three: Moderately', id: ACTION_IDS.RATE_MEETING_BUTTON_3, value: JSON.stringify([meeting.id, participant.id, 3]) },
{ text: ':four: Very', id: ACTION_IDS.RATE_MEETING_BUTTON_4, value: JSON.stringify([meeting.id, participant.id, 4]) },
{ text: ':five: Extremely', id: ACTION_IDS.RATE_MEETING_BUTTON_5, value: JSON.stringify([meeting.id, participant.id, 5]) },
]),
});
}));
await acknowledgeSlackButton(args);
},
);
app.action<BlockAction<ButtonAction>>(
ACTION_IDS.DECLINE_MEETING_BUTTON,
async (args) => {
const meeting = await db.update(meetingsTable, {
id: args.action.value,
lastModifiedAt: now(),
state: 'DECLINED',
});
const text = (await getGlobalSettings()).declineMeetingGroupMessage;
await args.say({
text,
blocks: makeMessage(text, [
{ text: ':leftwards_arrow_with_hook: Undo, we are going to meet!', id: ACTION_IDS.CONFIRM_MEETING_BUTTON, value: meeting.id },
]),
});
await acknowledgeSlackButton(args);
},
);
app.action<BlockAction<ButtonAction>>(
/^RATE_MEETING_BUTTON_\d$/,
async (args) => {
const [meetingId, participantId, rating]: [string, string, number] = JSON.parse(args.action.value);
const meeting = await db.get(meetingsTable, meetingId);
const installation = await db.get(installationsTable, meeting.installationId);
await db.insert(meetingFeedbacksTable, {
meetingId,
participantId,
createdAt: now(),
value: rating,
participantLinks: `[${args.body.user.name}](${getParticipantAirtableLink(installation, participantId)})`,
});
const text = (await getGlobalSettings()).feedbackMeetingIndividualMessage;
await args.say({ text, blocks: makeMessage(text) });
await acknowledgeSlackButton(args);
},
);