Docs menu

Platforms

Slack

This page shows how to test an app built on Slack's APIs: how to point it at the fake, which people and channels a test creates, what those people can do, and what you can check afterwards. It assumes you have run Getting started once.

import { expect, test } from "vitest";test("a mention gets exactly one reply, in the thread", async ({ slack }) => {  const ana = await slack.createUser({ name: "Ana" });  const channel = await slack.createChannel({ participants: [ana, slack.app] });  const msg = await ana.say("@my-agent summarize yesterday's incidents", { channel });  await expect(msg.thread).toHaveReplyFrom("my-agent", { text: /incident/i });  await expect(msg.thread).toHaveRepliesFrom(slack.app, { count: 1 });  await expect(channel).not.toHaveChannelMessageFrom("my-agent");});

What the fake covers#

The fake Slack is one workspace (Acme) with one app installed in it, named my-agent, and any other app a test adds (several apps). It serves the Web API at https://slack.localhost/api/ and delivers the Events API, interactivity and slash commands to your app, over HTTP or Socket Mode.

AreaWhat's there
Messageschat.postMessage, update, delete, postEphemeral, response_url answers, Block Kit blocks, message metadata
Streamingchat.startStream, appendStream, stopStream, and the Stop button
Agents and assistantsassistant.threads.setStatus, setSuggestedPrompts, setTitle, agents.sessions.setStatus and rename, the split view events
ConversationsPublic and private channels, DMs and group DMs: conversations.history, replies, info, list, members, open, join, archive
Peopleusers.info, users.list, auth.test
Reactions and filesreactions.add, remove, get; files.getUploadURLExternal, completeUploadExternal, info; a file's url_private for the bot token, and files a person shares (ana.say(text, { channel, files }))
Interactivityblock_actions (buttons, select menus), views.open, push, update, view_submission, view_closed, message shortcuts, slash commands
Eventsapp_mention, message.* (with the channel_join and channel_archive messages), reaction_added and removed, member_joined_channel and left, app_uninstalled, tokens_revoked, agent_session_stopped, assistant_thread_started, assistant_thread_context_changed
TransportHTTP request URL with Slack's signature, Socket Mode (apps.connections.open and the WebSocket)

The app has the scopes a conversational bot or agent usually has (chat:write, app_mentions:read, the *:history and *:read scopes, reactions:write, files:write, users:read, assistant:write and a few more). A method outside them answers missing_scope, as Slack does: conversations.archive, for one, needs channels:manage (groups:write for a private channel), which the app doesn't have.

Not covered yet: Slack Connect channels (createChannel({ shared: true }) answers unsupported_in_fake), renaming channels, a person archiving one, Workflow Builder, Canvases and Lists. A Web API method the fake doesn't have answers unknown_method.

Connecting a Slack app#

Ask the running server for the app's settings:

npx channelstack env
SLACK_API_URL=https://slack.localhost/api/
SLACK_BOT_TOKEN=xoxb-…
SLACK_APP_TOKEN=xapp-…
SLACK_SIGNING_SECRET=…

The tokens and the signing secret are random, made once for your machine and kept in ~/.channelstack/secrets.json. They stay the same across restarts until you run channelstack secrets rotate. With --no-portless, the URL is http://127.0.0.1:4500/slack/api/ instead.

Every Slack SDK takes a base URL for the Web API. Set it from SLACK_API_URL, and leave it unset in production, where the SDK falls back to https://slack.com/api/.

import { App } from "@slack/bolt";

const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  signingSecret: process.env.SLACK_SIGNING_SECRET,
  ...(process.env.SLACK_API_URL ? { clientOptions: { slackApiUrl: process.env.SLACK_API_URL } } : {}),
});

await app.start(3000);
import os
from slack_sdk import WebClient

client = WebClient(
    token=os.environ["SLACK_BOT_TOKEN"],
    base_url=os.environ.get("SLACK_API_URL", "https://slack.com/api/"),
)

In Bolt for Python, pass that client to the app: App(client=client, signing_secret=os.environ["SLACK_SIGNING_SECRET"]).

The Chat SDK's Slack adapter reads SLACK_BOT_TOKEN, SLACK_SIGNING_SECRET and SLACK_API_URL from the environment by itself, so it needs no option:

