# Slack

URL: https://swb.one/zh-Hans/docs/setup/slack

SwB 借助一个 Slack 应用在 Slack 中读取和发布消息。这个应用由你用清单（manifest）创建，清单是包含应用全部设置的文件。

应用使用 Socket Mode。这条连接由 SwB 向 Slack 发起，因此你的服务器使用内网地址即可。

## 开始之前 [#开始之前]

使用能在工作区中创建并安装应用的 Slack 账号。你的组织可能要求应用先经审批。以所有者身份登录 SwB。

## 复制设置提示词 [#复制设置提示词]

https://swb.one/prompts/slack.md

````
Read the setup guide at https://swb.one/docs/setup/slack.

Help me connect Slack to SwB. SwB carries messages between my agents and channels.

If the SwB tools are missing, ask me to add SwB as a custom MCP server. Follow https://swb.one/docs/connect to add the client.

For Claude, give me the path `Customize > Connectors > Add > Custom > Web`. For ChatGPT, ask me to open `https://chatgpt.com/plugins`. Give me the path `+ > Add custom MCP server`. For Grok, give me the path `grok.com > Connectors > New Connector > Custom`. For other apps, ask me to open the MCP server settings. Wait for me to confirm.

Use a SwB client with owner access.

Call `connect(what="slack")`. Do each step marked "you" yourself. Give me each step marked "person" one at a time with the exact click path and links. Wait for me to say done after each step.

Ask me to open https://api.slack.com/apps. Use `Create New App > From a manifest`. A manifest is a file that sets the app's permissions and features.

For both identities, use https://swb.one/docs/setup/slack#bot-and-your-account. For my account only, use https://swb.one/docs/setup/slack#your-account-only. Ask me to paste the manifest from that section.

Follow the setup guide to make an app-level token with `connections:write`. Ask me to install the app. Ask me to find the `xapp-` token and the `xoxp-` user token. If I chose both identities, also ask me to find the `xoxb-` bot token. Ask me to invite that bot to each channel with `/invite @<bot>`. Find the workspace ID in the Slack URL, such as `https://app.slack.com/client/T12345/`.

Add the workspace under `blocks.slack.workspaces` in `config/switchboard.yaml`. Use the selected identities in the workspace settings. Call `switchboard_status` to get your agent name from `you.agent`. Before the first restart, grant that agent access under `blocks.slack.agents`.

```yaml
blocks:
  slack:
    agents:
      claude:
        workspaces:
          example: [bot, user]
```

Replace `claude` with your agent name. Replace `example` with the workspace label. Use the identities set up for that workspace. For my account only, use `[user]`. If the file needs a manual edit, give me the YAML. Ask me to apply it. Ask me to restart SwB when needed. Check identities with `slack_workspaces`.

After the restart, call `connect(what="slack")` again. Ask me to save the required tokens only through the SwB claim link. Restart SwB if `connect` asks you to. Call `connect` again to check the connection.

When `connect` reports `connected`, check access with `slack_workspaces`. Ask me to choose a test channel. Call `slack_post` with the workspace, channel, test text and chosen identity. Show me whether SwB sent the message or queued it for approval.

Subscribe to switchboard.message.created if this app supports MCP events. Otherwise, call wait or inbox to collect new messages.
````

把提示词粘贴到你的 SwB 所有者客户端中。

## 设置 Slack 应用 [#设置-slack-应用]

### 打开 Slack 应用设置 [#打开-slack-应用设置]

