Skip to content

Open-source status page, incident response and uptime monitoring

slack app: dedupe event (#2353)

Thibault Le Ouay7c4ddd7d2af4
diff --git a/apps/server/src/routes/slack/handler.test.ts b/apps/server/src/routes/slack/handler.test.ts
index 0c49074fd..feca71bcc 100644
--- a/apps/server/src/routes/slack/handler.test.ts
+++ b/apps/server/src/routes/slack/handler.test.ts
@@ -150,6 +150,39 @@ describe("handleSlackEvent", () => {
expect(slackTestState.calls.length).toBe(0);
});
+ test("responds once when a mention arrives as app_mention and message", async () => {
+ const ts = `${Date.now()}.55`;
+ const base = {
+ type: "event_callback",
+ team_id: "T_KNOWN",
+ event: {
+ text: "<@UBOT> create an incident",
+ user: "U1",
+ channel: "C1",
+ channel_type: "channel",
+ ts,
+ },
+ };
+
+ // Same underlying message, delivered as two distinct events.
+ await signAndPost(app, {
+ ...base,
+ event_id: `evt_mention_${ts}`,
+ event: { ...base.event, type: "app_mention" },
+ });
+ await signAndPost(app, {
+ ...base,
+ event_id: `evt_message_${ts}`,
+ event: { ...base.event, type: "message" },
+ });
+ await new Promise((r) => setTimeout(r, 100));
+
+ const thinking = slackTestState.calls.filter(
+ (m) => m.method === "postMessage",
+ );
+ expect(thinking.length).toBe(1);
+ });
+
test("handles app_uninstalled event", async () => {
const res = await signAndPost(app, {
type: "event_callback",
diff --git a/apps/server/src/routes/slack/handler.ts b/apps/server/src/routes/slack/handler.ts
index fbdaf05d3..8d6a54da9 100644
--- a/apps/server/src/routes/slack/handler.ts
+++ b/apps/server/src/routes/slack/handler.ts
@@ -170,6 +170,12 @@ async function processEvent(body: SlackEvent) {
const teamId = body.team_id;
if (!teamId || !event.channel || !event.ts) return;
+ // A single mention arrives as BOTH an `app_mention` and a `message.*` event
+ // (distinct event_ids, same message ts), so the event_id dedup above doesn't
+ // catch the pair. Dedup on the message identity so we only respond once.
+ // Runs before the first `await` so concurrent deliveries can't both pass.
+ if (dedup(`msg:${event.channel}:${event.ts}`)) return;
+
const resolved = await resolveWorkspace(teamId);
if (!resolved) {
logger.warn("slack integration not found", { teamId });