乐于分享
好东西不私藏

OpenClaw 多飞书机器人配置教程

OpenClaw 多飞书机器人配置教程

场景概述

当需要为不同用户配置独立的 AI 助手时(例如团队成员各自拥有专属助手),需要实现:

  • 每个机器人有独立的工作空间和记忆

  • 消息能正确路由到对应的助手

  • 每个助手有独特的身份和回复风格

核心概念

1. dmScope - 会话路由范围

控制私信消息如何路由到 Agent:

值说明
main所有私信都路由到 main agent(默认,不推荐多机器人场景)
per-peer基于发送者 ID 精准匹配(推荐)
per-channel-peer基于频道+发送者匹配
per-account-channel-peer基于账号+频道+发送者匹配

关键配置:

{
"session": {
"dmScope": "per-peer"
  }
}

2. Bindings - 绑定规则

定义消息如何路由到不同的 Agent。

结构:

{
"agentId": "agent-name",
"match": {
"channel": "feishu",
"accountId": "account-name",
"peer": {
"kind": "direct",
"id": "user-open-id"
    }
  }
}

重要字段:

  • kind: 必须是 "direct"(私聊)或 "group"(群聊)

  • accountId: 飞书账号配置中的 key

  • peer.id: 用户的飞书 open_id

3. 绑定顺序

特定绑定必须放在 fallback 绑定之前!

"bindings": [
// ✅ 先放特定绑定
  { "agentId": "bot-a", "match": { "channel": "feishu", "accountId": "account-a", "peer": { "kind": "direct", "id": "user-a" }}},
  { "agentId": "bot-b", "match": { "channel": "feishu", "accountId": "account-b", "peer": { "kind": "direct", "id": "user-b" }}},
// ✅ 最后放 fallback
  { "agentId": "main", "match": { "channel": "feishu" }}
]

配置步骤

步骤 1: 创建 Agent 工作空间

为每个机器人在 ~/.openclaw/workspace/agents/ 下创建工作目录:

mkdir-p ~/.openclaw/workspace/agents/{bot-a,bot-b,bot-c}

步骤 2: 初始化核心文件

在每个工作空间创建:

SOUL.md - 机器人身份定义

# SOUL.md
你是 [机器人名称],[角色描述]。
## 回复风格
- [风格特点 1]
- [风格特点 2]
## 身份标识
回复开头使用:"[emoji][名称],[问候语]"

IDENTITY.md - 基础信息

- **Name:** [名称]
- **Creature:** AI 助手
- **Vibe:** [风格]
- **Emoji:** [emoji]

AGENTS.md - 工作规范(可选)

# AGENTS.md
## 工作目录
当前工作空间
## 可用工具
[列出该机器人可用的工具]

步骤 3: 配置多账号

在 ~/.openclaw/openclaw.json 中添加飞书账号:

{
"channels": {
"feishu": {
"enabled": true,
"accounts": {
"account-a": {
"appId": "cli_xxxxx",
"appSecret": "xxxxx",
"botName": "机器人A"
        },
"account-b": {
"appId": "cli_yyyyy",
"appSecret": "yyyyy",
"botName": "机器人B"
        }
      }
    }
  }
}

步骤 4: 配置 Agent 列表

{
"agents": {
"list": [
      {
"id": "bot-a",
"workspace": "/root/.openclaw/workspace/agents/bot-a",
"agentDir": "/root/.openclaw/agents/bot-a"
      },
      {
"id": "bot-b",
"workspace": "/root/.openclaw/workspace/agents/bot-b",
"agentDir": "/root/.openclaw/agents/bot-b"
      }
    ]
  }
}

步骤 5: 配置 Bindings

{
"bindings": [
    {
"agentId": "bot-a",
"match": {
"channel": "feishu",
"accountId": "account-a",
"peer": {
"kind": "direct",
"id": "ou_xxxxxxxxxxxxxxxx"// 用户 open_id
        }
      }
    },
    {
"agentId": "bot-b",
"match": {
"channel": "feishu",
"accountId": "account-b",
"peer": {
"kind": "direct",
"id": "ou_yyyyyyyyyyyyyyyy"
        }
      }
    },
    {
"agentId": "main",
"match": {
"channel": "feishu"
      }
    }
  ]
}

步骤 6: 重启网关

openclaw gateway restart

常见问题

问题 1: 所有消息都路由到同一个机器人

原因:dmScope 设置为 main

解决:

"session": {
"dmScope": "per-peer"
}

问题 2: 配置报错 "Invalid input"

原因 1: 缺少 peer.kind 字段

解决:

"peer": {
"kind": "direct",  // 必须显式指定
"id": "ou_xxxxx"
}

原因 2:kind 写成了 "dm" 或其他无效值

解决: 必须使用 "direct" 或 "group"

问题 3: 配置已修改但路由不生效

原因: 网关未重启或配置格式错误导致加载失败

解决:

  1. 检查日志:tail -50 ~/.openclaw/logs/openclaw.log | grep -E "(config|binding)"

  2. 验证配置格式:openclaw doctor

  3. 重启网关:openclaw gateway restart

问题 4: 找不到用户的 open_id

解决: 在飞书管理后台查看用户详情,或使用 API 查询:

# 通过手机号搜索
feishu_search_user --query"手机号"

验证方法

1. 检查网关状态

openclaw gateway status

2. 查看绑定是否加载

grep-E"(config change|binding)" ~/.openclaw/logs/openclaw.log | tail -10

预期输出:

config change applied (dynamic reads: bindings)

3. 测试消息路由

向不同机器人发送消息,检查:

  • 回复的身份标识是否正确

  • 日志中 dispatching to agent 的目标是否正确

# 查看路由日志
tail -f ~/.openclaw/logs/openclaw.log | grep dispatching

最佳实践

  1. 命名规范: 使用 {purpose}-claw 或 {team}-claw 格式命名 agent

  2. 文档化: 维护一份用户-机器人对应关系表

  3. 测试: 配置完成后逐一测试每个机器人的身份识别

  4. 备份: 定期备份 openclaw.json 配置文件

参考配置

完整的 openclaw.json 关键部分:

{
"agents": {
"list": [
      {"id": "bot-a", "workspace": "...", "agentDir": "..."},
      {"id": "bot-b", "workspace": "...", "agentDir": "..."}
    ]
  },
"bindings": [
    {"agentId": "bot-a", "match": {"channel": "feishu", "accountId": "acc-a", "peer": {"kind": "direct", "id": "ou_xxx"}}},
    {"agentId": "bot-b", "match": {"channel": "feishu", "accountId": "acc-b", "peer": {"kind": "direct", "id": "ou_yyy"}}},
    {"agentId": "main", "match": {"channel": "feishu"}}
  ],
"session": {
"dmScope": "per-peer"
  },
"channels": {
"feishu": {
"enabled": true,
"accounts": {
"acc-a": {"appId": "...", "appSecret": "..."},
"acc-b": {"appId": "...", "appSecret": "..."}
      }
    }
  }
}

相关学习资料