import { createSlackAdapter } from "@chat-adapter/slack";
import { createMemoryState } from "@chat-adapter/state-memory";
import { Chat } from "chat";

const slack = createSlackAdapter({ agentView: true });
const bot = new Chat({ userName: "my-agent", adapters: { slack }, state: createMemoryState() });

// Serve bot.webhooks.slack(request) at /slack/events.

An app that hard-codes slack.com (slack-go, most Go and Rust clients) can't take a base URL. Run it in proxy mode instead.

Where events are delivered#

channelstack never starts your app; it sends events to a URL you give it when the server starts:

CHANNELSTACK_SLACK_WEBHOOK_URL=http://localhost:3000/slack/events npx channelstack start

Events, interactions (buttons, menus, modals, shortcuts) and slash commands all go to that one URL, which is how Bolt and the Chat SDK serve them by default. /slack/events is Bolt's default path and the one the reference agents use. Any slash command name is accepted.

Without CHANNELSTACK_SLACK_WEBHOOK_URL and without a Socket Mode connection, nothing is delivered: a person's message is stored, but the app hears nothing. See Connecting your app for TLS trust and the other runtimes.

Socket Mode#

An app in Socket Mode connects out and needs no request URL. Give Bolt the app token:

const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  appToken: process.env.SLACK_APP_TOKEN,
  socketMode: true,
  clientOptions: { slackApiUrl: process.env.SLACK_API_URL },
});

await app.start();

Bolt calls apps.connections.open with the xapp-… token through the same base URL, and the fake hands back a WebSocket URL on its own host. From then on, deliveries go over the socket instead of the request URL. Attempts, retries and the quiet rule work the same on both transports. slack.socketConnections says how many connections the app held when the test's slack fixture was made: it's a snapshot, and nothing refreshes it during the test.

While the app is between connections, a delivery waits for it to reconnect until its ack window ends. An app that also has a request URL and has held no socket for 30 seconds is reached over HTTP again.

People and channels in a test#

A test creates the people it needs and the places they talk in. Nothing is declared in a config file, and nothing is shared between tests unless you share it with test.extend (see Writing tests).

const ana = await slack.createUser({ name: "Ana" });const ben = await slack.createUser({ name: "Ben", admin: true });const deployBot = await slack.createUser({ name: "deploy-bot", bot: true });const cara = await slack.createUser({ name: "Cara", id: "U0ADMIN01" });
OptionWhat it does
nameRequired. How mentions (@Ana) and matchers find the person
idA fixed user id, for apps that allowlist people by id. Generated (U…) otherwise
displayName, realName, email, timezoneProfile fields the app reads with users.info
admin, ownerWorkspace roles. users.info shows is_admin, is_owner
guestA guest restricted to the channels it's added to
botAnother bot, to check that the app ignores bots
token, browserLive mode only: see Live

Channels have only the members you list. The app is a member only when you include slack.app, which is Slack's own rule: an app that isn't in a channel gets no events from it and can't post there (not_in_channel).

const channel = await slack.createChannel({ participants: [ana, ben, slack.app] });const incidents = await slack.createChannel({ name: "incidents", private: true, participants: [ana, slack.app] });
OptionWhat it does
participantsPeople and the app. Only they are members
nameWithout #. Generated (test-<id>) if omitted. Unique per test session, so parallel tests can each have a #support
privateA private channel: the app reads it with the groups:* scopes, and an outsider gets channel_not_found
topic, purposeShown by conversations.info

A DM needs no channel: ana.say("hi", { to: slack.app }) opens one, and ana.openDM([ben, slack.app]) opens a group DM. The app is always in a DM with itself. To add the app to a channel later, a person invites it: await ana.invite(slack.app, { channel }) sends member_joined_channel, then the channel_join message Slack posts in the channel (subtype: "channel_join", with inviter). An agent that answers every message should skip those with a subtype, as the reference agents do.

What people do#

Every action is a method on the person who takes it. The content comes first, and where it happens is an option. Each one resolves to what it created: a message, a delivery or a modal. The API reference lists every method and option.

Messages, threads, reactions and edits#

