Skip to main content

Slack

Slack 渠道支持两种连接模式:Socket 模式(默认)和 HTTP 模式。

Socket 模式(默认)

Socket 模式是最简单的设置方式,不需要公共 URL。
1

创建 Slack 应用

前往 Slack API
  1. 点击 Create New App
  2. 选择 From scratch
  3. 输入应用名称并选择工作区
2

启用 Socket 模式

Socket Mode 中:
  1. 开启 Socket Mode
  2. 前往 Basic InformationApp-Level Tokens
  3. 点击 Generate Token and Scopes
  4. 添加 connections:write 权限范围
  5. 复制 App Token(以 xapp- 开头)
3

设置 Bot 权限

OAuth & Permissions 中添加 Bot Token 权限范围:
chat:write
channels:history, channels:read
groups:history, groups:read
im:history, im:read, im:write
mpim:history, mpim:read
users:read
app_mentions:read
reactions:read, reactions:write
pins:read, pins:write
emoji:read
files:read, files:write
安装应用并复制 Bot User OAuth Token(以 xoxb- 开头)
4

设置事件

Event Subscriptions 中启用事件并订阅:
message.channels, message.groups, message.im, message.mpim
app_mention
reaction_added, reaction_removed
member_joined_channel, member_left_channel
channel_rename
pin_added, pin_removed
5

配置 OpenClaw

export SLACK_APP_TOKEN="xapp-..."
export SLACK_BOT_TOKEN="xoxb-..."
或在配置文件中:
{
  "channels": {
    "slack": {
      "enabled": true,
      "appToken": "xapp-...",
      "botToken": "xoxb-..."
    }
  }
}

HTTP 模式

当你的 Gateway 网关可以通过 HTTPS 被 Slack 访问时使用(服务器部署的典型情况)。
1

获取凭据

  1. Basic Information → 复制 Signing Secret
  2. OAuth & Permissions → 安装应用并复制 Bot Token
2

配置 Webhook

{
  "channels": {
    "slack": {
      "enabled": true,
      "mode": "http",
      "botToken": "xoxb-...",
      "signingSecret": "your-signing-secret",
      "webhookPath": "/slack/events"
    }
  }
}
3

设置 Slack Webhook

在 Slack 应用设置中:
  1. Event Subscriptions → 设置 Request URL
    https://your-gateway-host/slack/events
    
  2. Interactivity & Shortcuts → 设置相同的 URL
  3. Slash Commands → 为每个命令设置相同的 URL

配置选项

基础配置

{
  "channels": {
    "slack": {
      "enabled": true,
      "appToken": "xapp-...",
      "botToken": "xoxb-...",
      "groupPolicy": "allowlist",
      "channels": {
        "C123456": { "allow": true, "requireMention": true },
        "#general": {
          "allow": true,
          "requireMention": true,
          "users": ["U123456"],
          "skills": ["search", "docs"],
          "systemPrompt": "保持回答简短。"
        }
      }
    }
  }
}
{
  "channels": {
    "slack": {
      "dm": {
        "enabled": true,
        "policy": "pairing"
      }
    }
  }
}
群组私信默认被忽略。启用方法:
{
  "channels": {
    "slack": {
      "dm": {
        "groupEnabled": true,
        "groupChannels": ["G123456"]  // 可选的允许列表
      }
    }
  }
}

用户令牌(可选)

OpenClaw 可以使用 Slack 用户令牌进行只读操作:
{
  "channels": {
    "slack": {
      "botToken": "xoxb-...",
      "userToken": "xoxp-...",
      "userTokenReadOnly": true  // 默认
    }
  }
}
用户令牌提供更多访问权限。将 userTokenReadOnly 设为 false 需要谨慎考虑。

回复线程

全局设置

{
  "channels": {
    "slack": {
      "replyToMode": "off"  // off | first | all
    }
  }
}
模式行为
off在主频道回复(默认)
first第一条回复进入线程
all所有回复都进入线程

按聊天类型

为不同类型的聊天配置不同的线程行为:
{
  "channels": {
    "slack": {
      "replyToMode": "off",
      "replyToModeByChatType": {
        "direct": "all",    // 私信始终使用线程
        "group": "first",   // 群组私信第一条回复使用线程
        "channel": "off"    // 频道在根级别
      }
    }
  }
}

手动线程标签

[[reply_to_current]]  # 回复触发消息
[[reply_to:1234]]     # 回复特定消息

会话路由

  • 私信: 共享 main 会话
  • 频道: agent:<agentId>:slack:channel:<channelId> 会话
  • 斜杠命令: agent:<agentId>:slack:slash:<userId> 会话
可通过 channels.slack.slashCommand.sessionPrefix 自定义前缀。

工具操作

{
  "channels": {
    "slack": {
      "actions": {
        "reactions": true,   // 表情反应 + 列表
        "messages": true,    // 读取/发送/编辑/删除
        "pins": true,        // 置顶/取消置顶/列表
        "memberInfo": true,  // 成员信息
        "emojiList": true    // 自定义表情符号列表
      }
    }
  }
}
{
  "channels": {
    "slack": {
      "slashCommand": {
        "enabled": true,
        "name": "openclaw",
        "sessionPrefix": "slack:slash",
        "ephemeral": true
      }
    }
  }
}

媒体处理

限制

{
  "channels": {
    "slack": {
      "textChunkLimit": 4000,
      "mediaMaxMb": 20
    }
  }
}

分块模式

{
  "channels": {
    "slack": {
      "chunkMode": "newline"  // length | newline
    }
  }
}

安全注意事项

Token 安全:
  • Bot 令牌用于写入操作,限制在应用权限范围内
  • 用户令牌提供更高权限,默认仅用于读取
  • 如启用 userTokenReadOnly: false,确保严格的访问控制
Bot 消息默认被忽略。 通过 channels.slack.allowBotschannels.slack.channels.<id>.allowBots 启用,但请设置防护措施防止机器人间的回复循环。

故障排除

原因: groupPolicyallowlist 但没有配置频道解决方案:
{
  "channels": {
    "slack": {
      "groupPolicy": "open"
    }
  }
}
或添加频道到允许列表。
检查项:
  1. dm.policy 设为 "pairing"
  2. 批准配对码:
    openclaw pairing list slack
    openclaw pairing approve slack <CODE>
    
原因: 机器人无法搜索频道解决方案: 使用频道 ID 而不是名称:
{
  "channels": {
    "slack": {
      "channels": {
        "C123456": { "allow": true }
      }
    }
  }
}

多账户支持

运行多个 Slack 工作区:
{
  "channels": {
    "slack": {
      "accounts": {
        "workspace1": {
          "name": "工作区 1",
          "appToken": "xapp-1-...",
          "botToken": "xoxb-1-..."
        },
        "workspace2": {
          "name": "工作区 2",
          "mode": "http",
          "botToken": "xoxb-2-...",
          "signingSecret": "secret2",
          "webhookPath": "/slack/workspace2/events"
        }
      }
    }
  }
}

相关资源

配对系统

了解如何批准新用户

斜杠命令

配置 Slack 斜杠命令

配置参考

完整的 Slack 配置选项

故障排除

解决常见问题