a fun bot for the hc slack
1import { slackApp } from "../../../index";
2import { blog } from "../../../libs/Logger";
3import handleHelp from "../handlers/help";
4import { handleHistory } from "../handlers/history";
5import handleHome from "../handlers/home";
6import { handleSettings, setupSubmitListener } from "../handlers/settings";
7import upload from "../handlers/upload";
8import type { MessageResponse } from "../types";
9import * as Sentry from "@sentry/bun";
10
11export default function setupActions() {
12 // Handle button actions
13 slackApp.action(
14 /^takes_(\w+)$/,
15 async () => Promise.resolve(),
16 async ({ payload, context }) => {
17 try {
18 const userId = payload.user.id;
19 const actionId = payload.actions[0]?.action_id as string;
20 const command = actionId.replace("takes_", "");
21
22 let response: MessageResponse | undefined;
23
24 // Route to the appropriate handler function
25 switch (command) {
26 case "history":
27 response = await handleHistory(userId);
28 break;
29 case "help":
30 response = await handleHelp();
31 break;
32 case "home":
33 response = await handleHome(userId);
34 break;
35 case "settings":
36 await handleSettings(
37 context.triggerId as string,
38 userId,
39 true,
40 );
41 if (context.respond)
42 await context.respond({ delete_original: true });
43 return;
44 case "setup":
45 await handleSettings(
46 context.triggerId as string,
47 userId,
48 );
49 return;
50 default:
51 response = await handleHome(userId);
52 break;
53 }
54
55 // Send the response
56 if (response && context.respond) {
57 await context.respond(response);
58 }
59 } catch (error) {
60 if (error instanceof Error)
61 blog(
62 `Error in \`${payload.actions[0]?.action_id}\` action: ${error.message}`,
63 "error",
64 );
65
66 // Capture the error in Sentry
67 Sentry.captureException(error, {
68 extra: {
69 actionId: payload.actions[0]?.action_id,
70 userId: payload.user.id,
71 channelId: context.channelId,
72 },
73 });
74
75 // Respond with error message to user
76 if (context.respond) {
77 await context.respond({
78 text: "An error occurred while processing your request. Please stand by while we try to put out the fire.",
79 response_type: "ephemeral",
80 });
81 }
82 }
83 },
84 );
85
86 // setup the upload actions
87 try {
88 upload();
89 } catch (error) {
90 Sentry.captureException(error, {
91 extra: {
92 context: "upload setup",
93 },
94 });
95 }
96
97 // setup the setup view handler
98 try {
99 setupSubmitListener();
100 } catch (error) {
101 Sentry.captureException(error, {
102 extra: {
103 context: "submit modal setup",
104 },
105 });
106 }
107}