asemxin commited on
Commit ·
59fe96c
1
Parent(s): 278f9aa
fix: use messages/resources API to download user images
Browse files- image_daemon.py +8 -6
image_daemon.py
CHANGED
|
@@ -101,14 +101,16 @@ def extract_image_keys(msg):
|
|
| 101 |
keys.append(k)
|
| 102 |
return keys
|
| 103 |
|
| 104 |
-
def download_image(token,
|
|
|
|
| 105 |
headers = {"Authorization": f"Bearer {token}"}
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 108 |
if resp.status_code == 200 and len(resp.content) > 100:
|
| 109 |
log(f"✅ 下载成功: {len(resp.content)} bytes")
|
| 110 |
return resp.content
|
| 111 |
-
log(f"❌ 下载图片失败 {
|
| 112 |
log(f" 需要权限: im:resource")
|
| 113 |
return None
|
| 114 |
|
|
@@ -176,8 +178,8 @@ def process_message(token, chat_id, msg):
|
|
| 176 |
log(f"🖼️ 发现 {len(image_keys)} 张图片 (msg_id={msg_id[:16]}...)")
|
| 177 |
urls = []
|
| 178 |
for key in image_keys[:3]:
|
| 179 |
-
log(f"📥 下载图片 {key}")
|
| 180 |
-
data = download_image(token, key)
|
| 181 |
if data:
|
| 182 |
log(f"📥 {len(data)} bytes, 上传中...")
|
| 183 |
url = upload_image(data)
|
|
|
|
| 101 |
keys.append(k)
|
| 102 |
return keys
|
| 103 |
|
| 104 |
+
def download_image(token, message_id, file_key):
|
| 105 |
+
"""通过 获取消息中的资源文件 API 下载用户发送的图片"""
|
| 106 |
headers = {"Authorization": f"Bearer {token}"}
|
| 107 |
+
url = f"{FEISHU_BASE}/im/v1/messages/{message_id}/resources/{file_key}"
|
| 108 |
+
log(f"📥 API: GET {url}?type=image")
|
| 109 |
+
resp = requests.get(url, headers=headers, params={"type": "image"}, timeout=30)
|
| 110 |
if resp.status_code == 200 and len(resp.content) > 100:
|
| 111 |
log(f"✅ 下载成功: {len(resp.content)} bytes")
|
| 112 |
return resp.content
|
| 113 |
+
log(f"❌ 下载图片失败 {file_key}: HTTP {resp.status_code}, {resp.text[:200]}")
|
| 114 |
log(f" 需要权限: im:resource")
|
| 115 |
return None
|
| 116 |
|
|
|
|
| 178 |
log(f"🖼️ 发现 {len(image_keys)} 张图片 (msg_id={msg_id[:16]}...)")
|
| 179 |
urls = []
|
| 180 |
for key in image_keys[:3]:
|
| 181 |
+
log(f"📥 下载图片 {key} (msg={msg_id[:16]}...)")
|
| 182 |
+
data = download_image(token, msg_id, key)
|
| 183 |
if data:
|
| 184 |
log(f"📥 {len(data)} bytes, 上传中...")
|
| 185 |
url = upload_image(data)
|