通過飛書開放平臺 API 直接傳送圖片到使用者,圖片以內嵌方式顯示,而非檔案附件。
兩步走:
image_keyimage_key 發一條 image 型別訊息curl -s -X POST "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal" \
-H "Content-Type: application/json" \
-d '{"app_id":"<APP_ID>","app_secret":"<APP_SECRET>"}'
響應:{"code":0,"tenant_access_token":"t-xxx","expire":3339}
記錄返回的 tenant_access_token。
curl -s -X POST "https://open.feishu.cn/open-apis/im/v1/images" \
-H "Authorization: Bearer <tenant_access_token>" \
-F "image_type=message" \
-F "image=@/path/to/image.png"
響應:{"code":0,"data":{"image_key":"img_v3_xxx"},"msg":"success"}
記錄返回的 image_key。
curl -s -X POST "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=open_id" \
-H "Authorization: Bearer <tenant_access_token>" \
-H "Content-Type: application/json" \
-d '{
"receive_id": "<open_id>",
"msg_type": "image",
"content": "{\"image_key\":\"<image_key>\"}"
}'
receive_id 可選 open_id(使用者唯標識)、chat_id(群會話)、user_id、union_id。
響應成功:{"code":0,"data":{"message_id":"om_xxx",...}}
#!/bin/bash
# 引數
IMAGE_PATH="$1"
OPEN_ID="$2"
APP_ID="cli_a924632610b8dbd9"
APP_SECRET="c3TXscIJPF1f8jcQ4mJJegNVk72ktbwK"
# 1. 獲取 token
TOKEN=$(curl -s -X POST "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal" \
-H "Content-Type: application/json" \
-d "{\"app_id\":\"$APP_ID\",\"app_secret\":\"$APP_SECRET\"}" | \
python3 -c "import sys,json; print(json.load(sys.stdin)['tenant_access_token'])")
# 2. 上傳圖片
IMAGE_KEY=$(curl -s -X POST "https://open.feishu.cn/open-apis/im/v1/images" \
-H "Authorization: Bearer $TOKEN" \
-F "image_type=message" \
-F "image=@$IMAGE_PATH" | \
python3 -c "import sys,json; print(json.load(sys.stdin)['data']['image_key'])")
# 3. 傳送圖片
curl -s -X POST "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=open_id" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"receive_id\":\"$OPEN_ID\",\"msg_type\":\"image\",\"content\":\"{\\\"image_key\\\":\\\"$IMAGE_KEY\\\"}\"}"
echo "Done: $IMAGE_KEY"
const fs = require('fs'); const path = require('path'); async function feishuSendImage(imagePath, openId, appId, appSecret) { // 1. get token const tokenRes = await fetch('https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ app_id: appId, app_secret: appSecret }) }); const { tenant_access_token } = await tokenRes.json(); // 2. upload image const imageBuffer = fs.readFileSync(imagePath); const uploadRes = await fetch('https://open.feishu.cn/open-apis/im/v1/images', { method: 'POST', headers: { 'Authorization': `Bearer ${tenant_access_token}` }, body: (() => { const form = new FormData(); form.append('image_type', 'message'); form.append('image', new Blob([imageBuffer]), path.basename(imagePath)); return form; })() }); const { data: { image_key } } = await uploadRes.json(); // 3. send image message const sendRes = await fetch(`https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=open_id`, { method: 'POST', headers: { 'Authorization': `Bearer ${tenant_access_token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ receive_id: openId, msg_type: 'image', content: JSON.stringify({ image_key }) }) }); return sendRes.json(); }發現更多技能外掛,請訪問7w4.net。
receive_id_type 必須與 receive_id 匹配(open_id / user_id / chat_id / union_id)這是一個功能完整的飛書圖片傳送技能,文件寫得清晰明白,步驟講解詳細,還提供了現成的程式碼示例,上手比較快。主要問題是程式碼裡直接寫了金鑰憑證,存在安全隱患;另外缺少對 API 調用出錯的處理,網路不好時可能出問題。整體質量中等偏上,功能能用,但安全性和健壯性還需要加強。推薦指數:★★★☆☆