# 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](https://channelstack.dev/docs/getting-started) once.

> [!TIP]
> Every method, option and wait of the Slack fake, with each call in TypeScript, Python and Rust, is in the [Slack API reference](https://channelstack.dev/docs/reference/api/slack).

```ts title="TypeScript"
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");
});
```

```python title="Python"
import re

from channelstack import SlackFixture
from pytest_channelstack import expect


async def test_a_mention_gets_exactly_one_reply_in_the_thread(slack: SlackFixture) -> None:
    ana = await slack.create_user(name="Ana")
    channel = await slack.create_channel(participants=[ana, slack.app])

    msg = await ana.say("@my-agent summarize yesterday's incidents", channel=channel)

    await expect(msg.thread).to_have_reply_from("my-agent", text=re.compile("incident", re.I))
    await expect(msg.thread).to_have_replies_from(slack.app, count=1)
    await expect(channel).not_to_have_channel_message_from("my-agent")
```

```rust title="Rust"
let ana = slack.create_user().name("Ana").await?;
let channel = slack
    .create_channel()
    .participants(vec![&ana as &dyn Handle, &slack.app()])
    .await?;

let msg = ana
    .say("@my-agent summarize yesterday's incidents")
    .channel(&channel)
    .await?;

expect(&msg.thread())
    .to_have_reply_from("my-agent")
    .text(Regex::new("(?i)incident")?)
    .await?;
expect(&msg.thread())
    .to_have_replies_from(&slack.app(), 1)
    .await?;
expect(&channel)
    .not_to_have_channel_message_from("my-agent")
    .await?;
```

## 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](#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.

| Area                  | What's there                                                                                                                                                                                                                                                                          |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Messages              | `chat.postMessage`, `update`, `delete`, `postEphemeral`, `response_url` answers, Block Kit blocks, message metadata                                                                                                                                                                   |
| Streaming             | `chat.startStream`, `appendStream`, `stopStream`, and the Stop button                                                                                                                                                                                                                 |
| Agents and assistants | `assistant.threads.setStatus`, `setSuggestedPrompts`, `setTitle`, `agents.sessions.setStatus` and `rename`, the split view events                                                                                                                                                     |
| Conversations         | Public and private channels, DMs and group DMs: `conversations.history`, `replies`, `info`, `list`, `members`, `open`, `join`, `archive`                                                                                                                                              |
| People                | `users.info`, `users.list`, `auth.test`                                                                                                                                                                                                                                               |
| Reactions and files   | `reactions.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 })`)                                                                           |
| Interactivity         | `block_actions` (buttons, select menus), `views.open`, `push`, `update`, `view_submission`, `view_closed`, message shortcuts, slash commands                                                                                                                                          |
| Events                | `app_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` |
| Transport             | HTTP 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:

```bash
npx channelstack env
```

```ini
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/`.

```ts title="Bolt for JavaScript"
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);
```

