Spaces:
Running
Running
asemxin commited on
Commit ·
4dca48a
1
Parent(s): d892f44
fix: 改用 send_message 直发消息,优化格式
Browse files- image_daemon.py +12 -8
image_daemon.py
CHANGED
|
@@ -124,27 +124,30 @@ def upload_image(data):
|
|
| 124 |
|
| 125 |
return None
|
| 126 |
|
| 127 |
-
# ----------
|
| 128 |
-
def
|
| 129 |
headers = {
|
| 130 |
"Authorization": f"Bearer {token}",
|
| 131 |
"Content-Type": "application/json; charset=utf-8"
|
| 132 |
}
|
| 133 |
-
|
|
|
|
| 134 |
headers=headers,
|
|
|
|
| 135 |
json={
|
|
|
|
| 136 |
"content": json.dumps({"text": text}),
|
| 137 |
"msg_type": "text"
|
| 138 |
}, timeout=10)
|
| 139 |
data = resp.json()
|
| 140 |
code = data.get("code", -1)
|
| 141 |
if code != 0:
|
| 142 |
-
log(f"❌
|
| 143 |
return data
|
| 144 |
|
| 145 |
# ---------- 处理图片消息 ----------
|
| 146 |
def handle_image_message(message_id, chat_id, image_key):
|
| 147 |
-
"""下载 → 上传 →
|
| 148 |
token = get_token()
|
| 149 |
if not token:
|
| 150 |
log("❌ 无法获取 token,跳过")
|
|
@@ -163,9 +166,10 @@ def handle_image_message(message_id, chat_id, image_key):
|
|
| 163 |
url = upload_image(img_data)
|
| 164 |
if url:
|
| 165 |
log(f"✅ {url}")
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
| 169 |
else:
|
| 170 |
path = f"/tmp/{image_key[:30]}.jpg"
|
| 171 |
with open(path, "wb") as f:
|
|
|
|
| 124 |
|
| 125 |
return None
|
| 126 |
|
| 127 |
+
# ---------- 发送消息 (直接发到 Chat) ----------
|
| 128 |
+
def send_text(token, chat_id, text):
|
| 129 |
headers = {
|
| 130 |
"Authorization": f"Bearer {token}",
|
| 131 |
"Content-Type": "application/json; charset=utf-8"
|
| 132 |
}
|
| 133 |
+
# 使用 post 消息类型可以支持富文本,这里先用 text 简单点,但直接发到 chat_id
|
| 134 |
+
resp = requests.post(f"{FEISHU_BASE}/im/v1/messages",
|
| 135 |
headers=headers,
|
| 136 |
+
params={"receive_id_type": "chat_id"},
|
| 137 |
json={
|
| 138 |
+
"receive_id": chat_id,
|
| 139 |
"content": json.dumps({"text": text}),
|
| 140 |
"msg_type": "text"
|
| 141 |
}, timeout=10)
|
| 142 |
data = resp.json()
|
| 143 |
code = data.get("code", -1)
|
| 144 |
if code != 0:
|
| 145 |
+
log(f"❌ 发送消息失败 (code={code}): {data.get('msg', '')}")
|
| 146 |
return data
|
| 147 |
|
| 148 |
# ---------- 处理图片消息 ----------
|
| 149 |
def handle_image_message(message_id, chat_id, image_key):
|
| 150 |
+
"""下载 → 上传 → 发送"""
|
| 151 |
token = get_token()
|
| 152 |
if not token:
|
| 153 |
log("❌ 无法获取 token,跳过")
|
|
|
|
| 166 |
url = upload_image(img_data)
|
| 167 |
if url:
|
| 168 |
log(f"✅ {url}")
|
| 169 |
+
# 改为直接发送消息,更显眼
|
| 170 |
+
reply = f"✅ 图片已转存:\n{url}"
|
| 171 |
+
result = send_text(token, chat_id, reply)
|
| 172 |
+
log(f"📤 已发送 (code={result.get('code', '?')})")
|
| 173 |
else:
|
| 174 |
path = f"/tmp/{image_key[:30]}.jpg"
|
| 175 |
with open(path, "wb") as f:
|