参考微信官方openclaw-weixin源码实现媒体内联发送链路:下载媒体→getuploadurl→AES-128-ECB加密→上传微信CDN→内联发送(type=2/5)。新增im_ilink_media.py模块。失败降级文本链接兜底。修复pg18/casdoor容器未启动问题。
This commit is contained in:
1 parent
894eeff65a
commit
46e9f1e1e6
2 files changed
+248
-11
No files matched your search
@@ -319,21 +319,44 @@ def _platform_relay_url(raw_url: str) -> str:
|
||||
|
||||
def _wechat_send_media(baseurl: str, bot_token: str, to_user_id: str, context_token: str,
|
||||
media_type: str, url: str):
|
||||
"""发送媒体(图片/视频)。
|
||||
"""发送媒体(图片/视频)。#969 iLink CDN 内联发送
|
||||
|
||||
问题:AGNES 媒体 URL 位于境外,iLink(中国大陆服务器)直连常被墙/超时。
|
||||
解决:改写为平台中转地址(经香港代理可达 AGNES)。
|
||||
|
||||
2026-07-31 兼容修复:iLink 对 type=2/5 的媒体 item 返回 ret=-2 invalid arguments,
|
||||
且旧代码把 ret=-2 误判为 token 失效,导致连接假性断开。先以纯文本链接形式发送,
|
||||
确保用户可点击访问且不会破坏会话;待拿到 iLink 媒体协议文档后再恢复内联 item。
|
||||
优先走 iLink 官方 CDN 加密上传链路,实现图片/视频真正内联显示;
|
||||
失败时降级为纯文本链接(点击访问),确保会话不中断。
|
||||
"""
|
||||
relay_url = _platform_relay_url(url)
|
||||
# 尝试内联发送(iLink CDN 加密上传)
|
||||
try:
|
||||
with open("/tmp/wechat_send_debug.log", "a", encoding="utf-8") as _f:
|
||||
_f.write(_beijing_now().isoformat() + " MEDIA_URL_LEN=%s type=%s\n" % (len(relay_url), media_type))
|
||||
except Exception:
|
||||
pass
|
||||
from app.api.im_ilink_media import (
|
||||
MEDIA_TYPE_IMAGE, MEDIA_TYPE_VIDEO,
|
||||
download_media_bytes, upload_media_to_wechat_cdn,
|
||||
build_image_item, build_video_item,
|
||||
)
|
||||
media_bytes = download_media_bytes(url)
|
||||
mt = MEDIA_TYPE_IMAGE if media_type == "image" else MEDIA_TYPE_VIDEO
|
||||
uploaded = upload_media_to_wechat_cdn(baseurl, bot_token, to_user_id, media_bytes, mt)
|
||||
item = build_image_item(uploaded) if media_type == "image" else build_video_item(uploaded)
|
||||
ok, ret, _ = _wechat_post_msg(baseurl, bot_token, to_user_id, context_token, [item])
|
||||
if ok:
|
||||
try:
|
||||
with open("/tmp/wechat_send_debug.log", "a", encoding="utf-8") as _f:
|
||||
_f.write(_beijing_now().isoformat() + " MEDIA_INLINE_OK type=%s size=%d\n"
|
||||
% (media_type, len(media_bytes)))
|
||||
except Exception:
|
||||
pass
|
||||
return True, ret, {}
|
||||
try:
|
||||
with open("/tmp/wechat_send_debug.log", "a", encoding="utf-8") as _f:
|
||||
_f.write(_beijing_now().isoformat() + " MEDIA_INLINE_FAIL ret=%s → fallback text\n" % (ret,))
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as _e:
|
||||
try:
|
||||
with open("/tmp/wechat_send_debug.log", "a", encoding="utf-8") as _f:
|
||||
_f.write(_beijing_now().isoformat() + " MEDIA_INLINE_EXC %s → fallback text\n" % (_e,))
|
||||
except Exception:
|
||||
pass
|
||||
# 降级:纯文本链接
|
||||
if media_type == "image":
|
||||
caption = "🖼️ 图片已生成,点击查看:" + relay_url
|
||||
else:
|
||||
|
||||
Reference in new issue
Block a user