OpenClaw Cross-Platform Agent 跨平台Agent统一管理

发布时间:2026-05-10 | 阅读时长:13分钟

早上在飞书问天气,下午在Discord发日报,晚上在Telegram查新闻。用户在每个平台都想要同样的Agent,但每个平台又都有自己的格式、限制、特性。

OpenClaw Cross-Platform模块让一个Agent内核适配多个平台外壳。写一次逻辑,部署到飞书、Discord、Telegram、微信、Slack。

支持的平台

平台接入方式特色能力
飞书OpenClaw Feishu MCP富文本卡片、文档读写、多维表格
DiscordBot API频道管理、角色权限、语音频道
TelegramBot API文件传输、Inline键盘、群组管理
企业微信WeCom MCP企业通讯录、审批流程、应用消息
SlackApp APIBlock Kit、频道、Thread

统一架构

import { CrossPlatformAgent } from 'openclaw/platform';

const agent = new CrossPlatformAgent({
  name: "miaoquai",
  
  // === Agent内核 ===
  core: {
    model: "claude-3-sonnet",
    persona: "妙趣AI",
    skills: ["web_search", "web_fetch", "analysis"],
    memory: { enabled: true, retention: "30d" }
  },
  
  // === 平台适配 ===
  platforms: {
    feishu: {
      enabled: true,
      appId: process.env.FEISHU_APP_ID,
      appSecret: process.env.FEISHU_APP_SECRET,
      
      // 飞书特有配置
      features: {
        cardMessage: true,    // 富文本卡片
        docReadWrite: true,  // 文档读写
        bitable: true        // 多维表格
      }
    },
    
    discord: {
      enabled: true,
      botToken: process.env.DISCORD_BOT_TOKEN,
      clientId: process.env.DISCORD_CLIENT_ID,
      
      // Discord特有配置
      features: {
        embedMessage: true,
        slashCommands: true,
        threadReply: true
      }
    },
    
    telegram: {
      enabled: true,
      botToken: process.env.TELEGRAM_BOT_TOKEN,
      
      // Telegram特有配置
      features: {
        inlineKeyboard: true,
        fileTransfer: true,
        groupAdmin: false
      }
    },
    
    wecom: {
      enabled: true,
      corpId: process.env.WECOM_CORP_ID,
      agentId: process.env.WECOM_AGENT_ID,
      secret: process.env.WECOM_SECRET
    }
  }
});

消息格式转换

不同平台的消息格式差异巨大。OpenClaw自动转换:

// 统一消息格式 → 平台特定格式
const messageConverter = {
  
  // 输入:统一消息对象
  input: {
    type: "rich",
    content: "这是一条消息",
    attachments: [
      { type: "image", url: "https://..." },
      { type: "file", name: "report.pdf", url: "https://..." }
    ],
    actions: [
      { type: "button", text: "查看详情", url: "https://..." }
    ]
  },
  
  // 输出:飞书卡片
  toFeishu: (message) => {
    return {
      msg_type: "interactive",
      card: {
        elements: [
          { tag: "div", text: { content: message.content } },
          { tag: "action", actions: message.actions.map(a => ({
            tag: "button",
            text: { content: a.text },
            url: a.url
          })) }
        ]
      }
    };
  },
  
  // 输出:Discord Embed
  toDiscord: (message) => {
    return {
      embeds: [{
        description: message.content,
        image: message.attachments.find(a => a.type === "image")?.url
      }],
      components: message.actions.map(a => ({
        type: 2, // BUTTON
        label: a.text,
        url: a.url
      }))
    };
  },
  
  // 输出:Telegram消息
  toTelegram: (message) => {
    return {
      text: message.content,
      parse_mode: "Markdown",
      reply_markup: {
        inline_keyboard: message.actions.map(a => [{
          text: a.text,
          url: a.url
        }])
      }
    };
  }
};

频道策略管理

