Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. π₯
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
Nick: fixes slack app
Nicolas7d3c1446821c
diff --git a/apps/api/src/controllers/v2/slack.ts b/apps/api/src/controllers/v2/slack.tsindex 213f79f8b..e0ae66d47 100644--- a/apps/api/src/controllers/v2/slack.ts+++ b/apps/api/src/controllers/v2/slack.ts@@ -15,7 +15,10 @@ import {getSlackInstallationBySlackTeam,} from "../../services/integrations/slack/store";import { decryptSlackToken } from "../../services/integrations/slack/crypto";-import { listChannels } from "../../services/integrations/slack/client";+import {+ listChannels,+ postToResponseUrl,+} from "../../services/integrations/slack/client";import { verifySlackSignature } from "../../services/integrations/slack/signature";import {handleFirecrawlCommand,@@ -195,8 +198,9 @@ export async function slackDisconnectController(return res.status(200).json({ success: true });}-// POST /v2/slack/commands (public, signature-verified) β the /monitor slash-// command. Slack expects a 200 with a JSON message within 3 seconds.+// POST /v2/slack/commands (public, signature-verified) β the /monitor and+// /firecrawl slash commands. Slack enforces a 3-second ack deadline, so we+// respond immediately and deliver results via response_url.export async function slackCommandsController(req: Request, res: Response) {const rawBody = (req as Request & { rawBody?: Buffer }).rawBody;const valid = verifySlackSignature({@@ -223,36 +227,64 @@ export async function slackCommandsController(req: Request, res: Response) {});}- const installation = await getSlackInstallationBySlackTeam(slackTeamId);- if (!installation) {- return res.status(200).json({- response_type: "ephemeral",- text: `This workspace isn't linked to Firecrawl yet. Connect it from the dashboard: ${config.FIRECRAWL_DASHBOARD_URL}/app/monitoring`,- });- }-- // Both /monitor and /firecrawl post to this endpoint; route by which command- // Slack sent so account/workspace commands stay off /monitor.- const command = (body.command ?? "").toLowerCase();- const handler =- command === "/firecrawl" ? handleFirecrawlCommand : handleSlashCommand;+ const runCommand = async (): Promise<{+ response_type: "ephemeral" | "in_channel";+ text: string;+ blocks?: unknown[];+ }> => {+ const installation = await getSlackInstallationBySlackTeam(slackTeamId);+ if (!installation) {+ return {+ response_type: "ephemeral",+ text: `This workspace isn't linked to Firecrawl yet. Connect it from the dashboard: ${config.FIRECRAWL_DASHBOARD_URL}/app/monitoring`,+ };+ }- try {- const response = await handler({+ // Both /monitor and /firecrawl post to this endpoint; route by which+ // command Slack sent so account/workspace commands stay off /monitor.+ const command = (body.command ?? "").toLowerCase();+ const handler =+ command === "/firecrawl" ? handleFirecrawlCommand : handleSlashCommand;+ return handler({installation,text: body.text ?? "",channelId: body.channel_id ?? "",channelName: body.channel_name ?? "this channel",userId: body.user_id ?? "",});- return res.status(200).json(response);- } catch (error) {- logger.error("Slack slash command failed", { error, slackTeamId });- return res.status(200).json({- response_type: "ephemeral",- text: "Something went wrong handling that command.",- });+ };++ const responseUrl = body.response_url;+ if (!responseUrl) {+ // Shouldn't happen for real slash commands; handle inline as a fallback.+ try {+ return res.status(200).json(await runCommand());+ } catch (error) {+ logger.error("Slack slash command failed", { error, slackTeamId });+ return res.status(200).json({+ response_type: "ephemeral",+ text: "Something went wrong handling that command.",+ });+ }}++ // Ack immediately β Slack shows "operation_timeout" if we take >3s (DB and+ // billing lookups can exceed that). The real result is delivered via the+ // response_url, which stays valid for ~30 minutes.+ res.status(200).send("");++ void (async () => {+ try {+ const response = await runCommand();+ await postToResponseUrl(responseUrl, response);+ } catch (error) {+ logger.error("Slack slash command failed", { error, slackTeamId });+ await postToResponseUrl(responseUrl, {+ response_type: "ephemeral",+ text: "Something went wrong handling that command.",+ }).catch(() => {});+ }+ })();}// POST /v2/slack/events (public, signature-verified) β URL verification duringdiff --git a/apps/api/src/services/integrations/slack/client.ts b/apps/api/src/services/integrations/slack/client.tsindex 271e6c153..a91ef5a7d 100644--- a/apps/api/src/services/integrations/slack/client.ts+++ b/apps/api/src/services/integrations/slack/client.ts@@ -104,6 +104,27 @@ export async function postSlackMessage(params: {return { ok: res.ok, error: res.error, ts: res.ts };}+// Delivers a delayed slash-command result via the response_url Slack includes+// in the payload (valid ~30 minutes). Used so the command endpoint can ack+// within Slack's 3-second deadline and do the real work asynchronously.+export async function postToResponseUrl(+ responseUrl: string,+ message: {+ response_type: "ephemeral" | "in_channel";+ text: string;+ blocks?: unknown[];+ },+): Promise<void> {+ const res = await fetch(responseUrl, {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify(message),+ });+ if (!res.ok) {+ logger.warn("Slack response_url delivery failed", { status: res.status });+ }+}+// Public channels can be auto-joined by the bot; private channels require a// manual invite (surfaced to the user as an error).export async function joinChannel(params: {