From 69a2f442405d35393bec4b807540af4782cfa6dc Mon Sep 17 00:00:00 2001 From: ylytdeng Date: Thu, 9 Apr 2026 11:43:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20/api/history=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=8C=89=E7=BE=A4=E8=BF=87=E6=BB=A4=E5=92=8C=E5=A2=9E=E9=87=8F?= =?UTF-8?q?=E6=8B=89=E5=8F=96=EF=BC=8C=E6=9B=B4=E6=96=B0=20README=20API=20?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - /api/history 新增 chat、since、limit 参数 - README 新增 HTTP API 端点说明和联系人标签工具文档 Co-Authored-By: Claude Opus 4.6 (1M context) --- README.md | 16 ++++++++++++++++ monitor_web.py | 24 +++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5cefe36..8279fae 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,20 @@ Linux 版 `config.json` 示例: - 总延迟约 100ms - **图片消息内联预览**(支持旧 XOR / V1 / V2 三种 .dat 加密格式) +#### HTTP API + +| 端点 | 说明 | +|------|------| +| `GET /api/history` | 最近消息列表 (JSON) | +| `GET /api/history?chat=群名` | 按群名/用户名过滤消息 | +| `GET /api/history?since=1712000000` | 增量拉取(返回该时间戳之后的消息) | +| `GET /api/history?chat=群名&since=ts&limit=100` | 参数可组合使用 | +| `GET /api/tags` | 所有联系人标签及成员 (JSON) | +| `GET /api/tags?name=同事` | 按标签名过滤 | +| `GET /stream` | SSE 实时消息推送 | + +将特定群消息存到自己的数据库:监听 `/stream` 或轮询 `/api/history?chat=群名&since=上次时间戳`,写入即可。 + ### MCP Server (Claude AI 集成) 将微信数据查询能力接入 [Claude Code](https://claude.ai/claude-code),让 AI 直接读取你的微信消息。 @@ -145,6 +159,8 @@ claude mcp add wechat -- python C:\Users\你的用户名\wechat-decrypt\mcp_serv | `get_chat_history(chat_name, limit, offset, start_time, end_time)` | 指定聊天的消息记录,支持时间范围和分页 | | `search_messages(keyword, chat_name, start_time, end_time, limit, offset)` | 统一搜索消息;支持全库、单个聊天对象、多个聊天对象、时间范围和分页 | | `get_contacts(query, limit)` | 搜索/列出联系人 | +| `get_contact_tags()` | 列出所有联系人标签及成员数量 | +| `get_tag_members(tag_name)` | 获取指定标签下的所有联系人,支持模糊匹配 | | `get_new_messages()` | 获取自上次调用以来的新消息 | 前置条件:需要先运行 `python main.py` 或 `python find_all_keys.py` 完成密钥提取。 diff --git a/monitor_web.py b/monitor_web.py index e4d6397..69b95aa 100644 --- a/monitor_web.py +++ b/monitor_web.py @@ -1905,9 +1905,31 @@ class Handler(BaseHTTPRequestHandler): self.end_headers() self.wfile.write(HTML_PAGE.encode('utf-8')) - elif self.path == '/api/history': + elif self.path.startswith('/api/history'): + parsed = urllib.parse.urlparse(self.path) + params = urllib.parse.parse_qs(parsed.query) + filter_chat = params.get('chat', [''])[0].strip().lower() + since_ts = 0 + try: + since_ts = int(params.get('since', ['0'])[0]) + except (ValueError, TypeError): + pass + limit_val = 500 + try: + limit_val = min(int(params.get('limit', ['500'])[0]), 2000) + except (ValueError, TypeError): + pass + with messages_lock: data = sorted(messages_log, key=lambda m: m.get('timestamp', 0)) + + if since_ts: + data = [m for m in data if m.get('timestamp', 0) > since_ts] + if filter_chat: + data = [m for m in data if filter_chat in m.get('chat', '').lower() + or filter_chat in m.get('username', '').lower()] + data = data[-limit_val:] + self.send_response(200) self.send_header('Content-Type', 'application/json; charset=utf-8') self.end_headers()