OpenClaw 飞书集成:企业协作智能助手

飞书是字节跳动旗下的企业协作平台,在中国企业市场占有重要地位。通过 OpenClaw 的飞书集成,你可以将 AI 助手无缝融入企业工作流程,实现自动化办公、智能客服、数据分析等功能。本文将详细介绍如何配置和使用 OpenClaw 的飞书集成功能。

飞书平台概述

飞书核心功能

  • 即时通讯:单聊、群聊、频道
  • 文档协作:在线文档、表格、思维导图
  • 日程管理:日历、会议、任务
  • 审批流程:自定义审批工作流
  • 开放平台:丰富的 API 和 SDK

为什么选择飞书集成?

优势 说明
企业渗透率高 大量中国企业使用
功能丰富 通讯、文档、审批一体化
API 完善 开放平台功能强大
私有化支持 支持私有化部署

创建飞书应用

步骤 1:进入开发者后台

  1. 访问 飞书开放平台
  2. 登录并进入 开发者后台
  3. 点击 创建企业自建应用

步骤 2:配置应用信息

填写基本信息: - 应用名称:OpenClaw Assistant - 应用描述:OpenClaw AI 智能助手 - 应用图标:上传应用图标

步骤 3:配置权限

权限管理 中申请以下权限:

权限名称 权限说明
im:message 获取与发送消息
im:message:send_as_bot 以应用身份发送消息
im:chat 获取群组信息
contact:user.base:readonly 获取用户基本信息
docs:doc:readonly 读取文档内容
docs:doc 编辑文档内容
calendar:calendar 读写日历

步骤 4:获取凭证

凭证与基础信息 中获取: - App ID - App Secret

步骤 5:配置事件订阅

  1. 事件订阅 中启用事件
  2. 配置请求网址(你的服务器 URL)
  3. 添加事件:
  4. im.message.receive_v1:接收消息
  5. im.message.message_read_v1:消息已读

步骤 6:发布应用

配置完成后,提交审核并发布应用。

OpenClaw 飞书配置

环境变量配置

.env 文件中添加:

# 飞书应用凭证
FEISHU_APP_ID=cli_xxxxxxxxxxxx
FEISHU_APP_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# 飞书应用配置
FEISHU_VERIFICATION_TOKEN=xxxxxxxxxxxx
FEISHU_ENCRYPT_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# 允许的用户/群组(可选)
FEISHU_ALLOWED_USERS=ou_xxxxx,ou_xxxxx
FEISHU_ALLOWED_CHATS=oc_xxxxx,oc_xxxxx

配置文件

config/app.yaml 中配置:

plugins:
  - feishu

feishu:
  enabled: true
  appId: "${FEISHU_APP_ID}"
  appSecret: "${FEISHU_APP_SECRET}"

  # 消息加密配置
  encryptKey: "${FEISHU_ENCRYPT_KEY}"
  verificationToken: "${FEISHU_VERIFICATION_TOKEN}"

  # 基础设置
  settings:
    # 自动通过好友请求
    autoAcceptFriend: true
    # 自动加入群聊
    autoJoinGroup: true
    # 响应所有消息
    respondToAll: false

  # 权限配置
  permissions:
    # 允许的用户(留空则允许所有)
    allowedUsers: []
    # 允许的群组(留空则允许所有)
    allowedChats: []
    # 管理员
    admins: []

  # 功能开关
  features:
    skills: true
    cron: true
    memory: true
    docs: true
    calendar: true

使用场景示例

场景 1:企业智能客服

配置示例:

feishu:
  enabled: true
  features:
    skills: true
    memory: true

skills:
  - path: "./skills/enterprise-faq"
    name: "企业知识库问答"
    enabled: true

技能实现:

// skills/enterprise-faq/index.js
module.exports = {
  name: 'enterprise-faq',
  description: '企业知识库问答',

  async execute(params, context) {
    const { question } = params;
    const userId = context.user?.id;

    // 1. 搜索企业知识库
    const results = await searchKnowledgeBase(question);

    if (results.length > 0) {
      // 找到匹配答案
      return {
        type: 'text',
        content: results[0].answer,
        card: createAnswerCard(results[0])
      };
    }

    // 2. 未找到答案,转人工
    return {
      type: 'text',
      content: '抱歉,我暂时无法回答这个问题。已为您转接人工客服。',
      transfer: {
        type: 'human',
        department: 'support'
      }
    };
  }
};

场景 2:自动化日报收集

# config/cron.yaml
tasks:
  - name: "daily-report-reminder"
    description: "每日日报提醒"
    schedule: "0 17 * * 1-5"  # 工作日下午5点
    skill: "report-reminder"
    params:
      chatId: "oc_daily_report_group"
      message: "请记得提交今日工作日报!模板:\n1. 今日完成\n2. 明日计划\n3. 需要支持"

场景 3:智能文档助手

feishu:
  features:
    docs: true

skills:
  - path: "./skills/doc-assistant"
    name: "文档助手"
    triggers:
      - "总结文档"
      - "文档摘要"
// skills/doc-assistant/index.js
module.exports = {
  name: 'doc-assistant',

  async execute(params, context) {
    const { docUrl, action } = params;

    // 1. 获取文档内容
    const docContent = await context.feishu.getDocContent(docUrl);

    // 2. 生成摘要
    if (action === 'summarize') {
      const summary = await context.llm.summarize(docContent);
      return {
        type: 'card',
        content: createSummaryCard(summary)
      };
    }

    // 其他操作...
  }
};

场景 4:会议助手