```python title="Python (slack_sdk)"
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:

```ts title="Chat SDK"
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](https://channelstack.dev/docs/guide/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:

```bash
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](https://channelstack.dev/docs/guide/connecting-your-agent) 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:

```ts title="agent.ts"
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](https://channelstack.dev/docs/guide/writing-tests)).

```ts title="TypeScript"
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" });
```

```python title="Python"
ana = await slack.create_user(name="Ana")
ben = await slack.create_user(name="Ben", admin=True)
deploy_bot = await slack.create_user(name="deploy-bot", bot=True)
cara = await slack.create_user(name="Cara", id="U0ADMIN01")
```

```rust title="Rust"
let ana = slack.create_user().name("Ana").await?;
let ben = slack.create_user().name("Ben").admin(true).await?;
let deploy_bot = slack.create_user().name("deploy-bot").bot(true).await?;
let cara = slack.create_user().name("Cara").id("U0ADMIN01").await?;
```

| Option                                         | What it does                                                                      |
| ---------------------------------------------- | --------------------------------------------------------------------------------- |
| `name`                                         | Required. How mentions (`@Ana`) and matchers find the person                      |
| `id`                                           | A fixed user id, for apps that allowlist people by id. Generated (`U…`) otherwise |
| `displayName`, `realName`, `email`, `timezone` | Profile fields the app reads with `users.info`                                    |
| `admin`, `owner`                               | Workspace roles. `users.info` shows `is_admin`, `is_owner`                        |
| `guest`                                        | A guest restricted to the channels it's added to                                  |
| `bot`                                          | Another bot, to check that the app ignores bots                                   |
| `token`, `browser`                             | Live mode only: see [Live](#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`).

```ts title="TypeScript"
const channel = await slack.createChannel({ participants: [ana, ben, slack.app] });
const incidents = await slack.createChannel({ name: "incidents", private: true, participants: [ana, slack.app] });
```

```python title="Python"
channel = await slack.create_channel(participants=[ana, ben, slack.app])
incidents = await slack.create_channel(name="incidents", private=True, participants=[ana, slack.app])
```

```rust title="Rust"
let channel = slack
    .create_channel()
    .participants(vec![&ana as &dyn Handle, &ben, &slack.app()])
    .await?;
let incidents = slack
    .create_channel()
    .name("incidents")
    .private(true)
    .participants(vec![&ana as &dyn Handle, &slack.app()])
    .await?;
```

| Option             | What it does                                                                                                           |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------- |
| `participants`     | People and the app. Only they are members                                                                              |
| `name`             | Without `#`. Generated (`test-<id>`) if omitted. Unique per test session, so parallel tests can each have a `#support` |
| `private`          | A private channel: the app reads it with the `groups:*` scopes, and an outsider gets `channel_not_found`               |
| `topic`, `purpose` | Shown 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](https://channelstack.dev/docs/reference/api/slack) lists every method and option.

### Messages, threads, reactions and edits

```ts title="TypeScript"
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 });
```

```python title="Python"
msg = await ana.say("@my-agent what's the status of the web deploy?", channel=channel)
await msg.thread.wait_for_reply(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)
```

```rust title="Rust"
let msg = ana
    .say("@my-agent what's the status of the web deploy?")
    .channel(&channel)
    .await?;
msg.thread().wait_for_reply().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).await?;
```

`@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

```ts title="TypeScript"
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 });
```

```python title="Python"
ask = await msg.thread.latest_reply(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)
modal = await ana.wait_for_modal(title=re.compile("incident", re.I))
await modal.submit({"Severity": "SEV2", "Summary": "Checkout errors"})

await ana.run_command("/deploy web staging", channel=channel)
await ana.shortcut("create_ticket", message=report)
```

```rust title="Rust"
let ask = msg.thread().latest_reply().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()).await?;
let modal = ana
    .wait_for_modal()
    .title(Regex::new("(?i)incident")?)
    .await?;
modal
    .submit(json!({ "Severity": "SEV2", "Summary": "Checkout errors" }))
    .await?;

ana.run_command("/deploy web staging")
    .channel(&channel)
    .await?;
ana.shortcut("create_ticket").message(&report).await?;
```

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

```ts title="TypeScript"
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");
```

```python title="Python"
msg = await ana.say("write a long report on Q3", to=slack.app)
await expect(msg.thread).to_have_streaming_reply_from("my-agent")

await ana.stop(thread=msg)
await expect(msg.thread).to_have_status("stopped")
```

```rust title="Rust"
let msg = ana
    .say("write a long report on Q3")
    .to(&slack.app())
    .await?;
expect(&msg.thread())
    .to_have_streaming_reply_from("my-agent")
    .await?;

ana.stop().thread(&msg).await?;
expect(&msg.thread()).to_have_status("stopped").await?;
```

`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

```ts title="TypeScript"
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 });
```

```python title="Python"
thread = await ana.open_agent_thread(from_=incidents)
prompts = await thread.suggested_prompts()

await ana.pick_prompt("What changed?", thread=thread)
await ana.switch_context(general, thread=thread)
await ana.reply("summarize this channel", thread=thread)
```

