|
| 1 | +import { ForbiddenError } from "@openstatus/services"; |
| 2 | +import { |
| 3 | + createSlackSubscriber, |
| 4 | + listSlackSubscribersForChannel, |
| 5 | + removeSlackSubscriber, |
| 6 | +} from "@openstatus/services/page-subscriber"; |
| 7 | +import { WebClient } from "@slack/web-api"; |
| 8 | +import type { Context } from "hono"; |
| 9 | +import { z } from "zod"; |
| 10 | + |
| 11 | +import { resolvePageFromUrl } from "./resolve-page"; |
| 12 | +import { resolveWorkspace } from "./workspace-resolver"; |
| 13 | + |
| 14 | +const slashCommandSchema = z.object({ |
| 15 | + text: z.string().optional().default(""), |
| 16 | + team_id: z.string(), |
| 17 | + channel_id: z.string(), |
| 18 | + channel_name: z.string().optional(), |
| 19 | +}); |
| 20 | + |
| 21 | +const HELP = [ |
| 22 | + "*OpenStatus*", |
| 23 | + "• `/openstatus add <status-page-url>` — subscribe this channel to a status page", |
| 24 | + "• `/openstatus remove <status-page-url>` — unsubscribe", |
| 25 | + "• `/openstatus list` — show this channel's subscriptions", |
| 26 | +].join("\n"); |
| 27 | + |
| 28 | +function ephemeral(c: Context, text: string) { |
| 29 | + return c.json({ response_type: "ephemeral", text }); |
| 30 | +} |
| 31 | + |
| 32 | +async function joinChannel(teamId: string, channelId: string): Promise<void> { |
| 33 | + try { |
| 34 | + const resolved = await resolveWorkspace(teamId); |
| 35 | + if (!resolved) return; |
| 36 | + const client = new WebClient(resolved.botToken); |
| 37 | + await client.conversations.join({ channel: channelId }); |
| 38 | + } catch (error) { |
| 39 | + // Private channels can't be self-joined — the bot must be /invited. |
| 40 | + if (error instanceof Error) { |
| 41 | + console.error( |
| 42 | + `slack: conversations.join failed for ${channelId}: ${error.message}`, |
| 43 | + ); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export async function handleSlackCommand(c: Context) { |
| 49 | + const parsed = slashCommandSchema.safeParse(c.get("slackBody")); |
| 50 | + if (!parsed.success) { |
| 51 | + return ephemeral(c, "Could not read the command."); |
| 52 | + } |
| 53 | + const { text, team_id, channel_id, channel_name } = parsed.data; |
| 54 | + |
| 55 | + const tokens = text.trim().split(/\s+/).filter(Boolean); |
| 56 | + const sub = (tokens[0] ?? "help").toLowerCase(); |
| 57 | + const arg = tokens[1]; |
| 58 | + |
| 59 | + if (sub === "add") { |
| 60 | + if (!arg) { |
| 61 | + return ephemeral(c, "Usage: `/openstatus add <status-page-url>`"); |
| 62 | + } |
| 63 | + const page = await resolvePageFromUrl(arg); |
| 64 | + if (!page) { |
| 65 | + return ephemeral(c, `Couldn't find a status page at \`${arg}\`.`); |
| 66 | + } |
| 67 | + try { |
| 68 | + const result = await createSlackSubscriber({ |
| 69 | + input: { |
| 70 | + pageId: page.id, |
| 71 | + teamId: team_id, |
| 72 | + channelId: channel_id, |
| 73 | + channelName: channel_name, |
| 74 | + }, |
| 75 | + }); |
| 76 | + await joinChannel(team_id, channel_id); |
| 77 | + if (result.alreadySubscribed) { |
| 78 | + return ephemeral( |
| 79 | + c, |
| 80 | + `This channel is already subscribed to *${page.title}*.`, |
| 81 | + ); |
| 82 | + } |
| 83 | + return ephemeral( |
| 84 | + c, |
| 85 | + `📡 This channel is now subscribed to *${page.title}*. Incident updates will appear here.`, |
| 86 | + ); |
| 87 | + } catch (error) { |
| 88 | + if (error instanceof ForbiddenError) { |
| 89 | + return ephemeral( |
| 90 | + c, |
| 91 | + `*${page.title}* isn't on a plan that supports subscribers.`, |
| 92 | + ); |
| 93 | + } |
| 94 | + console.error("slack /openstatus add failed:", error); |
| 95 | + return ephemeral(c, "Something went wrong subscribing this channel."); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (sub === "remove") { |
| 100 | + if (!arg) { |
| 101 | + const subs = await listSlackSubscribersForChannel({ |
| 102 | + input: { channelId: channel_id }, |
| 103 | + }); |
| 104 | + if (subs.length === 0) { |
| 105 | + return ephemeral( |
| 106 | + c, |
| 107 | + "This channel isn't subscribed to any status page.", |
| 108 | + ); |
| 109 | + } |
| 110 | + if (subs.length === 1) { |
| 111 | + await removeSlackSubscriber({ |
| 112 | + input: { pageId: subs[0].pageId, channelId: channel_id }, |
| 113 | + }); |
| 114 | + return ephemeral(c, `Unsubscribed from *${subs[0].pageName}*.`); |
| 115 | + } |
| 116 | + const list = subs.map((s) => `• ${s.pageName}`).join("\n"); |
| 117 | + return ephemeral( |
| 118 | + c, |
| 119 | + `This channel is subscribed to several pages — specify which:\n${list}\n\nUsage: \`/openstatus remove <status-page-url>\``, |
| 120 | + ); |
| 121 | + } |
| 122 | + const page = await resolvePageFromUrl(arg); |
| 123 | + if (!page) { |
| 124 | + return ephemeral(c, `Couldn't find a status page at \`${arg}\`.`); |
| 125 | + } |
| 126 | + const { removed } = await removeSlackSubscriber({ |
| 127 | + input: { pageId: page.id, channelId: channel_id }, |
| 128 | + }); |
| 129 | + return ephemeral( |
| 130 | + c, |
| 131 | + removed |
| 132 | + ? `Unsubscribed from *${page.title}*.` |
| 133 | + : `This channel wasn't subscribed to *${page.title}*.`, |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + if (sub === "list") { |
| 138 | + const subs = await listSlackSubscribersForChannel({ |
| 139 | + input: { channelId: channel_id }, |
| 140 | + }); |
| 141 | + if (subs.length === 0) { |
| 142 | + return ephemeral(c, "This channel isn't subscribed to any status page."); |
| 143 | + } |
| 144 | + const list = subs.map((s) => `• *${s.pageName}*`).join("\n"); |
| 145 | + return ephemeral(c, `This channel is subscribed to:\n${list}`); |
| 146 | + } |
| 147 | + |
| 148 | + return ephemeral(c, HELP); |
| 149 | +} |
0 commit comments