// skills/meeting-assistant/index.js
module.exports = {
  name: 'meeting-assistant',

  async execute(params, context) {
    const { action, date, time, participants, topic } = params;

    switch (action) {
      case 'schedule':
        // 创建会议
        const meeting = await context.feishu.createMeeting({
          title: topic,
          startTime: `${date} ${time}`,
          duration: 60,
          participants
        });

        return {
          type: 'text',
          content: `✅ 会议已创建\n\n主题:${topic}\n时间:${date} ${time}\n参与者:${participants.join('、')}\n\n会议链接:${meeting.url}`
        };

      case 'list':
        // 查询会议
        const meetings = await context.feishu.getMeetings({
          date
        });

        return {
          type: 'card',
          content: createMeetingListCard(meetings)
        };

      case 'remind':
        // 发送提醒
        await context.feishu.sendMessage({
          chatId: params.chatId,
          content: `⏰ 会议提醒:${topic} 即将在 15 分钟后开始`
        });

        return { type: 'text', content: '提醒已发送' };
    }
  }
};

场景 5:审批流程助手

// skills/approval-assistant/index.js
module.exports = {
  name: 'approval-assistant',

  async execute(params, context) {
    const { action, approvalCode, reason } = params;
    const userId = context.user?.id;

    switch (action) {
      case 'query':
        // 查询待审批
        const pendingApprovals = await context.feishu.getPendingApprovals(userId);
        return {
          type: 'card',
          content: createApprovalListCard(pendingApprovals)
        };

      case 'approve':
        // 审批通过
        await context.feishu.approve({
          approvalCode,
          userId,
          action: 'approve',
          comment: reason
        });
        return { type: 'text', content: '✅ 审批已通过' };

      case 'reject':
        // 审批拒绝
        await context.feishu.approve({
          approvalCode,
          userId,
          action: 'reject',
          comment: reason
        });
        return { type: 'text', content: '❌ 审批已拒绝' };
    }
  }
};

飞书消息卡片

创建消息卡片

const card = {
  config: {
    wide_screen_mode: true
  },
  elements: [
    {
      tag: 'div',
      text: {
        content: '**工作日报收集**',
        tag: 'lark_md'
      }
    },
    {
      tag: 'div',
      fields: [
        {
          is_short: true,
          text: {
            content: '**日期**\n2024-01-15',
            tag: 'lark_md'
          }
        },
        {
          is_short: true,
          text: {
            content: '**提交人数**\n25/30',
            tag: 'lark_md'
          }
        }
      ]
    },
    {
      tag: 'action',
      actions: [
        {
          tag: 'button',
          text: {
            content: '提交日报',
            type: 'plain_text'
          },
          url: 'https://example.com/submit',
          type: 'primary'
        },
        {
          tag: 'button',
          text: {
            content: '查看详情',
            type: 'plain_text'
          },
          url: 'https://example.com/details',
          type: 'default'
        }
      ]
    }
  ]
};

await context.feishu.sendMessage({
  chatId: 'oc_xxxxx',
  msgType: 'interactive',
  card: card
});

处理卡片回调

# config/feishu.yaml
feishu:
  features:
    cardCallback: true
// 处理卡片按钮点击
module.exports = {
  name: 'card-callback-handler',

  async onCardAction(action, context) {
    const { action: actionType, value } = action;

    switch (actionType.value) {
      case 'approve':
        // 处理审批
        return {
          toast: {
            type: 'success',
            content: '审批成功'
          },
          updateCard: createApprovedCard()
        };

      case 'reject':
        // 处理拒绝
        return {
          toast: {
            type: 'error',
            content: '已拒绝'
          }
        };
    }
  }
};

高级功能

1. 多租户支持

feishu:
  tenants:
    - id: "tenant1"
      appId: "${TENANT1_APP_ID}"
      appSecret: "${TENANT1_APP_SECRET}"

    - id: "tenant2"
      appId: "${TENANT2_APP_ID}"
      appSecret: "${TENANT2_APP_SECRET}"

2. 消息加密

feishu:
  encryptKey: "${FEISHU_ENCRYPT_KEY}"

  # 配置后,所有消息都会自动加解密

3. 权限验证

// 自定义权限验证
async function checkPermission(userId, action) {
  const user = await getUserInfo(userId);
  const permissions = await getPermissions(user.departmentId);

  return permissions.includes(action);
}

常见问题

问题 1:消息无法接收

检查: 1. 事件订阅是否正确配置 2. 服务器是否公网可访问 3. 验证 Token 是否正确 4. 应用是否已发布

问题 2:权限不足

检查: 1. 应用权限是否已申请 2. 权限是否已生效 3. 用户是否在允许列表

问题 3:消息卡片不显示

检查: 1. 卡片 JSON 格式是否正确 2. 图片 URL 是否可访问 3. 按钮链接是否有效

最佳实践

1. 消息频率控制

// 添加频率限制
const rateLimiter = new RateLimiter({
  windowMs: 60000,
  max: 10  // 每分钟最多10条
});

if (!rateLimiter.check(userId)) {
  return { type: 'text', content: '消息发送过于频繁,请稍后再试' };
}

2. 错误处理

try {
  const result = await context.feishu.sendMessage(params);
  return result;
} catch (error) {
  context.logger.error('Feishu API error', error);

  // 重试或降级
  return {
    type: 'text',
    content: '抱歉,服务暂时不可用,请稍后再试'
  };
}

3. 日志记录

// 记录所有消息
context.logger.info('Message received', {
  from: context.user.id,
  content: message.content,
  timestamp: Date.now()
});

总结

通过 OpenClaw 的飞书集成,你可以:

  • ✅ 将 AI 助手融入企业工作流程
  • ✅ 自动化日常办公任务
  • ✅ 构建智能客服系统
  • ✅ 实现文档和日程智能管理

飞书集成让 OpenClaw 成为真正的企业级智能助手。


相关阅读: - OpenClaw Telegram 集成 - OpenClaw Discord 集成 - OpenClaw 自动化工作流