$npx -y skills add kweaver-ai/kweaver-dip --skill feishu-push向飞书指定用户(通过 user_id)推送消息。 支持文本、卡片、图片、文件等多种消息类型。当用户需要给飞书用户发送推送消息时自动使用。
| 1 | # Feishu Push Message Skill |
| 2 | |
| 3 | 通过飞书开放平台 API 向指定飞书用户推送消息。 |
| 4 | |
| 5 | ## 前提条件 |
| 6 | |
| 7 | 1. 需要已经创建飞书自定义应用,并启用机器人能力 |
| 8 | 2. 应用已获取 `im:message`(或 `im:message:send_as_bot`)权限 |
| 9 | 3. 目标用户需要在应用的可用范围内 |
| 10 | 4. 需要配置以下环境变量或凭据: |
| 11 | - `FEISHU_APP_ID`: 飞书应用 App ID (如 `cli_a94a1d897cb85cbb`) |
| 12 | - `FEISHU_APP_SECRET`: 飞书应用 App Secret |
| 13 | |
| 14 | ## API 端点 |
| 15 | |
| 16 | ``` |
| 17 | POST https://open.feishu.cn/open-apis/im/v1/messages |
| 18 | ``` |
| 19 | |
| 20 | ## 使用方式 |
| 21 | |
| 22 | ### 命令行方式(curl) |
| 23 | |
| 24 | ```bash |
| 25 | # 获取 tenant_access_token |
| 26 | TOKEN=$(curl -X POST "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal" \ |
| 27 | -H "Content-Type: application/json; charset=utf-8" \ |
| 28 | -d "{\"app_id\":\"$FEISHU_APP_ID\",\"app_secret\":\"$FEISHU_APP_SECRET\"}" | \ |
| 29 | node -e "let s=''; process.stdin.on('data', c => s += c); process.stdin.on('end', () => console.log(JSON.parse(s).tenant_access_token));") |
| 30 | |
| 31 | # 发送文本消息 |
| 32 | curl --request POST "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=user_id" \ |
| 33 | --header "Authorization: Bearer $TOKEN" \ |
| 34 | --header "Content-Type: application/json; charset=utf-8" \ |
| 35 | --data-raw '{ |
| 36 | "receive_id": "<USER_ID>", |
| 37 | "msg_type": "text", |
| 38 | "content": "{\"text\":\"你的消息内容\"}" |
| 39 | }' |
| 40 | ``` |
| 41 | |
| 42 | ### 接收者 ID 类型 |
| 43 | |
| 44 | 当前技能只接收飞书租户内用户 ID,即 `receive_id_type=user_id`。调用时传入的第一个参数必须是 `user_id`。 |
| 45 | |
| 46 | ### 支持的消息类型 |
| 47 | |
| 48 | | msg_type | 说明 | |
| 49 | |----------|------| |
| 50 | | `text` | 文本消息 | |
| 51 | | `post` | 富文本消息 | |
| 52 | | `image` | 图片消息(需要先上传图片获取 key)| |
| 53 | | `file` | 文件消息(需要先上传文件获取 key)| |
| 54 | | `audio` | 音频消息 | |
| 55 | | `media` | 视频消息 | |
| 56 | | `sticker` | 表情 | |
| 57 | | `interactive` | 互动卡片 | |
| 58 | | `share_chat` | 分享群名片 | |
| 59 | | `share_user` | 分享个人名片 | |
| 60 | | `system` | 系统消息(仅单会话有效)| |
| 61 | |
| 62 | ## 示例 |
| 63 | |
| 64 | ### 发送文本消息给指定 user_id 用户 |
| 65 | |
| 66 | ```bash |
| 67 | # 用法: feishu-push text <user_id> "你的消息内容" |
| 68 | ``` |
| 69 | |
| 70 | ### 环境变量配置 |
| 71 | |
| 72 | 推荐在环境中配置应用凭证: |
| 73 | ```bash |
| 74 | export FEISHU_APP_ID="cli_a94a1d897cb85cbb" |
| 75 | export FEISHU_APP_SECRET="your_app_secret_here" |
| 76 | ``` |
| 77 | |
| 78 | ## 错误处理 |
| 79 | |
| 80 | 常见错误码及处理: |
| 81 | |
| 82 | - `230013`: Bot 对该用户无可用权限 → 将用户添加到应用可用范围 |
| 83 | - `230006`: 未启用机器人能力 → 在飞书开发者后台启用 bot 能力 |
| 84 | - `230029`: 用户已离职 → 无法发送给已离职用户 |
| 85 | - `230053`: 用户停止接收机器人消息 → 用户需要取消拉黑 |
| 86 | |
| 87 | ## NodeJS 示例 |
| 88 | |
| 89 | ```js |
| 90 | const tokenUrl = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"; |
| 91 | const messageUrl = "https://open.feishu.cn/open-apis/im/v1/messages"; |
| 92 | |
| 93 | async function postJson(url, payload, headers = {}) { |
| 94 | const response = await fetch(url, { |
| 95 | method: "POST", |
| 96 | headers: { |
| 97 | "Content-Type": "application/json; charset=utf-8", |
| 98 | ...headers, |
| 99 | }, |
| 100 | body: JSON.stringify(payload), |
| 101 | }); |
| 102 | return response.json(); |
| 103 | } |
| 104 | |
| 105 | async function getTenantAccessToken() { |
| 106 | const data = await postJson(tokenUrl, { |
| 107 | app_id: process.env.FEISHU_APP_ID, |
| 108 | app_secret: process.env.FEISHU_APP_SECRET, |
| 109 | }); |
| 110 | |
| 111 | if (data.code !== 0) { |
| 112 | throw new Error(`Failed to get token: ${data.msg}`); |
| 113 | } |
| 114 | |
| 115 | return data.tenant_access_token; |
| 116 | } |
| 117 | |
| 118 | async function sendTextMessage(userId, content) { |
| 119 | const token = await getTenantAccessToken(); |
| 120 | |
| 121 | return postJson( |
| 122 | `${messageUrl}?receive_id_type=user_id`, |
| 123 | { |
| 124 | receive_id: userId, |
| 125 | msg_type: "text", |
| 126 | content: JSON.stringify({ text: content }), |
| 127 | }, |
| 128 | { |
| 129 | Authorization: `Bearer ${token}`, |
| 130 | }, |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | const [, , userId, message] = process.argv; |
| 135 | if (userId && message) { |
| 136 | const result = await sendTextMessage(userId, message); |
| 137 | console.log(JSON.stringify(result, null, 2)); |
| 138 | } |
| 139 | ``` |
| 140 | |
| 141 | ## 调用示例 |
| 142 | |
| 143 | ``` |
| 144 | /feishu-push 给用户 7g9f6e2d 发送文本消息 "Hello from OpenClaw!" |
| 145 | /feishu-push send user_id 7g9f6e2d "这是一条测试推送消息" |
| 146 | ``` |