const msg = await ana.say("@my-agent what's the status of the web deploy?", { channel });await msg.thread.waitForReply({ from: "my-agent" });await ana.reply("and the API?", { thread: msg });await ana.edit("@my-agent what's the status of the web deploy today?", { message: msg });await ana.react("ticket", { message: msg });await ana.delete({ message: msg });

@name in a message becomes a real mention (<@U…>) of that person or app; write @@ for a literal @. A mention of the app sends app_mention, and the message also sends message.channels (or .groups, .im, .mpim). msg.delivery is the app_mention delivery; msg.deliveries() lists them all.

say and reply also take { text, blocks, metadata } for a message with Block Kit or message metadata. reply takes alsoSendToChannel: true to broadcast the reply.

say and reply take files, a directory path or a list of files ({ path } on disk, or { name, content }), which the person shares with the message: ana.say("the log", { channel, files: [{ path: "./fixtures/crash.log" }] }). The message then sends message with subtype: "file_share" and the files, and app_mention carries them too; a message with files needs no text. The agent reads each file from its url_private (or url_private_download) with the bot token, as with Slack. The fake keeps files in memory and takes 10 MB a file at most.

Buttons, menus, modals, commands and shortcuts#

const ask = await msg.thread.latestReply({ from: "my-agent" });await ben.click("Approve", { message: ask });await ana.select("staging", { menu: "Environment", message: ask });await ana.click("Open form", { thread: msg.thread });const modal = await ana.waitForModal({ title: /incident/i });await modal.submit({ Severity: "SEV2", Summary: "Checkout errors" });await ana.runCommand("/deploy web staging", { channel });await ana.shortcut("create_ticket", { message: report });

A button is found by its visible label or by { actionId, value }. Without message, click uses the app's latest message in the thread (or channel) that has the button. Like a person, click, select and pickPrompt wait for the control to appear: they try again while it isn't there, up to the timeout, and then fail with a list of what was there.

A modal is the one the app opened for that person with views.open. submit fills inputs by label or by block_id.action_id and sends view_submission; close sends view_closed if the app asked for it. modal.errors holds what the app answered with response_action: "errors". ana.modals() lists every modal the app opened for Ana, open or not.

Streams, status and Stop#

const msg = await ana.say("write a long report on Q3", { to: slack.app });await expect(msg.thread).toHaveStreamingReplyFrom("my-agent");await ana.stop({ thread: msg });await expect(msg.thread).toHaveStatus("stopped");

stop presses Stop on the agent's work in a thread and sends agent_session_stopped. After that, chat.appendStream and chat.stopStream on that stream answer stopped_by_user.

The agent split view#

const thread = await ana.openAgentThread({ from: incidents });const prompts = await thread.suggestedPrompts();await ana.pickPrompt("What changed?", { thread });await ana.switchContext(general, { thread });await ana.reply("summarize this channel", { thread });

openAgentThread sends assistant_thread_started (with the channel Ana is looking at as its context), switchContext sends assistant_thread_context_changed, and pickPrompt posts the prompt's message as Ana. An app that isn't subscribed to those events gets split_view_unavailable.

Conversations and membership#

ana.openDM(ben) or ana.openDM([ben, slack.app]) opens or reuses a DM; ana.invite(...) and ana.kick(...) change who's in a channel. A DM stays between its two people: inviting a third into it, or kicking one of them, is refused with method_not_supported_for_channel_type, as on Slack. To talk with a third person, open a group DM with all of them. ana.ephemeral() returns the messages only Ana can see, once the app is quiet.

What only the test does#

Some things have no person behind them. They live on the fixture or the delivery:

await slack.uninstallApp(); // app_uninstalled and tokens_revokedawait slack.fault({ type: "rate_limit", operation: "chat.postMessage", retryAfter: "1s", times: 1 });await msg.delivery!.retry({ after: "3s", reason: "http_timeout" });

What to check#

The matchers read what a person would see: edits applied, deleted messages gone, stream chunks joined, button labels as text, ephemeral messages only for their recipient. Positive matchers resolve as soon as the condition holds; .not and exact counts wait until the app is quiet first. Waiting and quiet explains why.

