diff --git a/skills/find-recent-chat-media/SKILL.md b/skills/find-recent-chat-media/SKILL.md index 3b8dc4d..10fe195 100644 --- a/skills/find-recent-chat-media/SKILL.md +++ b/skills/find-recent-chat-media/SKILL.md @@ -68,7 +68,8 @@ argument-hint: "需要 media_type;可选 count,最多 5。media_type 可为 4. 根据 `media_type` / `media_types` 查找图片、视频、语音,可一次查找多种类型。 5. 最多返回 5 条。脚本会先取最近匹配的 N 条,再按时间升序输出,因此第一条最早,最后一条最晚。 6. 历史消息查询直接读取数据库 `messages` 表,不调用历史消息 HTTP 接口。 -7. 查到消息记录后,脚本必须先调用客户端下载接口下载媒体,再调用客户端上传接口上传到 CDN,最后只把 CDN URL 返回给智能体。 +7. 如果消息记录的 `attachment_url` 不为空,直接使用该 URL。 +8. 如果 `attachment_url` 为空,脚本必须先调用客户端下载接口下载媒体,再调用客户端上传接口上传到 CDN,最后只把 CDN URL 返回给智能体。 ## 执行步骤 @@ -91,8 +92,9 @@ python3 scripts/find_recent_chat_media.py --media_type image --count 1 - `sender_wxid = ROBOT_SENDER_WX_ID` - `created_at` 在最近十分钟内 - `type` 为图片 `3`、语音 `34`、视频 `43` +- `attachment_url` 不为空时直接作为媒体 URL 返回 -脚本会调用以下客户端接口下载和上传媒体: +当 `attachment_url` 为空时,脚本会调用以下客户端接口下载和上传媒体: - 下载图片:`GET http://127.0.0.1:{ROBOT_WECHAT_CLIENT_PORT}/api/v1/robot/chat/image/download?message_id=...` - 下载视频:`GET http://127.0.0.1:{ROBOT_WECHAT_CLIENT_PORT}/api/v1/robot/chat/video/download?message_id=...` diff --git a/skills/find-recent-chat-media/scripts/find_recent_chat_media.py b/skills/find-recent-chat-media/scripts/find_recent_chat_media.py index adbd30c..b94bcd4 100644 --- a/skills/find-recent-chat-media/scripts/find_recent_chat_media.py +++ b/skills/find-recent-chat-media/scripts/find_recent_chat_media.py @@ -296,7 +296,7 @@ def _fetch_history_media_messages( placeholders = ", ".join(["%s"] * len(message_types)) sql = f""" - SELECT id, type, from_wxid, sender_wxid, created_at + SELECT id, type, from_wxid, sender_wxid, attachment_url, created_at FROM messages WHERE from_wxid = %s AND sender_wxid = %s @@ -405,11 +405,6 @@ def main() -> int: sys.stdout.write(f"参数格式错误: {exc}\n") return 1 - client_port = os.environ.get("ROBOT_WECHAT_CLIENT_PORT", "").strip() - if not client_port: - sys.stdout.write("环境变量 ROBOT_WECHAT_CLIENT_PORT 未配置\n") - return 1 - from_wx_id = os.environ.get("ROBOT_FROM_WX_ID", "").strip() if not from_wx_id: sys.stdout.write("环境变量 ROBOT_FROM_WX_ID 未配置\n") @@ -420,7 +415,6 @@ def main() -> int: sys.stdout.write("环境变量 ROBOT_SENDER_WX_ID 未配置\n") return 1 - base_url = _client_base_url(client_port) end_time = int(time.time()) start_time = end_time - HISTORY_MINUTES * 60 @@ -449,13 +443,24 @@ def main() -> int: items: list[dict[str, Any]] = [] urls_by_type: dict[str, list[str]] = {"image": [], "video": [], "voice": []} try: + client_port = "" + base_url = "" for message in selected_messages: message_id = _to_int(message.get("id")) media_type = str(message.get("media_type") or "") if message_id <= 0 or media_type not in MEDIA_MESSAGE_TYPES: continue - data, filename, content_type, extension = _download_media(base_url, message_id, media_type) - media_url = _upload_media(base_url, message_id, media_type, data, filename, content_type, extension) + + media_url = str(message.get("attachment_url") or "").strip() + if not media_url: + if not client_port: + client_port = os.environ.get("ROBOT_WECHAT_CLIENT_PORT", "").strip() + if not client_port: + raise RuntimeError("环境变量 ROBOT_WECHAT_CLIENT_PORT 未配置") + base_url = _client_base_url(client_port) + data, filename, content_type, extension = _download_media(base_url, message_id, media_type) + media_url = _upload_media(base_url, message_id, media_type, data, filename, content_type, extension) + urls_by_type[media_type].append(media_url) items.append( {