```rust title="Rust"
let thread = ana.open_agent_thread().from(&incidents).await?;
let prompts = thread.suggested_prompts().await?;

ana.pick_prompt("What changed?").thread(&thread).await?;
ana.switch_context(&general).thread(&thread).await?;
ana.reply("summarize this channel").thread(&thread).await?;
```

`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:

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

```python title="Python"
await slack.uninstall_app()  # app_uninstalled and tokens_revoked
await slack.fault(type="rate_limit", operation="chat.postMessage", retry_after="1s", times=1)
assert msg.delivery
await msg.delivery.retry(after="3s", reason="http_timeout")
```

```rust title="Rust"
slack.uninstall_app().await?; // app_uninstalled and tokens_revoked
slack
    .fault()
    .r#type("rate_limit")
    .operation("chat.postMessage")
    .retry_after("1s")
    .times(1)
    .await?;
let delivery = msg.delivery().expect("the fake delivered the message");
delivery.retry().after("3s").reason("http_timeout").await?;
```

## 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](https://channelstack.dev/docs/guide/waiting-and-quiet) explains why.

| Matcher                                           | Subject                    | Reads                                       |
| ------------------------------------------------- | -------------------------- | ------------------------------------------- |
| `toHaveReplyFrom(author, { text })`               | thread, DM                 | `thread.replies()`                          |
| `toHaveRepliesFrom(author, { count })`            | thread, DM                 | `thread.replies()`, once quiet              |
| `toHaveChannelMessageFrom(author)`                | conversation               | `conversation.messages()` (outside threads) |
| `toContainText(text, { count, from, as })`        | anything with a transcript | `transcript()`                              |
| `toHaveStreamingReplyFrom(author)`                | thread                     | `message.stream`                            |
| `toHaveStatus(state)`, `toHaveStatusText(text)`   | thread                     | `thread.status()`                           |
| `toHaveButton(label)`                             | message, thread            | `message.blocks`                            |
| `toHaveReaction(emoji, { by })`                   | message                    | `message.reactions()`                       |
| `toHaveOpenedModal(title)`                        | person                     | `user.modals()`                             |
| `toBeEphemeralFor(user)`                          | message                    | `message.visibleTo`                         |
| `toBeAccepted`, `toBeRejected`, `toBeRetried`     | delivery                   | `delivery.attempts`                         |
| `toHaveReceivedCall(operation, { times, error })` | `slack`                    | `slack.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](https://channelstack.dev/docs/reference/matchers) 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:

```ts title="TypeScript"
const transcript = await msg.thread.transcript();
expect(transcript.toString()).toMatchSnapshot();
```

```python title="Python"
transcript = await msg.thread.transcript()
# pytest has no snapshots of its own: compare with the conversation as a person reads it.
assert str(transcript) == "Ana: what's 2 + 2?\n  my-agent: 2 + 2 is 4."
```

```rust title="Rust"
let transcript = msg.thread().transcript().await?;
// No snapshots in Rust's test harness: compare with the conversation as a person reads it.
assert_eq!(
    transcript["text"],
    "Ana: what's 2 + 2?\n  my-agent: 2 + 2 is 4."
);
```

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

```ts title="TypeScript"
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" });
});
```

```python title="Python"
import re

from channelstack import SlackFixture
from pytest_channelstack import expect


async def test_approval_buttons_act_once_even_on_a_double_click(slack: SlackFixture) -> None:
    ana = await slack.create_user(name="Ana")
    ben = await slack.create_user(name="Ben", admin=True)
    channel = await slack.create_channel(participants=[ana, ben, slack.app])
    msg = await ana.say("@my-agent deploy web to staging", channel=channel)

    ask = await msg.thread.latest_reply(from_="my-agent")
    await expect(ask).to_have_button("Approve")

    await ben.click("Approve", message=ask)
    await ben.click("Approve", message=ask)

    await expect(msg.thread).to_contain_text("Deploying", count=1)
    await expect(slack).not_to_have_received_call(re.compile(".*"), error="not_in_channel")
```

```rust title="Rust"
let ana = slack.create_user().name("Ana").await?;
let ben = slack.create_user().name("Ben").admin(true).await?;
let channel = slack
    .create_channel()
    .participants(vec![&ana as &dyn Handle, &ben, &slack.app()])
    .await?;
let msg = ana
    .say("@my-agent deploy web to staging")
    .channel(&channel)
    .await?;

let ask = msg.thread().latest_reply().from("my-agent").await?;
expect(&ask).to_have_button("Approve").await?;

ben.click("Approve").message(&ask).await?;
ben.click("Approve").message(&ask).await?;

expect(&msg.thread())
    .to_contain_text("Deploying")
    .count(1)
    .await?;
expect(&slack)
    .not_to_have_received_call(Regex::new(".*")?)
    .error("not_in_channel")
    .await?;
```

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](https://channelstack.dev/docs/guide/standard-checks).

## Delivery specifics

### Signatures and headers

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

| Header                                      | Value                                                                                       |
| ------------------------------------------- | ------------------------------------------------------------------------------------------- |
| `X-Slack-Request-Timestamp`                 | Seconds since the epoch                                                                     |
| `X-Slack-Signature`                         | `v0=` and the hex HMAC-SHA256 of `v0:<timestamp>:<body>`, keyed with `SLACK_SIGNING_SECRET` |
| `X-Slack-Retry-Num`, `X-Slack-Retry-Reason` | On 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:

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

```python title="Python"
assert msg.delivery
await msg.delivery.retry(after="3s", reason="http_timeout")
await expect(msg.thread).to_have_replies_from("my-agent", count=1)
```

```rust title="Rust"
let delivery = msg.delivery().expect("the fake delivered the message");
delivery.retry().after("3s").reason("http_timeout").await?;
expect(&msg.thread())
    .to_have_replies_from("my-agent", 1)
    .await?;
```

Over Socket Mode, the same retry arrives as an envelope with `retry_attempt` and `retry_reason`. [Delivery and failures](https://channelstack.dev/docs/guide/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:

| Fault                                                  | Effect                                                       |
| ------------------------------------------------------ | ------------------------------------------------------------ |
| `{ 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:

| Error                                                              | When                                                                         |
| ------------------------------------------------------------------ | ---------------------------------------------------------------------------- |
| `not_authed`, `invalid_auth`                                       | No token, or a token that isn't the app's                                    |
| `not_allowed_token_type`                                           | The bot token on `apps.connections.open`, or the app token anywhere else     |
| `missing_scope`                                                    | A method outside the app's scopes, with `needed` and `provided`              |
| `not_in_channel`, `channel_not_found`                              | Posting where the app isn't a member (public, or private and DM)             |
| `is_archived`, `message_not_found`, `thread_not_found`             | What the names say                                                           |
| `cant_update_message`, `cant_delete_message`                       | Editing or deleting someone else's message                                   |
| `user_not_in_channel`                                              | `chat.postEphemeral` to someone outside the channel                          |
| `msg_too_long`                                                     | A message or stream over Slack's length limit                                |
| `stopped_by_user`, `message_not_in_streaming_state`                | Writing to a stream after Stop, or one that has ended                        |
| `invalid_trigger_id`, `expired_trigger_id`, `exchanged_trigger_id` | `views.open` with a bad, more than 3-second-old or already used `trigger_id` |
| `hash_conflict`                                                    | `views.update` with an outdated `hash`                                       |
| `already_reacted`, `no_reaction`                                   | Adding a reaction twice, removing one that isn't there                       |
| `account_inactive`                                                 | Any call after `slack.uninstallApp()`                                        |
| `unknown_method`                                                   | A 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](https://channelstack.dev/docs/reference/api/errors) 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](https://channelstack.dev/docs/guide/several-apps).

```ts
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](https://channelstack.dev/docs/guide/live-mode) and the [API reference](https://channelstack.dev/docs/reference/api/slack).