// 不同频道不同策略
const channelStrategy = {
  
  // 频道配置
  channels: {
    "feishu:c942dfd": {
      name: "妙趣AI运营群",
      persona: "运营助手",
      features: ["日报推送", "任务提醒"],
      schedule: ["08:00", "22:00"]
    },
    
    "discord:1483699": {
      name: "AI工具讨论区",
      persona: "社区助手",
      features: ["新闻分享", "资源推荐"],
      schedule: ["10:00", "16:00", "22:00"]
    },
    
    "telegram:miaoquai_bot": {
      name: "个人助手",
      persona: "日常助手",
      features: ["即时问答"],
      schedule: "always"
    }
  },
  
  // 内容适配
  adaptContent: (content: string, channel: string) => {
    const strategy = channels[channel];
    
    // 飞书:格式化卡片
    if (channel.startsWith("feishu")) {
      return {
        type: "card",
        title: "妙趣日报",
        content: formatAsFeishuCard(content)
      };
    }
    
    // Discord:限制长度(2000字符)
    if (channel.startsWith("discord")) {
      return truncate(content, 2000);
    }
    
    // Telegram:Markdown格式
    if (channel.startsWith("telegram")) {
      return toTelegramMarkdown(content);
    }
    
    return content;
  }
};

统一事件处理

// 统一事件入口
agent.on("message", async (event) => {
  
  // 解析平台来源
  const platform = event.platform; // "feishu" | "discord" | "telegram"
  const channelId = event.channelId;
  const userId = event.userId;
  
  // 获取用户跨平台身份(可选)
  const unifiedUser = await resolveUserId(userId, platform);
  
  // 统一处理逻辑
  const response = await agent.process({
    message: event.content,
    user: unifiedUser,
    context: {
      platform,
      channel: channelId,
      history: await getHistory(platform, channelId)
    }
  });
  
  // 自动转换为平台格式
  const formattedResponse = agent.formatFor(response, platform);
  
  // 发送响应
  await agent.send(formattedResponse, {
    platform,
    channel: channelId,
    replyTo: event.messageId
  });
});

飞书深度集成示例

// 飞书特色能力
const feishuFeatures = {
  
  // 文档读写
  doc: {
    read: async (docToken: string) => {
      const content = await feishuDoc.read(docToken);
      return content;
    },
    
    write: async (docToken: string, content: string) => {
      await feishuDoc.write(docToken, content);
    }
  },
  
  // 多维表格
  bitable: {
    queryRecords: async (appToken: string, tableId: string) => {
      const records = await feishuBitable.listRecords(appToken, tableId);
      return records;
    },
    
    createRecord: async (appToken: string, tableId: string, fields: object) => {
      await feishuBitable.createRecord(appToken, tableId, fields);
    }
  },
  
  // 富文本卡片
  card: {
    dailyReport: () => ({
      msg_type: "interactive",
      card: {
        header: { title: { tag: "plain_text", content: "📊 每日AI新闻" } },
        elements: [
          { tag: "div", text: { tag: "lark_md", content: "**热点新闻**\n- GPT-5.5发布..." } },
          { tag: "action", actions: [
            { tag: "button", text: { content: "查看详情" }, url: "https://miaoquai.com" }
          ] }
        ]
      }
    })
  }
};

最佳实践

平台适配原则

  • 保持核心逻辑一致:不同平台输出相同质量的响应
  • 利用平台特色:飞书发卡片、Discord用Embed、Telegram用Inline键盘
  • 注意平台限制:字数限制、格式限制、频率限制
  • 统一监控:一个Dashboard看所有平台

部署建议

  • 先用一个平台验证Agent逻辑
  • 逐步扩展到其他平台
  • 使用消息队列解耦发送
  • 设置平台独立的重试策略

常见问题

Q: 如何处理平台间的用户身份?

可选方案:1)独立身份,每个平台独立用户;2)绑定身份,让用户在不同平台用同一邮箱;3)匿名处理,不存储用户信息。

Q: 发送失败如何处理?

OpenClaw内置重试机制,记录失败原因。如果平台API限流,会自动等待后重试。建议设置备用通知渠道。

相关资源

一次开发,多平台部署

不要为每个平台写一遍代码。OpenClaw Cross-Platform模块让Agent内核复用,平台外壳适配。你的Agent在飞书是运营助手,在Discord是社区管理员,在Telegram是私人秘书——但它们共享同一个大脑。

查看平台模板