-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslackHandlers.ts
137 lines (125 loc) · 4.67 KB
/
slackHandlers.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
import Boom from '@hapi/boom';
import type { Lifecycle } from '@hapi/hapi';
import { random } from 'lodash';
import { SLACK_COMMAND_LOCAL_PREFIX, SLACK_COMMAND_STAGING_PREFIX } from '../../consts/api';
import { handleArenaAction, handleViewSubmissionAction } from '../../games/arena/actions';
import { HUNDRED, ZERO } from '../../games/consts/global';
import type { SlackChallengesPayload } from '../../games/model/SlackChallengePayload';
import type { SlackChatUnfurlUrl } from '../../games/model/SlackChatUnfurlPayload';
import type { SlackEventsPayload } from '../../games/model/SlackEventPayload';
import type { SlackSlashCommandPayload } from '../../games/model/SlackSlashCommandPayload';
import { handleTowerBlockAction } from '../../games/tower/actions';
import { handleTowerAction } from '../../games/tower/actions/towerAction';
import { theTowerUnfurlLink } from '../../games/tower/utils';
import { blockKitCompositionImage } from '../../games/utils/generators/slack';
import { acknowledgeSlackResponse } from '../../utils/slack';
import { slackCommandSwitcher } from './utils';
export const testRouteHandler: Lifecycle.Method = async () => {
return {
data: {
message: 'Hello from the server',
},
};
};
export const slackCommandHandler: Lifecycle.Method = async (request, _h) => {
// We need to respond to Slack within 3000ms or the action will fail.
// So we acknowledge the request and then handle the command asynchronously.
void acknowledgeSlackResponse();
const slashCommandPayload: SlackSlashCommandPayload = request.pre.slashCommandPayload;
slashCommandPayload.command = slashCommandPayload.command?.replace(
SLACK_COMMAND_STAGING_PREFIX,
'/'
);
slashCommandPayload.command = slashCommandPayload.command?.replace(
SLACK_COMMAND_LOCAL_PREFIX,
'/'
);
try {
return slackCommandSwitcher(slashCommandPayload);
} catch (err: any) {
err.data = slashCommandPayload;
throw err;
}
};
export const arenaSlackActionHandler: Lifecycle.Method = async (request, _h) => {
// We need to respond to Slack within 3000ms or the action will fail.
// So we acknowledge the request and then handle the command asynchronously.
void acknowledgeSlackResponse();
const slackActionPayload = request.pre.slackActionPayload;
try {
switch (slackActionPayload.type) {
case 'view_submission':
return handleViewSubmissionAction(slackActionPayload);
default:
return handleArenaAction(slackActionPayload);
}
} catch (err: any) {
err.data = slackActionPayload;
throw err;
}
};
export const towerSlackActionHandler: Lifecycle.Method = async (request, _h) => {
// We need to respond to Slack within 3000ms or the action will fail.
// So we acknowledge the request and then handle the command asynchronously.
void acknowledgeSlackResponse();
const slackActionPayload = request.pre.slackActionPayload;
try {
switch (slackActionPayload.type) {
case 'shortcut':
case 'view_submission':
return handleTowerAction(slackActionPayload);
case 'block_actions':
return handleTowerBlockAction(slackActionPayload);
default:
throw Boom.internal('Unknown payload');
}
} catch (err: any) {
err.data = slackActionPayload;
throw err;
}
};
export const towerSlackEventHandler: Lifecycle.Method = async (request, _h) => {
const { challenge, type }: SlackChallengesPayload | SlackEventsPayload =
request.pre.slackEventsPayload;
try {
if (type === 'event_callback') {
const payload = request.pre.slackEventsPayload;
switch (payload.event.type) {
case 'link_shared':
const eventLinks = payload.event.links as { domain: string; url: string }[];
const unfurlUrlInfo: SlackChatUnfurlUrl = {
channel: payload.event.channel,
ts: payload.event.message_ts,
unfurls: Object.assign(
{},
...eventLinks.map((link) => ({
[link.url]: {
blocks: [
blockKitCompositionImage(
link.url,
`The Tower Gif ${random(ZERO, HUNDRED)}.${random(ZERO, HUNDRED)}`
),
],
},
}))
),
};
const response: { ok: boolean; error?: string | undefined } = await theTowerUnfurlLink(
unfurlUrlInfo
);
if (!response.ok) {
throw Boom.internal(response.error || 'Error in ChatUnfurl');
}
return null;
default:
return null;
}
}
return {
challenge,
};
} catch (err: any) {
err.data = request.pre.slackEventsPayload;
throw err;
}
};