Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-donkeys-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chat-adapter/slack": patch
---

Keep alert attachment content on normalized Slack messages. Attachments that aren't link unfurls now contribute their title, text, and fields instead of being dropped.
78 changes: 78 additions & 0 deletions packages/adapter-slack/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,84 @@ describe("parseMessage", () => {
});
});

it("preserves alert attachment content as message content", async () => {
const event: SlackEvent = {
type: "message",
user: "U123",
username: "sentry",
channel: "C456",
text: "New alert",
ts: "1786120899.208429",
attachments: [
{
fallback: "[Sentry] TypeError in checkout",
title: "TypeError: cannot read property 'id' of undefined",
text: "Occurred 42 times in the last hour.",
fields: [
{ title: "Project", value: "storefront", short: true },
{ title: "Environment", value: "production", short: true },
],
},
],
};
const expected =
"New alert\n\n" +
"TypeError: cannot read property 'id' of undefined\n" +
"Occurred 42 times in the last hour.\n" +
"Project: storefront\n" +
"Environment: production";

const sync = adapter.parseMessage(event);
const internals = adapter as unknown as {
parseSlackMessage(
value: SlackEvent,
threadId: string
): Promise<Message<unknown>>;
};
const async = await internals.parseSlackMessage(
event,
"slack:C456:1786120899.208429"
);

for (const message of [sync, async]) {
expect(message.text).toBe(expected);
}
});

it("falls back to attachment fallback only when nothing else carries content", () => {
const message = adapter.parseMessage({
type: "message",
user: "U123",
channel: "C456",
text: "Deploy finished",
ts: "1786120899.208429",
attachments: [{ fallback: "build #421 succeeded" }],
});

expect(message.text).toBe("Deploy finished\n\nbuild #421 succeeded");
});

it("ignores content in unfurl and app attachments", () => {
const message = adapter.parseMessage({
type: "message",
user: "U123",
channel: "C456",
text: "Check this out",
ts: "1786120899.208429",
attachments: [
{ is_msg_unfurl: true, title: "Foreign title", text: "Foreign text" },
{ is_app_unfurl: true, fallback: "Foreign fallback" },
{ from_url: "https://example.com", title: "Preview" },
{
original_url: "https://example.com/page",
fields: [{ title: "Key", value: "Value" }],
},
],
});

expect(message.text).toBe("Check this out");
});

it("ignores tables in unfurl and app attachments", () => {
const tableBlock = {
type: "table",
Expand Down
66 changes: 64 additions & 2 deletions packages/adapter-slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,66 @@ function eventTables(event: SlackEvent): SlackEventTables {
};
}

/**
* Legacy attachment content. Alerting integrations (Sentry, PagerDuty, GitHub)
* put the real payload in `title`/`text`/`fields` rather than in blocks, so
* without this the normalized message keeps only the one-line summary.
*/
function attachmentContent(
attachment: NonNullable<SlackEvent["attachments"]>[number]
): string[] {
const lines: string[] = [];
const push = (value: string | undefined) => {
const trimmed = value?.trim();
if (trimmed) {
lines.push(trimmed);
}
};

push(attachment.title);
push(attachment.text);
for (const field of attachment.fields ?? []) {
const title = field.title?.trim();
const value = field.value?.trim();
push(title && value ? `${title}: ${value}` : title || value);
}

// `fallback` is a plain-text stand-in for content rendered elsewhere, so it
// only adds anything when nothing else on the attachment carried it.
const hasBlocks =
Array.isArray(attachment.blocks) && attachment.blocks.length > 0;
if (lines.length === 0 && !hasBlocks) {
push(attachment.fallback);
}
return lines;
}

/**
* Attachment content from the author, in attachment order. Unfurls are skipped
* for the same reason their blocks are: the content is not theirs.
*/
function eventAttachmentContent(event: SlackEvent): string[] {
return (event.attachments ?? [])
.filter((attachment) => !isForeignAttachment(attachment))
.flatMap(attachmentContent);
}

/** Append attachment content below the message text. */
function withAttachmentContent(text: string, lines: string[]): string {
if (lines.length === 0) {
return text;
}
const joined = lines.join("\n");
return text ? `${text}\n\n${joined}` : joined;
}

/** Slack event payload (raw message format) */
export interface SlackEvent {
/** Legacy attachments (unfurl previews, app unfurls, etc.) */
attachments?: Array<{
blocks?: SlackMessageBlock[];
fallback?: string;
fields?: Array<{ title?: string; value?: string; short?: boolean }>;
from_url?: string;
image_url?: string;
is_app_unfurl?: boolean;
Expand Down Expand Up @@ -5661,7 +5715,10 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
}

protected content(event: SlackEvent, text: string): FormattedContent {
return this.assembleContent(text, eventTables(event));
return this.assembleContent(
withAttachmentContent(text, eventAttachmentContent(event)),
eventTables(event)
);
}

/**
Expand All @@ -5687,7 +5744,12 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
});

const { leading, trailing } = eventTables(event);
return this.assembleContent(text, {
const attachmentLines = await Promise.all(
eventAttachmentContent(event).map((line) =>
this.resolveInlineMentions(line, skipSelfMention)
)
);
return this.assembleContent(withAttachmentContent(text, attachmentLines), {
leading: await Promise.all(leading.map(resolve)),
trailing: await Promise.all(trailing.map(resolve)),
});
Expand Down