# Slack

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

SwB 用一個 Slack 應用程式在 Slack 中讀取訊息和發文，這個應用程式由你以資訊清單建立。資訊清單是一個檔案，包含應用程式的全部設定。

應用程式使用 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 交給代理。這個 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` 權限範圍的使用者權杖。
