Skip to content
Merged
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/itchy-wolves-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chat-adapter/slack": patch
---

surface Slack file upload confirmation ids in postMessage results
46 changes: 45 additions & 1 deletion packages/adapter-slack/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3918,7 +3918,10 @@ describe("postMessage", () => {
mockClientMethod(
adapter,
"files.uploadV2",
vi.fn().mockResolvedValue({ ok: true })
vi.fn().mockResolvedValue({
files: [{ files: [{ id: "F123" }] }],
ok: true,
})
);

const chatPostMessage = vi.fn();
Expand All @@ -3930,8 +3933,49 @@ describe("postMessage", () => {
} as AdapterPostableMessage);

expect(result.id).toMatch(FILE_ID_PATTERN);
expect(result.raw).toEqual({
files: [{ data: Buffer.from("hello"), filename: "test.txt" }],
uploadedFileIds: ["F123"],
});
expect(chatPostMessage).not.toHaveBeenCalled();
});

it("adds uploaded file ids to text posts with files", async () => {
const adapter = createSlackAdapter({
botToken: "xoxb-test-token",
signingSecret: "test-signing-secret",
logger: mockLogger,
});

mockClientMethod(
adapter,
"files.uploadV2",
vi.fn().mockResolvedValue({
files: [{ files: [{ id: "F123" }, { id: "F456" }] }],
ok: true,
})
);
mockClientMethod(
adapter,
"chat.postMessage",
vi.fn().mockResolvedValue({ ok: true, ts: "1234567890.999999" })
);

const result = await adapter.postMessage("slack:C123:1234567890.000000", {
markdown: "uploaded files",
files: [
{ data: Buffer.from("one"), filename: "one.txt" },
{ data: Buffer.from("two"), filename: "two.txt" },
],
} as AdapterPostableMessage);

expect(result.id).toBe("1234567890.999999");
expect(result.raw).toEqual({
ok: true,
ts: "1234567890.999999",
uploadedFileIds: ["F123", "F456"],
});
});
});

// ============================================================================
Expand Down
16 changes: 12 additions & 4 deletions packages/adapter-slack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3123,11 +3123,13 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
const threadTs = rawThreadTs || undefined;

try {
let uploadedFileIds: string[] | undefined;

// Check for files to upload
const files = extractFiles(message);
if (files.length > 0) {
// Upload files first (they're shared to the channel automatically)
await this.uploadFiles(files, channel, threadTs);
uploadedFileIds = await this.uploadFiles(files, channel, threadTs);

// If message only has files (no text/card), return early
const hasText =
Expand All @@ -3144,7 +3146,7 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
return {
id: `file-${Date.now()}`,
threadId,
raw: { files },
raw: { files, uploadedFileIds },
};
}
}
Expand Down Expand Up @@ -3182,7 +3184,10 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
return {
id: result.ts as string,
threadId,
raw: result,
raw:
uploadedFileIds === undefined
? result
: { ...result, uploadedFileIds },
};
}

Expand Down Expand Up @@ -3212,7 +3217,10 @@ export class SlackAdapter implements Adapter<SlackThreadId, unknown> {
return {
id: result.ts as string,
threadId,
raw: result,
raw:
uploadedFileIds === undefined
? result
: { ...result, uploadedFileIds },
};
} catch (error) {
this.handleSlackError(error);
Expand Down