wx-cli
Triggers
- 查微信聊天记录
- 微信消息历史
- 微信联系人
- 微信群成员
- 微信群昵称 / 群名片
- 微信收藏
- wechat history / messages / contacts
- wx-cli
- 帮我看看微信里
- 搜索微信消息
Prerequisites
- macOS(Apple Silicon / Intel)或 Linux
- 微信桌面版 4.x 已安装并登录
- Node.js >= 14(npm 安装方式)或 curl(shell 安装方式)
- 首次
wx init 需要 sudo(内存扫描提取密钥)
安装
方式一:npm(推荐)
npm install -g @jackwener/wx-cli
方式二:curl
curl -fsSL https://raw.githubusercontent.com/jackwener/wx-cli/main/install.sh | bash
安装后验证:
wx --version
初始化(首次使用,只需一次)
macOS(必须按顺序执行)
第一步:对 WeChat 重新签名(只需做一次,WeChat 更新后需重做)
codesign --force --deep --sign - /Applications/WeChat.app
如果报错 signature in use 或某个 dylib 签名损坏,先修复再签名:
codesign --remove-signature "/Applications/WeChat.app/Contents/Frameworks/vlc_plugins/librtp_mpeg4_plugin.dylib"
codesign --force --deep --sign - /Applications/WeChat.app
第二步:清理 WeChat 在 macOS TCC 隐私数据库里的旧授权记录(重签名后必做)
macOS TCC 按 bundle id + csreq 联合校验权限;csreq 编码自代码签名。重签名后旧 csreq 和新签名不再匹配,旧授权记录会 silent 失效(System Settings 仍把开关画成"已允许",运行时实际拒绝)。把 WeChat 在 TCC 里的旧记录抹掉,让 macOS 在下次微信请求权限时按新签名重新生成 csreq:
tccutil reset ScreenCapture com.tencent.xinWeChat # 截图 / 屏幕共享
tccutil reset Camera com.tencent.xinWeChat # 视频通话 / 扫码
tccutil reset Microphone com.tencent.xinWeChat # 语音消息 / 通话
tccutil reset AppleEvents com.tencent.xinWeChat # 自动化 / 输入法
tccutil reset AddressBook com.tencent.xinWeChat # 通讯录
tccutil reset SystemPolicyDocumentsFolder com.tencent.xinWeChat
tccutil reset SystemPolicyDownloadsFolder com.tencent.xinWeChat
tccutil reset SystemPolicyDesktopFolder com.tencent.xinWeChat
tccutil 对没有授权过的 service 会报 "No such bundle identifier",是 no-op,不影响其他 service 的 reset。
第三步:重启 WeChat
killall WeChat && open /Applications/WeChat.app
# 等待微信完全登录后再继续
之后微信触发权限请求时按 GUI 提示重新允许即可。在 macOS 26 上,把 WeChat 加进 隐私与安全 → 录屏与系统录音 的上半区,不要只勾下半区的"仅系统录音"——后者不能授予截图权限。
第四步:初始化
sudo wx init
Linux
sudo wx init
wx init 会自动:
- 检测微信数据目录
- 扫描进程内存,提取所有数据库密钥
- 写入
~/.wx-cli/config.json
初始化完成后,后续所有命令无需 sudo,daemon 在首次调用时自动启动。
命令速查
所有命令默认输出 YAML,更省 token & 易读;--json 可切换为 JSON(方便 jq 处理等)。
会话与消息
# 最近 20 个会话
wx sessions
# 有未读消息的会话
wx unread
# 只看真人(私聊 + 群聊)的未读,过滤公众号与折叠入口
wx unread --filter private,group
# 上次检查后的新消息(增量)
wx new-messages
wx new-messages --json # JSON 输出,适合 agent 解析
# 聊天记录(支持昵称/备注名)
wx history "张三"
wx history "张三" -n 2000
wx history "AI群" --since 2026-04-01 --until 2026-04-15 -n 100
# 全库搜索
wx search "关键词"
wx search "关键词" -n 500
wx search "会议" --in "工作群" --since 2026-01-01
history / search / export 都支持 -n / --limit 指定返回条数。默认值只是为了避免一次输出过多,不是硬上限。
sessions / unread / history / new-messages / stats 的输出都带 chat_type 字段,agent 可据此分流:
| 取值 |
含义 |
username 特征 |
private |
真人私聊 |
wxid_* 或自定义短号 |
group |
群聊 |
*@chatroom |
official_account |
公众号 / 订阅号 / 服务号 / 系统通知 |
gh_*、biz_*、mphelper、qqsafe、@opencustomerservicemsg |
folded |
折叠入口(订阅号折叠、折叠群聊的聚合条目) |
brandsessionholder、@placeholder_foldgroup |
wx unread --filter 支持 private / group / official / folded / all,逗号分隔多选。默认 all。
群聊消息里的 last_sender、sender 和 stats.top_senders 会优先显示群昵称(群名片)。如果本地数据库没有群昵称,再回退到联系人备注、微信昵称或 username。
sessions / unread / history / search / new-messages / stats / attachments 的 stdout 现在统一是 wrapper:
{
"messages": [...],
"meta": {
"status": "ok",
"unknown_shards": [],
"chat_latest_timestamp": 1715750400,
"chat_latest_db": "message/message_2.db",
"session_last_timestamp": 1715760000
}
}
其中:
status = possibly_stale_unknown_shards:磁盘上出现 daemon 不认识的新 message_N.db,先跑 wx init --force
status = possibly_stale:session.db 记录的最新时间明显领先于本次查到的最新消息,结果可能漏消息
status = windowed:这次查询本来就是窗口化/过滤后的局部视图,不应把它当作"全量最新状态"
--with-meta:额外返回 per_shard_latest / cache_mode_per_shard
--debug-source:在 --with-meta 基础上再暴露真实 shard_paths
引用消息(appmsg type=57)在 history / search / new-messages 输出里会展开为两行:第一行是当前回复,第二行以 ↳ 开头显示被引用原文,例如:
[引用] 当前回复
↳ 发送者: 被引用内容
--type link / --type file 会覆盖微信 appmsg 的链接、文件、合并聊天记录和引用消息等变体;search --type link 也会匹配解压并格式化后的引用原文。
联系人与群组
# 联系人列表 / 搜索
wx contacts
wx contacts --query "李"
# 群成员列表
wx members "AI交流群"
wx members --json 每个成员包含:
username:微信内部 username
display:推荐展示名,优先使用群昵称
contact_display:联系人备注或微信昵称
group_nickname:群昵称;没有记录时为空字符串
is_owner:是否群主
Agent 展示群成员时优先用 display。需要区分群昵称和联系人名时,再读取 group_nickname 与 contact_display。
朋友圈(SNS)
三个命令,作用各不同:
# 1) 互动通知(点赞 / 评论,默认仅未读)
wx sns-notifications
wx sns-notifications --include-read --since 2026-04-01 -n 100
# 2) 时间线:浏览本地缓存的朋友圈帖子
wx sns-feed # 近 20 条
wx sns-feed --user "张三" # 只看某人
wx sns-feed --since 2026-04-01 --until 2026-04-18 -n 100
# 3) 全文搜索:在正文里找关键词
wx sns-search "关键词"
wx sns-search "婚礼" --user "李四" --since 2023-01-01 -n 50
字段区分:
sns-notifications 返回"通知"条目:type(like/comment)、from_nickname、content(评论正文,点赞为空)、feed_preview + feed_author(对应的原帖)
sns-feed / sns-search 返回"帖子"条目:author、content(朋友圈正文)、media、media_count(图片/视频数)、location、timestamp;media 字段含每张图的 url/thumb/key/token/md5/enc_idx/size,供下游做图片代理或离线渲染。media_count = media.len(),按 DOM 解析的合法 <media> 子节点计数(malformed XML 返回 0)
只保存你本地刷到过的朋友圈(微信 app 按需下载)。没刷到过的帖子不在本地,任何命令都拿不到。
公众号文章
公众号的文章推送存在独立的 biz_message_0.db,与普通 message_0.db 分开:
# 最近 50 篇(默认)
wx biz-articles
# 更多
wx biz-articles -n 200
# 限定公众号(名称模糊匹配 display name / username)
wx biz-articles --account "返朴"
# 时间范围(YYYY-MM-DD,发布时间,非接收时间)
wx biz-articles --since 2026-05-01 --until 2026-05-10
# 仅有未读消息的公众号,每号取最新 1 篇(适合"今天有什么新推送"扫描)
wx biz-articles --unread
wx biz-articles --unread --account "Datawhale" # 与 --account 取交集
# 下游消费:拿 URL 做内容抓取
wx biz-articles --since 2026-05-10 --json | jq '.[].url'
每条返回的字段:account / account_username(gh_*)/ title / url(mp.weixin.qq.com 链接)/ digest / cover_url / time + timestamp(文章发布时间)/ recv_time_str + recv_time(微信接收推送的时间)。多图文推送会展开为多行。
附件提取(图片)
聊天里的图片本体在 xwechat_files/<wxid>/msg/attach/... 下加密存储(.dat),需要按消息所在 message_resource.db 的 md5 + 平台相关 image key 才能解码。两步走:
# 1) 先列出图片附件,拿到不透明的 attachment_id
wx attachments "张三"
wx attachments "AI群" --kind image -n 100
wx attachments "AI群" --since 2026-04-01 --until 2026-04-15
# 2) 用 attachment_id 把单个资源解密写到指定路径
wx extract <attachment_id> -o ~/Desktop/photo.jpg
wx extract <attachment_id> -o /tmp/x.jpg --overwrite
attachments 输出每条带:attachment_id / kind(当前固定 image)/ type / local_id / timestamp / time,群聊里另带 sender。命令名保留成 attachments 是为了后续扩到其他附件类型时不 break CLI。
extract 报告里带:md5 / dat_path / dat_size / output / output_size / format(实际识别出的图片格式:jpg / png / gif / webp / hevc 等)/ decoder(实际选用的解码器:legacy_xor / v1_aes / v2)。
支持的解码档位:
- legacy XOR:早期单字节 XOR,无 magic(按文件首字节探测格式自动反推)
- V1 fixed-AES(
07 08 V1 08 07):AES-128-ECB + 固定 key cfcd208495d565ef
- V2 AES + XOR(
07 08 V2 08 07):AES-128-ECB + raw + XOR;AES key 平台派生
V2 image key 提取(macOS / Windows 自动;Linux 暂不支持):
- macOS:
kvcomm cache(key_<uin>_*.statistic 文件名取 uin → md5(str(uin) + wxid)[:16])+ brute-force fallback;xor_key = uin & 0xff
- Windows:扫
Weixin.exe 内存匹配 [A-Za-z0-9]{32|16} 候选,按 V2 template ciphertext-block 反验
收藏与统计
# 全部收藏
wx favorites
# 按类型筛选:text / image / article / card / video
wx favorites --type image
# 搜索收藏内容
wx favorites --query "关键词"
# 聊天统计(发言人、消息类型、活跃时段)
wx stats "AI群"
wx stats "AI群" --since 2026-01-01
导出
# 导出为 Markdown(默认)
wx export "张三" --format markdown -o chat.md
wx export "张三" -n 2000 --format markdown -o chat.md
# 导出为 JSON
wx export "AI群" --since 2026-01-01 --format json -o chat.json
Daemon 管理
wx daemon status
wx daemon stop
wx daemon logs --follow
Agent 使用建议
查询结果需要程序处理时,统一加 --json:
wx sessions --json
wx new-messages --json
wx search "关键词" --json | jq '.results[0]'
wx history "张三" --json -n 50 | jq '.messages[0]'
wx history "张三" --json | jq '.meta'
wx history "张三" --json --with-meta | jq '.meta.cache_mode_per_shard'
CHAT 参数支持昵称、备注名、微信 ID,模糊匹配。不确定准确名称时,先用 wx contacts --query 搜索。
数据文件位置
~/.wx-cli/
├── config.json # 配置
├── all_keys.json # 数据库密钥(敏感,勿分享)
├── daemon.sock # Unix socket
├── daemon.pid / .log
└── cache/ # 解密后的数据库缓存
常见问题
微信重启后密钥失效:重新运行 sudo wx init --force(微信必须正在运行)。
daemon 无响应:wx daemon stop 后重新调用任意命令自动重启。
找不到聊天:用 wx contacts --query 确认昵称/备注名,或用微信 ID 直接查询。
为什么只能获取 500 条消息?:这是默认输出条数,不是硬限制。显式传 -n 即可,例如 wx history "张三" -n 2000 或 wx export "张三" -n 2000 -o chat.md。
Ver el texto completo de la licencia (Apache-2.0)
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.