MatcherSubjectReads
toHaveReplyFrom(author, { text })thread, DMthread.replies()
toHaveRepliesFrom(author, { count })thread, DMthread.replies(), once quiet
toHaveChannelMessageFrom(author)conversationconversation.messages() (outside threads)
toContainText(text, { count, from, as })anything with a transcripttranscript()
toHaveStreamingReplyFrom(author)threadmessage.stream
toHaveStatus(state), toHaveStatusText(text)threadthread.status()
toHaveButton(label)message, threadmessage.blocks
toHaveReaction(emoji, { by })messagemessage.reactions()
toHaveOpenedModal(title)personuser.modals()
toBeEphemeralFor(user)messagemessage.visibleTo
toBeAccepted, toBeRejected, toBeRetrieddeliverydelivery.attempts
toHaveReceivedCall(operation, { times, error })slackslack.calls()

thread.status() is working while a status line is set, streaming while a reply streams, stopped after Stop and idle when done. The matchers reference has every option.

Every matcher is built on a read you can call yourself: channel.messages(), thread.replies(), thread.latestReply({ from }) (once the app is quiet), message.refresh(), message.stream, thread.status(), slack.calls({ operation, error }), slack.deliveries(). A transcript reads like the conversation and makes a good snapshot:

const transcript = await msg.thread.transcript();expect(transcript.toString()).toMatchSnapshot();

A full test, an approval that must act once even on a double click:

import { expect, test } from "vitest";test("approval buttons act once, even on a double click", async ({ slack }) => {  const ana = await slack.createUser({ name: "Ana" });  const ben = await slack.createUser({ name: "Ben", admin: true });  const channel = await slack.createChannel({ participants: [ana, ben, slack.app] });  const msg = await ana.say("@my-agent deploy web to staging", { channel });  const ask = await msg.thread.latestReply({ from: "my-agent" });  await expect(ask).toHaveButton("Approve");  await ben.click("Approve", { message: ask });  await ben.click("Approve", { message: ask });  await expect(msg.thread).toContainText("Deploying", { count: 1 });  await expect(slack).not.toHaveReceivedCall(/.*/, { error: "not_in_channel" });});

To run the same checks every Slack app should pass (one reply to a mention, survives the 3-second retry, rejects a bad signature, honours Stop), add slackChecks() from channelstack/checks. See Standard checks.

Delivery specifics#

Signatures and headers#

Every HTTP delivery carries Slack's signature over the exact bytes sent:

HeaderValue
X-Slack-Request-TimestampSeconds since the epoch
X-Slack-Signaturev0= and the hex HMAC-SHA256 of v0:<timestamp>:<body>, keyed with SLACK_SIGNING_SECRET
X-Slack-Retry-Num, X-Slack-Retry-ReasonOn retries only

Events are JSON (event_callback with event_id, team_id, api_app_id). Interactions are form-encoded with a payload field, and slash commands are form-encoded fields, as on Slack. Your app should refuse a signature that doesn't match and a timestamp more than five minutes old: delivery.resend({ badSignature: true }) and resend({ stale: true }) check that it does.

Acknowledgement and retries#

The app has 3 seconds to answer an event with a 2xx. If it doesn't, the fake retries the way Slack does: the same payload and event_id, almost at once, then after a minute, then after five minutes, with X-Slack-Retry-Num and X-Slack-Retry-Reason (http_timeout, http_error or connection_failed). An answer with X-Slack-No-Retry: 1 ends the retries. Interactions and slash commands are not retried.

In a test, msg.delivery!.retry({ after: "3s", reason: "http_timeout" }) sends a retry on purpose, so you can check the app answers once:

await msg.delivery!.retry({ after: "3s", reason: "http_timeout" });await expect(msg.thread).toHaveRepliesFrom("my-agent", { count: 1 });

Over Socket Mode, the same retry arrives as an envelope with retry_attempt and retry_reason. Delivery and failures covers redelivery, faults and what each proves.

Faults#

slack.fault(...) changes how the Web API answers the app until it's undone or the test ends:

FaultEffect
{ type: "rate_limit", operation, retryAfter, times }HTTP 429, Retry-After in seconds, error ratelimited
{ type: "latency", operation, delay }The call answers late
{ type: "error", operation, error, times }The call fails with that Slack error, such as msg_too_long
{ type: "drop_socket", after }Cuts the Socket Mode connections once, without a goodbye

