🏢 OpenClaw 企业级集成指南

连接飞书、企业微信、OA、ERP - 让 AI Agent 融入企业工作流

🎯 为什么需要企业集成?

早上9点01分,老板在飞书群里问:"上个月的销售数据出来了吗?" 如果Agent能自动从ERP拉数据、生成报表、发到群里,那该多好。

就像王家卫电影里说的:"有些连接,建立了,就回不去了。" 企业集成就是让AI Agent成为企业系统的一部分。

🚀 快速集成:飞书(Feishu)

1. 创建飞书应用

📝 步骤

  • 访问 飞书开放平台
  • 创建企业自建应用
  • 获取 App ID 和 App Secret
  • 配置权限:消息与群组、通讯录、文档等

2. 配置 OpenClaw

# 安装飞书插件
openclaw plugin install feishu

# 配置认证
openclaw config set feishu.appId your_app_id
openclaw config set feishu.appSecret your_app_secret

# 测试连接
openclaw feishu test

3. 发送消息示例

const { Skill } = require('@openclaw/core');
const { FeishuClient } = require('@openclaw/feishu');

class FeishuNotifierSkill extends Skill {
  async init() {
    this.feishu = new FeishuClient();
  }

  async execute(options) {
    const { chatId, message } = options;

    // 发送文本消息
    const result = await this.feishu.sendMessage({
      chatId,
      msgType: 'text',
      content: { text: message }
    });

    return result;
  }
}

💬 集成:企业微信(WeCom)

1. 企业微信配置

# 配置企业微信
openclaw config set wecom.corpId your_corp_id
openclaw config set wecom.secret your_secret
openclaw config set wecom.agentId your_agent_id

# 验证配置
openclaw wecom test

2. 接收消息(Webhook)

const express = require('express');
const { WecomBot } = require('@openclaw/wecom');

const app = express();
const bot = new WecomBot();

app.post('/wecom/webhook', async (req, res) => {
  const message = req.body;

  // 处理文本消息
  if (message.MsgType === 'text') {
    const reply = await bot.processMessage(message);
    
    // 回复消息
    await bot.sendText(message.FromUserName, reply);
  }

  res.send('success');
});

app.listen(3000);

📊 集成:OA/ERP 系统

1. 通过 API 对接

大多数现代OA/ERP系统都提供REST API:

const { Skill } = require('@openclaw/core');
const axios = require('axios');

class ERPAPISkill extends Skill {
  constructor() {
    super();
    this.apiClient = axios.create({
      baseURL: this.config.apiBaseUrl,
      headers: {
        Authorization: `Bearer ${this.config.apiToken}`
      }
    });
  }

  async getSalesReport(startDate, endDate) {
    const response = await this.apiClient.get('/api/sales/report', {
      params: { startDate, endDate }
    });

    return response.data;
  }
}

2. 通过数据库直连

const { Skill } = require('@openclaw/core');
const { Pool } = require('pg'); // PostgreSQL

class DatabaseSkill extends Skill {
  async init() {
    this.pool = new Pool({
      host: this.config.dbHost,
      port: this.config.dbPort,
      database: this.config.dbName,
      user: this.config.dbUser,
      password: this.config.dbPassword
    });
  }

  async query(sql, params = []) {
    const client = await this.pool.connect();
    
    try {
      const result = await client.query(sql, params);
      return result.rows;
    } finally {
      client.release();
    }
  }
}

🔐 认证与安全

✅ 企业集成安全原则

  • 使用 OAuth 2.0 进行认证
  • 敏感信息(Token、密码)使用环境变量
  • 设置 IP 白名单限制访问
  • 定期轮换 API 密钥
  • 记录所有 API 调用日志

示例:OAuth 2.0 认证

const { OAuth2Client } = require('@openclaw/auth');

class SecureIntegration {
  constructor() {
    this.oauth = new OAuth2Client({
      clientId: process.env.CLIENT_ID,
      clientSecret: process.env.CLIENT_SECRET,
      redirectUri: 'https://your-domain.com/callback'
    });
  }

  async getAccessToken() {
    // 尝试从缓存获取
    let token = this.getCachedToken();

    if (!token || this.isExpired(token)) {
      // 刷新 token
      token = await this.oauth.refreshToken();
      this.cacheToken(token);
    }

    return token;
  }
}

🎯 实战案例:自动化工单处理

场景:自动从飞书接收工单,处理后回复

const { Skill } = require('@openclaw/core');
const { FeishuClient } = require('@openclaw/feishu');

class TicketAutomationSkill extends Skill {
  async init() {
    this.feishu = new FeishuClient();
  }

  async execute(options) {
    const { message } = options;

    // 1. 解析工单信息
    const ticketInfo = this.parseTicket(message);

    // 2. 查询 ERP 获取客户信息
    const customerInfo = await this.queryERP(ticketInfo.customerId);

    // 3. 生成处理结果
    const solution = await this.generateSolution(ticketInfo, customerInfo);

    // 4. 回复到飞书
    await this.feishu.reply(message.messageId, solution);

    return { success: true };
  }

  parseTicket(message) {
    // 解析工单逻辑
    return {
      ticketId: 'extracted_id',
      customerId: 'extracted_customer',
      description: message.content
    };
  }
}

✅ 最佳实践

✅ 集成设计原则

  • 松耦合: 集成应该是可插拔的,不影响核心逻辑
  • 容错性: 外部系统不可用时,Agent 应该优雅降级
  • 幂等性: 重复调用不会产生副作用
  • 可观测: 记录所有集成调用的日志和指标

✅ 性能优化

  • 使用连接池管理数据库连接
  • 对频繁查询的数据使用缓存
  • 异步处理非关键路径操作
  • 设置合理的超时时间

📊 集成测试

⚠️ 测试检查清单

  • ✅ 单元测试:mock 外部系统,测试逻辑
  • ✅ 集成测试:连接测试环境,验证端到端流程
  • ✅ 压力测试:模拟高并发场景
  • ✅ 故障测试:模拟外部系统不可用
# 运行集成测试
openclaw test --integration

# 模拟飞书消息
openclaw feishu mock-message --text "测试消息"

# 验证 ERP 连接
openclaw erp test-connection