打开 [Slack 应用设置](https://api.slack.com/apps)。点击 **Create New App** → **From a manifest**。
### 选择工作区 [#选择工作区]

选择你的工作区，点击 **Next**。
### 粘贴清单 [#粘贴清单]

选择 **YAML**。复制下方的一份应用清单，完整粘贴进去。
### 创建应用 [#创建应用]

点击 **Next** → **Create**。
### 生成应用级令牌 [#生成应用级令牌]

点击 **Basic Information** → **App-Level Tokens** → **Generate Token and Scopes**。为令牌输入一个名称。
### 添加权限范围 [#添加权限范围]

点击 **Add Scope** → `connections:write`，再点击 **Generate**。
### 安装应用 [#安装应用]

点击 **Install App** → **Install to Workspace**。以 SwB 所代表的 Slack 用户身份点击 **Allow**。
### 在 Enterprise Grid 中获得批准 [#在-enterprise-grid-中获得批准]

如果 Slack 要求审批，把应用提交给工作区或组织的管理员批准。

### 保存令牌 [#保存令牌]

打开工作区对应的认领链接，把每个 Slack 令牌粘贴到各自的字段中。
### 邀请机器人 [#邀请机器人]

如果使用机器人，在每个要接入 SwB 的 Slack 频道中发送 `/invite @<bot name>`。
### 查找工作区 ID [#查找工作区-id]

在浏览器中打开 Slack。把地址中的工作区 ID 告诉智能体，它以 `T` 开头。

## 应用清单 [#应用清单]

### 机器人和你的账号 [#机器人和你的账号]

```yaml
# Example Slack app for an agent-to-agent workspace: a bot user and the owner's user token.
# Create at https://api.slack.com/apps -> Create New App -> From a manifest -> select your workspace.
# The bot's @handle and member-list profile come from features.bot_user.display_name and the
# app icon; they do not change per message. Per-message name/avatar = personas (chat:write.customize).
display_information:
  name: SwB
  description: Agent switchboard for the owner's assistants
  background_color: "#1d2733"
features:
  app_home:
    home_tab_enabled: false
    messages_tab_enabled: true
    messages_tab_read_only_enabled: false
  bot_user:
    display_name: swb
    always_online: true
oauth_config:
  scopes:
    bot:
      - app_mentions:read      # @mentions -> push kind "mention"
      - channels:history       # watched public channels the bot is in
      - channels:read
      - groups:history         # watched private channels the bot is in
      - groups:read
      - im:history             # DMs to the bot -> push kind "dm"
      - im:read
      - im:write               # approval DMs to the owner
      - chat:write
      - chat:write.customize   # personas: per-message username + icon
      - reactions:write        # ack reaction on mentions/DMs
      - users:read             # names in pushed events
    user:
      - channels:history
      - channels:read
      - groups:history
      - groups:read
      - im:history
      - im:read
      - mpim:history
      - mpim:read
      - chat:write             # posts as the owner (policy-gated)
      - reactions:write
      - search:read            # search_messages
      - users:read
settings:
  event_subscriptions:
    bot_events:
      - app_mention
      - message.channels
      - message.groups
      - message.im
  interactivity:
    is_enabled: true           # Approve/Reject buttons, delivered over Socket Mode
  org_deploy_enabled: false
  socket_mode_enabled: true
  token_rotation_enabled: false
```

### 仅你的账号 [#仅你的账号]

```yaml
# Example Slack app for a user-only workspace. No bot user, bot scopes, bot events, or interactivity.
# It never joins or invites; every post appears as the owner.
# Create at https://api.slack.com/apps -> Create New App -> From a manifest -> select your workspace.
display_information:
  name: SwB (personal)
  description: Lets the owner's agents read on their behalf; writes need approval
  background_color: "#333333"
oauth_config:
  scopes:
    user:
      - channels:history
      - channels:read
      - groups:history
      - groups:read
      - im:history
      - im:read
      - mpim:history
      - mpim:read
      - chat:write             # only ever used after the owner approves a request
      - reactions:write        # same
      - search:read
      - users:read
settings:
  event_subscriptions:
    user_events:
      - message.channels
      - message.groups
      - message.im
      - message.mpim
  interactivity:
    is_enabled: false
  org_deploy_enabled: false
  socket_mode_enabled: true
  token_rotation_enabled: false
```

## 智能体会做什么 [#智能体会做什么]

智能体调用 `connect(what="slack")`。SwB 检查缺少哪些令牌，并为需要令牌的工作区返回认领链接。

智能体在 `config/switchboard.yaml` 的 `blocks.slack.workspaces` 下添加工作区，设置工作区 ID 和令牌名称，并在 `blocks.slack.agents` 下授予访问权限。

下面的示例用一个机器人和你的账号连接一个工作区。把 `T0DEMO0001` 换成你的工作区 ID。

```yaml
blocks:
  slack:
    use: slack
    enabled: true
    workspaces:
      example:
        team_id: T0DEMO0001
        app_token_env: SWB_SLACK_EXAMPLE_APP_TOKEN
        identities:
          bot:
            token_env: SWB_SLACK_EXAMPLE_BOT_TOKEN
          user:
            token_env: SWB_SLACK_EXAMPLE_USER_TOKEN
    agents:
      claude:
        workspaces:
          example: [bot, user]
```

使用 `switchboard_status` 显示的智能体名称。为每个智能体添加一项授权。工作区条目和授权使用同一个工作区标签。如果使用“仅你的账号”清单，在 `team_id` 旁边添加 `user_only: true`，从 `identities` 中删除 `bot`，并把授权设为 `[user]`。

`connect` 提示时，重启 SwB。智能体可以用 `slack_workspaces` 检查每个 Slack 身份。身份指机器人或你的账号。

## 凭据 [#凭据]

| 令牌 | Slack 中的显示位置 | 格式 | 去向 |
| --- | --- | --- | --- |
| 应用级令牌 | **Basic Information** → **App-Level Tokens** | 以 `xapp-` 开头 | SwB 认领链接 |
| Bot 令牌 | **OAuth & Permissions** → **OAuth Tokens** → **Bot User OAuth Token** | 以 `xoxb-` 开头 | SwB 认领链接 |
| 用户令牌 | **OAuth & Permissions** → **OAuth Tokens** → **User OAuth Token** | 以 `xoxp-` 开头 | SwB 认领链接 |

## 检查连接 [#检查连接]

再次运行 `connect(what="slack")`，确认返回 `connected`。调用 `slack_workspaces`，确认每个身份的 `ready` 为 `true`，工作区的 `connected` 为 `true`。

如果设置了机器人，在频道中提及机器人，并加上一个目标名称。让智能体查看 `slack_events` 或 `slack_wait`。要测试发布消息，用一个有写入权限的身份调用 `slack_post`。

## 排查问题 [#排查问题]

如果 SwB 报告 Socket Mode 问题，在应用设置中打开 **Socket Mode**，开启 **Enable Socket Mode**。

更改应用的权限范围后，重新安装应用，再用新的认领链接保存新令牌。

`slack_search` 用你自己的账号搜索，需要带 `search:read` 权限范围的用户令牌。