What the fake refuses#

The fake refuses what Slack refuses, with Slack's error code ({ ok: false, error }). The ones apps meet most:

ErrorWhen
not_authed, invalid_authNo token, or a token that isn't the app's
not_allowed_token_typeThe bot token on apps.connections.open, or the app token anywhere else
missing_scopeA method outside the app's scopes, with needed and provided
not_in_channel, channel_not_foundPosting where the app isn't a member (public, or private and DM)
is_archived, message_not_found, thread_not_foundWhat the names say
cant_update_message, cant_delete_messageEditing or deleting someone else's message
user_not_in_channelchat.postEphemeral to someone outside the channel
msg_too_longA message or stream over Slack's length limit
stopped_by_user, message_not_in_streaming_stateWriting to a stream after Stop, or one that has ended
invalid_trigger_id, expired_trigger_id, exchanged_trigger_idviews.open with a bad, more than 3-second-old or already used trigger_id
hash_conflictviews.update with an outdated hash
already_reacted, no_reactionAdding a reaction twice, removing one that isn't there
account_inactiveAny call after slack.uninstallApp()
unknown_methodA method the fake doesn't have

A test's own action can be refused too: name_taken for a second channel with the same name in one session, split_view_unavailable, dispatch_failed when the app has no request URL and no socket, and not_found when a button never appeared. The errors reference lists them all.

Several apps#

A test adds another app to the workspace with slack.createApp({ name }): a Slack app with its own bot user, xoxb- and xapp- tokens and signing secret, and my-agent's scopes and bot events unless scopes and botEvents say otherwise. It joins channels as my-agent does, through participants, invite or openDM. Its events go to CHANNELSTACK_SLACK_APP_<NAME>_WEBHOOK_URL, or over its own Socket Mode connection; channelstack env --app coder --channel slack prints its .env lines under my-agent's keys. See several apps.

const coder = await slack.createApp({ name: "coder" });
const ana = await slack.createUser({ name: "Ana" });
const channel = await slack.createChannel({ participants: [ana, slack.app, coder] });

const msg = await ana.say("@coder what's the status of the web deploy?", { channel });

await expect(msg.thread).toHaveReplyFrom(coder);
await expect(msg.thread).toGoQuiet({ within: "10s", maxReplies: 1 });

What one app hears of another, as Slack sends it:

  • Another app's message arrives as message with its user, bot_id, app_id and bot_profile, and no subtype. An agent that ignores only subtype: "bot_message" answers it. A message that mentions an app is an app_mention for it, with the writer's bot_id.
  • Each app's own messages come back to it, as Slack sends them; Bolt's ignoreSelf drops them.
  • A bot can't open a DM with another bot: conversations.open answers cannot_dm_bot. A click on an app's button goes to that app alone, and auth.test answers as the token's app.

A test can act as an app it made: coder.say, coder.reply, coder.react and coder.unreact go through the fake's Web API with its token, as its own calls would; acting as my-agent is refused, because your agent is that app. msg.delivery is still the delivery to my-agent; msg.deliveryFor(coder) reads another app's, and every delivery says which app it went to (delivery.app). slack.uninstallApp({ app: coder }) uninstalls one app and leaves the others working.

Live#

The same tests can run against a real Slack sandbox workspace. Set the app's credentials (CHANNELSTACK_SLACK_APP_ID, CHANNELSTACK_SLACK_BOT_TOKEN), allowlist the workspace with CHANNELSTACK_LIVE_SLACK_TEAMS, and give each sandbox person a user token (CHANNELSTACK_SLACK_USER_ANA_TOKEN for createUser({ name: "Ana" })). Live, Slack delivers to your app itself; channelstack acts and reads the results back.

Slack marks every message posted through its API as an app's, so a person's say and reply are typed in Slack's own client by the browser actor, and so is click. Save each person's browser session once with channelstack login slack --user ana, or pass browser: { storageState } to createUser. Edits, deletes, reactions, invites and every read go through the user token.

Still fake only: select, shortcut, runCommand, stop, a person's files, the agent split view, uninstallApp, fixed ids, faults and the delivery checks (retry, resend, toBeAccepted). Live runs skip those steps and say why. See Live mode and the API reference.

Suggest changes to this page