Feishu File Upload
Upload local files to Feishu Drive and send as file messages to chats or users.
When to Use
- - User asks to "send file", "upload file", or "share file" to Feishu
- Need to share documents (CSV, PDF, Excel, Word) with group or individual
- Bot needs to send files that Feishu's native message API doesn't support directly
- Images/videos: Use
media parameter in message tool instead (no need for this skill)
Quick Start
Send to Group Chat
CODEBLOCK0
Send to Individual User
CODEBLOCK1
Send via Email
CODEBLOCK2
Use Environment Variable
CODEBLOCK3
How It Works
- 1. Read credentials from
~/.openclaw/openclaw.json (channels.feishu.appId/appSecret) - Get tenant access token from Feishu Auth API
- Upload file to Feishu Drive → returns INLINECODE2
- Send file message using
file_key to target chat/user
Script Usage
Arguments
| Argument | Required | Default | Description |
|---|
| INLINECODE4 | Yes | - | Absolute path to local file |
| INLINECODE5 |
Yes* | - | Recipient ID (chat
id, openid, user_id, or email) |
|
--type | No |
chat_id | Recipient type:
chat_id,
open_id,
user_id,
email |
|
--env | No |
false | Get receive_id from
OPENCLAW_CHAT_ID env var |
*Not required if --env is used
Receive ID Types
| Type | Example | Use Case |
|---|
| INLINECODE16 | INLINECODE17 | Group chats (default) |
| INLINECODE18 |
ou_b0f83ea276761ab10ebb3f4f277453b8 | Individual user (recommended) |
|
user_id |
123456 | Individual user (by user_id) |
|
email |
user@example.com | Send to email address |
Extract Chat ID from Feishu Message
From Feishu message metadata:
{
"conversation_label": "oc_06a6b40e03e98e41c8aebcbed8b09871"
}
→
receive_id =
oc_06a6b40e03e98e41c8aebcbed8b09871,
type = INLINECODE27
From sender info:
{
"sender_id": "ou_b0f83ea276761ab10ebb3f4f277453b8"
}
→
receive_id =
ou_b0f83ea276761ab10ebb3f4f277453b8,
type = INLINECODE31
Prerequisites
- - Python 3.6+
- INLINECODE32 library: INLINECODE33
- Feishu bot credentials in INLINECODE34
Feishu API Permissions
Bot needs these permissions in Feishu Developer Console:
Required for file upload:
- -
im:resource:upload - Upload files (preferred) - OR
im:resource - Read and upload files
Required for sending messages:
- -
im:message - Send messages to chats
How to Enable Permissions
- 1. Open Feishu Developer Console: https://open.feishu.cn/app/YOURAPPID/auth
- Search for
im:resource:upload or INLINECODE39 - Click "Apply" and enable the permission
- Wait a few minutes for permission to take effect
Quick link: https://open.feishu.cn/app/cli_a94a21db99385bd8/auth?q=im:resource:upload,im:resource
File Size Limits
- - Single file: 30MB max (Feishu API limit)
- Large files may take longer to upload
Supported File Types
All file types supported by Feishu Drive:
| Category | Extensions |
|---|
| Documents | PDF, DOC, DOCX, XLS, XLSX, PPT, PPTX |
| Images |
JPG, PNG, GIF, WEBP |
| Archives | ZIP, RAR, 7Z |
| Code/Text | TXT, MD, JSON, CSV, XML, PY, JS |
| Media | MP3, MP4, WAV (auto-converted to OPUS) |
Error Handling
Script handles:
- - Missing credentials → Error message
- File not found → Error message
- Upload failure → API error details
- Send failure → API error details
- Missing permissions → Clear error with fix link
Testing
CODEBLOCK6
Troubleshooting
"Access denied" / "99991672"
- - Bot lacks
im:resource:upload or im:resource permission - Open: https://open.feishu.cn/app/cli_a94a21db99385bd8/auth?q=im:resource:upload,im:resource
- Enable permission and wait a few minutes
"Bot/User can NOT be out of the chat (230002)"
- - Bot is not a member of the target group chat
- Add bot to the chat, or send to a different chat
"Invalid receive_id"
- - Check the ID format matches the type
- INLINECODE42 starts with INLINECODE43
- INLINECODE44 starts with INLINECODE45
- INLINECODE46 is numeric
- INLINECODE47 must be valid email format
Upload fails for large files
- - Feishu API limit is 30MB
- Compress file or use Feishu Drive web interface for larger files
Alternative: Manual Upload
If script fails, user can manually:
- 1. Upload file to Feishu Drive via web/app
- Share file with the chat
- Bot can then reference the file
Security Notes
- - Credentials read from
openclaw.json (local only) - Token expires after ~2 hours (auto-refreshed each run)
- Files uploaded to Feishu Drive follow Feishu's security policies
- Script does not store or transmit credentials
Comparison with Message Tool
| Feature | This Skill | Message Tool INLINECODE49 |
|---|
| Documents (PDF, CSV, etc.) | ✅ Supported | ❌ Not supported |
| Images |
✅ Supported | ✅ Supported (easier) |
| Videos | ✅ Supported | ✅ Supported (easier) |
| File size limit | 30MB | Varies by platform |
| Complexity | 2 API calls | 1 API call |
Recommendation: Use media parameter for images/videos, use this skill for documents and other files.
飞书文件上传
将本地文件上传至飞书云盘,并以文件消息形式发送到群聊或用户。
适用场景
- - 用户要求“发送文件”、“上传文件”或“分享文件”到飞书
- 需要与群组或个人分享文档(CSV、PDF、Excel、Word)
- 机器人需要发送飞书原生消息API不直接支持的文件
- 图片/视频:请使用消息工具中的media参数(无需此技能)
快速开始
发送到群聊
bash
python scripts/upload
tofeishu.py /path/to/file.csv oc
xxxxxx --type chatid
发送给个人用户
bash
python scripts/upload
tofeishu.py /path/to/file.pdf ou
xxxxxx --type openid
通过邮箱发送
bash
python scripts/upload
tofeishu.py /path/to/file.zip user@example.com --type email
使用环境变量
bash
export OPENCLAW
CHATID=oc_xxxxxx
python scripts/upload
tofeishu.py /path/to/file.csv --env
工作原理
- 1. 从~/.openclaw/openclaw.json读取凭证(channels.feishu.appId/appSecret)
- 通过飞书认证API获取租户访问令牌
- 将文件上传至飞书云盘 → 返回filekey
- 使用filekey向目标群聊/用户发送文件消息
脚本使用说明
参数
| 参数 | 必填 | 默认值 | 说明 |
|---|
| filepath | 是 | - | 本地文件的绝对路径 |
| receiveid |
是* | - | 接收方ID(chat
id、openid、user_id或邮箱) |
| --type | 否 | chat
id | 接收方类型:chatid、open
id、userid、email |
| --env | 否 | false | 从OPENCLAW
CHATID环境变量获取receive_id |
*使用--env时可不填
接收方ID类型
| 类型 | 示例 | 使用场景 |
|---|
| chatid | oc06a6b40e03e98e41c8aebcbed8b09871 | 群聊(默认) |
| openid |
oub0f83ea276761ab10ebb3f4f277453b8 | 个人用户(推荐) |
| user_id | 123456 | 个人用户(按用户ID) |
| email | user@example.com | 发送到邮箱地址 |
从飞书消息中提取聊天ID
从飞书消息元数据中:
json
{
conversationlabel: oc06a6b40e03e98e41c8aebcbed8b09871
}
→ receiveid = oc06a6b40e03e98e41c8aebcbed8b09871,type = chat_id
从发送者信息中:
json
{
senderid: oub0f83ea276761ab10ebb3f4f277453b8
}
→ receiveid = oub0f83ea276761ab10ebb3f4f277453b8,type = open_id
前置条件
- - Python 3.6+
- requests库:pip install requests
- openclaw.json中的飞书机器人凭证
飞书API权限
机器人需要在飞书开发者后台启用以下权限:
文件上传所需权限:
- - im:resource:upload - 上传文件(推荐)
- 或 im:resource - 读取和上传文件
发送消息所需权限:
如何启用权限
- 1. 打开飞书开发者后台:https://open.feishu.cn/app/YOURAPPID/auth
- 搜索im:resource:upload或im:resource
- 点击“申请”并启用权限
- 等待几分钟使权限生效
快捷链接: https://open.feishu.cn/app/cli_a94a21db99385bd8/auth?q=im:resource:upload,im:resource
文件大小限制
- - 单个文件:最大30MB(飞书API限制)
- 大文件上传可能需要更长时间
支持的文件类型
飞书云盘支持的所有文件类型:
| 类别 | 扩展名 |
|---|
| 文档 | PDF、DOC、DOCX、XLS、XLSX、PPT、PPTX |
| 图片 |
JPG、PNG、GIF、WEBP |
| 压缩包 | ZIP、RAR、7Z |
| 代码/文本 | TXT、MD、JSON、CSV、XML、PY、JS |
| 媒体 | MP3、MP4、WAV(自动转换为OPUS) |
错误处理
脚本处理以下情况:
- - 缺少凭证 → 错误提示
- 文件未找到 → 错误提示
- 上传失败 → API错误详情
- 发送失败 → API错误详情
- 缺少权限 → 清晰的错误信息及修复链接
测试
bash
使用小文件测试发送到群聊
python scripts/upload
tofeishu.py /tmp/test.txt oc
xxxxxx --type chatid
测试发送给自己(从消息元数据中找到自己的open_id)
python scripts/upload
tofeishu.py /tmp/test.pdf ou
xxxxxx --type openid
故障排除
“访问被拒绝” / “99991672”
- - 机器人缺少im:resource:upload或im:resource权限
- 打开:https://open.feishu.cn/app/cli_a94a21db99385bd8/auth?q=im:resource:upload,im:resource
- 启用权限并等待几分钟
“机器人/用户不在聊天中(230002)”
- - 机器人不是目标群聊的成员
- 将机器人添加到群聊,或发送到其他群聊
“无效的receive_id”
- - 检查ID格式是否与类型匹配
- chatid以oc开头
- openid以ou开头
- user_id为数字
- email必须是有效的邮箱格式
大文件上传失败
- - 飞书API限制为30MB
- 压缩文件或使用飞书云盘网页界面上传更大文件
替代方案:手动上传
如果脚本失败,用户可以手动操作:
- 1. 通过网页/应用将文件上传到飞书云盘
- 与群聊分享文件
- 机器人随后可引用该文件
安全说明
- - 凭证从openclaw.json读取(仅本地)
- 令牌约2小时后过期(每次运行自动刷新)
- 上传到飞书云盘的文件遵循飞书安全策略
- 脚本不会存储或传输凭证
与消息工具对比
| 功能 | 本技能 | 消息工具media |
|---|
| 文档(PDF、CSV等) | ✅ 支持 | ❌ 不支持 |
| 图片 |
✅ 支持 | ✅ 支持(更简单) |
| 视频 | ✅ 支持 | ✅ 支持(更简单) |
| 文件大小限制 | 30MB | 因平台而异 |
| 复杂度 | 2次API调用 | 1次API调用 |
建议: 图片/视频使用media参数,文档和其他文件使用本技能。