|
| 1 | +--- |
| 2 | +title: "Slack" |
| 3 | +description: "Send and receive messages through the Slack API" |
| 4 | +--- |
| 5 | + |
| 6 | +```ts |
| 7 | +import { slack } from "spectrum-ts/providers/slack"; |
| 8 | +``` |
| 9 | + |
| 10 | +The Slack provider connects your agent to Slack workspaces through the Slack API. It supports multi-workspace installations, text and media messaging, reactions, threaded replies, typing indicators, and edits. |
| 11 | + |
| 12 | +## Config |
| 13 | + |
| 14 | +<Tabs> |
| 15 | + <Tab title="Single workspace"> |
| 16 | + ```ts |
| 17 | + slack.config({ |
| 18 | + tokens: { |
| 19 | + TEAM_ID: "xoxb-your-bot-token", |
| 20 | + }, |
| 21 | + }); |
| 22 | + ``` |
| 23 | + </Tab> |
| 24 | + <Tab title="Multi-workspace"> |
| 25 | + ```ts |
| 26 | + slack.config({ |
| 27 | + tokens: { |
| 28 | + TEAM_A: process.env.SLACK_TOKEN_A!, |
| 29 | + TEAM_B: process.env.SLACK_TOKEN_B!, |
| 30 | + }, |
| 31 | + teams: { |
| 32 | + TEAM_A: { |
| 33 | + appId: "A0123456789", |
| 34 | + botUserId: "U0123456789", |
| 35 | + grantedScopes: ["chat:write", "channels:history"], |
| 36 | + teamName: "Team A", |
| 37 | + }, |
| 38 | + TEAM_B: { |
| 39 | + appId: "A9876543210", |
| 40 | + botUserId: "U9876543210", |
| 41 | + grantedScopes: ["chat:write", "channels:history"], |
| 42 | + teamName: "Team B", |
| 43 | + }, |
| 44 | + }, |
| 45 | + }); |
| 46 | + ``` |
| 47 | + </Tab> |
| 48 | +</Tabs> |
| 49 | + |
| 50 | +| Option | Description | |
| 51 | +|---|---| |
| 52 | +| `tokens` | A map of team ID to bot token (`xoxb-...`). At least one entry is required. | |
| 53 | +| `teams` | Optional metadata per team — app ID, bot user ID, granted scopes, and display name. | |
| 54 | +| `endpoint` | Optional custom Slack API base URL. | |
| 55 | + |
| 56 | +## Example |
| 57 | + |
| 58 | +```ts |
| 59 | +import { Spectrum } from "spectrum-ts"; |
| 60 | +import { slack } from "spectrum-ts/providers/slack"; |
| 61 | + |
| 62 | +const app = await Spectrum({ |
| 63 | + projectId: process.env.PROJECT_ID!, |
| 64 | + projectSecret: process.env.PROJECT_SECRET!, |
| 65 | + providers: [ |
| 66 | + slack.config({ |
| 67 | + tokens: { TEAM_ID: process.env.SLACK_BOT_TOKEN! }, |
| 68 | + }), |
| 69 | + ], |
| 70 | +}); |
| 71 | + |
| 72 | +for await (const [space, message] of app.messages) { |
| 73 | + if (message.content.type === "text") { |
| 74 | + await space.send(`Echo: ${message.content.text}`); |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## Starting a conversation |
| 80 | + |
| 81 | +Resolve a user by their Slack user ID and open a space. Pass `teamId` as a space parameter to target a specific workspace: |
| 82 | + |
| 83 | +```ts |
| 84 | +const sl = slack(app); |
| 85 | +const user = await sl.user("U0123456789"); |
| 86 | +const space = await sl.space.create(user, { teamId: "TEAM_ID" }); |
| 87 | + |
| 88 | +await space.send("Hello from Spectrum."); |
| 89 | +``` |
| 90 | + |
| 91 | +## Space properties |
| 92 | + |
| 93 | +Slack spaces carry a `teamId` field indicating which workspace the conversation belongs to. Access it through narrowing: |
| 94 | + |
| 95 | +```ts |
| 96 | +for await (const [space, message] of app.messages) { |
| 97 | + if (message.platform !== "Slack") continue; |
| 98 | + const slackSpace = slack(space); |
| 99 | + console.log(slackSpace.teamId); |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## Message extras |
| 104 | + |
| 105 | +Narrowed Slack messages expose additional fields: |
| 106 | + |
| 107 | +| Field | Type | Description | |
| 108 | +|---|---|---| |
| 109 | +| `isFromMe` | `boolean` | Whether the message was sent by the bot. | |
| 110 | +| `subtype` | `string` (optional) | Slack message subtype (e.g. `"bot_message"`). | |
| 111 | +| `threadTs` | `string` (optional) | Thread timestamp if the message is in a thread. | |
| 112 | +| `ts` | `string` (optional) | Message timestamp. | |
| 113 | + |
| 114 | +## Supported features |
| 115 | + |
| 116 | +| Feature | Support | |
| 117 | +|---|---| |
| 118 | +| Text messages | Send and receive | |
| 119 | +| Markdown | Send (rendered as Slack mrkdwn) | |
| 120 | +| Media (files, images) | Send and receive | |
| 121 | +| Reactions | Send and receive | |
| 122 | +| Threaded replies | Send and receive | |
| 123 | +| Typing indicators | Send | |
| 124 | +| Message edits | Send and receive | |
0 commit comments