wxautox4 - WeChat自动化工具
wxautox4 是一个专门为 WeChat 4.0.5 版本设计的 Python 自动化库,提供完整的微信操作接口,包括消息发送、文件传输、朋友圈发布等功能。
⚠️ 重要声明
当前为 4.0 Beta 版本,仅完成部分功能,暂不保证稳定性,仅适用于微信 4.0.5 版本客户端
✨ 主要特性
- 🚀 高性能: 使用 Nuitka 编译核心模块,提供原生性能
- 🔧 易用性: 简洁的 API 设计,快速上手
- 🎯 精确控制: 基于 Windows UI 自动化,操作精确可靠
- 🔄 多线程: 支持多线程消息监听和处理
- 📱 朋友圈: 支持朋友圈发布和隐私设置
- 🔐 授权系统: 内置许可证验证机制
- 🐍 多版本支持: 兼容 Python 3.9-3.13
📦 安装方式
使用 pip 安装(推荐)
pip install wxautox4
从源码安装
git clone https://github.com/your-repo/wxautox4.git
cd wxautox4
pip install -e .
🚀 快速开始
基础使用
from wxautox4 import WeChat
# 创建微信实例
wx = WeChat()
# 发送消息
wx.SendMsg('你好,世界!', '好友昵称')
# 发送文件
wx.SendFiles(r'C:\path\to\file.txt', '好友昵称')
# 获取消息
messages = wx.GetAllMessage()
for msg in messages:
print(f'{msg.sender}: {msg.content}')
许可证授权
使用前需要进行授权验证:
# 使用授权码授权
wxautox4 --auth your_license_key
# 使用授权文件授权
wxautox4 --auth-file license.dat
# 导出机器码用于授权
wxautox4 --export
# 调试许可证信息
wxautox4 --debug-license
📚 详细文档
1. 获取微信实例
from wxautox4 import WeChat
# 创建微信主窗口实例
wx = WeChat()
# 检查是否在线
if wx.IsOnline():
print('微信已登录')
else:
print('微信未登录')
2. 发送消息 - SendMsg
# 基础消息发送
wx.SendMsg('Hello, World!', '目标用户')
# 带@功能的消息发送(群聊)
wx.SendMsg('大家好!', '群聊名称', at=['用户1', '用户2'])
# 精确匹配用户名
wx.SendMsg('消息内容', '用户名', exact=True)
# 发送后不清空输入框
wx.SendMsg('消息内容', '用户名', clear=False)
参数说明:
msg(str): 消息内容who(str, optional): 发送对象,不指定则发送给当前聊天对象clear(bool, optional): 发送后是否清空编辑框,默认 Trueat(Union[str, List[str]], optional): @对象,支持字符串或列表exact(bool, optional): 是否精确匹配用户名,默认 False
3. 发送文件 - SendFiles
# 发送单个文件
wx.SendFiles(r'C:\path\to\file.txt', '目标用户')
# 发送多个文件
files = [
r'C:\path\to\file1.txt',
r'C:\path\to\file2.jpg',
r'C:\path\to\file3.pdf'
]
wx.SendFiles(files, '目标用户')
# 向当前聊天窗口发送文件
wx.SendFiles(r'C:\path\to\file.txt')
参数说明:
filepath(str|list): 文件的绝对路径,支持单个文件或文件列表who(str, optional): 发送对象,不指定则发送给当前聊天对象exact(bool, optional): 是否精确匹配用户名,默认 False
4. 获取消息 - GetAllMessage
# 获取当前聊天窗口的所有消息
all_messages = wx.GetAllMessage()
# 获取新消息
new_messages = wx.GetNewMessage()
# 遍历消息
for message in all_messages:
print(f'发送者: {message.sender}')
print(f'内容: {message.content}')
print(f'时间: {message.time}')
print(f'消息类型: {message.type}')
print('-' * 30)
返回值:
List[Message]: 消息列表,每个消息对象包含发送者、内容、时间、类型等信息
5. 监听消息 - AddListenChat
def message_callback(msg, chat):
"""消息回调函数"""
print(f'收到来自 {chat} 的消息: {msg.content}')
# 自动回复
if msg.content == 'hello':
chat.SendMsg('Hello! 我是机器人')
# 添加消息监听
wx.AddListenChat('好友昵称', message_callback)
# 监听多个聊天
wx.AddListenChat(['好友1', '群聊1'], message_callback)
# 开始监听(如果未开始)
wx._listener_start()
参数说明:
who(str|List[str]): 监听对象,支持单个或多个callback(Callable): 回调函数,接收(msg, chat)两个参数
6. 移除监听 - RemoveListenChat
# 移除特定对象的监听
wx.RemoveListenChat('好友昵称')
# 移除多个监听
wx.RemoveListenChat(['好友1', '群聊1'])
# 停止所有监听
wx.StopListening()
7. @所有人 - AtAll
# 在群聊中@所有人
wx.AtAll('重要通知:明天开会!', '工作群')
# 在当前聊天窗口@所有人
wx.AtAll('大家注意!')
参数说明:
msg(str): 要发送的消息who(str, optional): 群聊名称,不指定则在当前聊天窗口操作exact(bool, optional): 是否精确匹配群聊名称
8. 判断是否在线 - IsOnline
# 检查微信是否在线
if wx.IsOnline():
print('微信已登录,可以进行操作')
else:
print('微信未登录,请先登录')
# 在发送消息前检查
if wx.IsOnline():
wx.SendMsg('测试消息', '好友')
else:
print('微信离线,无法发送消息')
9. 切换聊天窗口 - ChatWith
# 切换到指定聊天窗口
wx.ChatWith('好友昵称')
# 精确匹配用户名
wx.ChatWith('用户名', exact=True)
# 切换后发送消息
wx.ChatWith('工作群')
wx.SendMsg('切换成功!')
参数说明:
who(str): 要切换到的聊天对象exact(bool, optional): 是否精确匹配名称
10. 获取子窗口实例 - GetSubWindow
# 获取指定聊天的子窗口
chat_window = wx.GetSubWindow('好友昵称')
# 通过子窗口发送消息(不会切换主窗口)
chat_window.SendMsg('这是通过子窗口发送的消息')
# 获取子窗口信息
info = chat_window.ChatInfo()
print(f'聊天对象: {info["chat_name"]}')
# 关闭子窗口
chat_window.Close()
11. 获取所有子窗口实例 - GetAllSubWindow
# 获取所有打开的子窗口
all_windows = wx.GetAllSubWindow()
for window in all_windows:
print(f'窗口: {window.who}')
# 可以对每个窗口进行操作
window.SendMsg('批量消息发送')
# 关闭所有子窗口
for window in all_windows:
window.Close()
12. 停止监听 - StopListening
# 停止所有消息监听
wx.StopListening()
# 程序结束前建议停止监听
try:
wx.SendMsg('程序即将结束', '管理员')
finally:
wx.StopListening()
13. 发送朋友圈 - PublishMoment
# 发送纯文本朋友圈
text = '''今天天气真好☀️
适合出去走走
心情不错~😊'''
wx.PublishMoment(text)
# 发送带图片的朋友圈
media_files = [
r"C:\Users\用户名\Pictures\photo1.jpg",
r"C:\Users\用户名\Pictures\photo2.jpg",
r"C:\Users\用户名\Pictures\photo3.jpg",
]
wx.PublishMoment(text, media_files)
# 设置隐私权限的朋友圈
privacy_config = {
'privacy': '白名单', # 权限类型:公开/私密/白名单/黑名单
'tags': ['家人', '朋友'] # 标签列表
}
wx.PublishMoment(text, media_files, privacy_config)
# 仅对特定好友可见
privacy_config = {
'privacy': '白名单',
'friends': ['张三', '李四'], # 指定好友
'tags': ['同事'] # 指定标签
}
wx.PublishMoment('工作总结', [], privacy_config)
参数说明:
text(str): 朋友圈文字内容media_files(List[str], optional): 图片/视频文件路径列表privacy_config(dict, optional): 隐私设置配置
隐私配置选项:
privacy: 权限类型('公开', '私密', '白名单', '黑名单')friends: 特定好友列表tags: 特定标签列表
🔧 高级功能
消息过滤与处理
def advanced_message_handler(msg, chat):
"""高级消息处理器"""
# 根据消息类型处理
if msg.type == 'text':
if '帮助' in msg.content:
chat.SendMsg('我是自动回复机器人,可以帮助您处理消息')
elif msg.type == 'image':
chat.SendMsg('收到图片,正在处理...')
elif msg.type == 'file':
chat.SendMsg('收到文件,已保存')
wx.AddListenChat('客服群', advanced_message_handler)
批量操作
# 批量发送消息
contacts = ['好友1', '好友2', '好友3']
message = '群发消息测试'
for contact in contacts:
result = wx.SendMsg(message, contact)
if result:
print(f'发送给 {contact} 成功')
else:
print(f'发送给 {contact} 失败: {result["msg"]}')
# 批量发送文件
file_path = r'C:\Users\用户名\Documents\重要文件.pdf'
for contact in contacts:
wx.SendFiles(file_path, contact)
定时任务
import time
import threading
def scheduled_message():
"""定时发送消息"""
while True:
current_time = time.strftime('%H:%M')
if current_time == '09:00': # 每天9点发送
wx.SendMsg('早上好!新的一天开始了!', '工作群')
time.sleep(60) # 避免重复发送
time.sleep(30) # 每30秒检查一次
# 启动定时任务
scheduler_thread = threading.Thread(target=scheduled_message, daemon=True)
scheduler_thread.start()
🏗️ 开发指南
项目结构
wxautox4/
├── wxautox4/
│ ├── __init__.py # 主要导出
│ ├── wx.py # 核心WeChat类
│ ├── ui/ # UI自动化模块
│ │ ├── main.py # 主窗口管理
│ │ ├── chatbox.py # 聊天窗口
│ │ ├── moment.py # 朋友圈功能
│ │ └── ...
│ ├── uia/ # Windows UI自动化
│ ├── msgs/ # 消息处理
│ ├── utils/ # 工具函数
│ └── ...
├── build_config.json # 构建配置
├── thread_build.py # 多线程构建脚本
└── pyproject.toml # 项目配置
构建项目
# 使用自定义构建脚本
python thread_build.py
# 指定配置文件和输出目录
python thread_build.py --config build_config.json --output-dir dist
开发依赖
项目依赖以下主要库:
pywin32: Windows API 访问pillow: 图像处理tenacity: 重试机制psutil: 进程管理comtypes: COM 组件支持requests: HTTP 请求
⚠️ 注意事项
兼容性要求
- 操作系统: Windows 7/8/10/11
- Python版本: 3.9-3.13
- 微信版本: 4.0.5(其他版本不保证兼容)
- 架构: 仅支持 Windows x64
使用限制
- 本工具仅供学习和个人使用
- 请遵守微信服务条款和相关法律法规
- 不建议用于商业用途或大规模自动化
- 使用前请备份重要数据
常见问题
Q: 提示"微信版本不兼容"怎么办? A: 请确保使用微信 4.0.5 版本,其他版本暂不支持。
Q: 授权失败怎么办?
A: 使用 wxautox4 --export 导出机器码,联系管理员获取授权文件。
Q: 消息发送失败? A: 检查微信是否登录,目标用户是否存在,网络是否正常。
Q: 监听消息不生效?
A: 确保已调用 AddListenChat() 并且微信窗口处于活动状态。
📄 许可证
MIT License - 详见 LICENSE 文件
🤝 贡献
欢迎提交 Issue 和 Pull Request!
📞 联系方式
- 作者: Cluic
- 邮箱: tikic@qq.com
- 项目地址: GitHub
免责声明: 本工具仅用于学习和研究目的,使用者应当遵守相关法律法规,作者不承担任何因使用本工具而产生的法律责任。
Metadata
Release files for wxautox4 41.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| wxautox4-41.2.1-cp313-cp313-win_amd64.whl | CPython 3.13 | CPython 3.13 | Windows x86-64 | Details |
| wxautox4-41.2.1-cp312-cp312-win_amd64.whl | CPython 3.12 | CPython 3.12 | Windows x86-64 | Details |
| wxautox4-41.2.1-cp311-cp311-win_amd64.whl | CPython 3.11 | CPython 3.11 | Windows x86-64 | Details |
| wxautox4-41.2.1-cp310-cp310-win_amd64.whl | CPython 3.10 | CPython 3.10 | Windows x86-64 | Details |
| wxautox4-41.2.1-cp39-cp39-win_amd64.whl | CPython 3.9 | CPython 3.9 | Windows x86-64 | Details |
Total release size: 26.7 MB
Release files / wxautox4-41.2.1-cp313-cp313-win_amd64.whl
| Download URL | wxautox4-41.2.1-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 4.3 MB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
6932d41f5d47127889b1c2840a8e07308f9c4dbd35a8c2d6693aef5212957ea1
|
|
BLAKE2b-256 checksum How to use checksums |
4e0a7b3a84bdbdebc1402186ccac28eab941342264d39903d5d35f134696e139
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.3
|
Release files / wxautox4-41.2.1-cp312-cp312-win_amd64.whl
| Download URL | wxautox4-41.2.1-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 5.7 MB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
48824552e0f9859eaa681c7a1fef63d3ade2fe1e7a8161d4c9a1a6fb331871c2
|
|
BLAKE2b-256 checksum How to use checksums |
deac17333625d6c8de91536a9ccb78861ed4c04f6aca4a693ef84e7f0757cd44
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.3
|
Release files / wxautox4-41.2.1-cp311-cp311-win_amd64.whl
| Download URL | wxautox4-41.2.1-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 5.7 MB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
d3a2bc7c701107081daba1db1428b9191de7cd64bc46cc95f552db3c5211ee5c
|
|
BLAKE2b-256 checksum How to use checksums |
7023f4ebb000d985b02cad073fd7ea24dbcd93d126ab59d5e0ed3c05a34ad95c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.3
|
Release files / wxautox4-41.2.1-cp310-cp310-win_amd64.whl
| Download URL | wxautox4-41.2.1-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 5.5 MB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
2db9555982536c8f802b995613a54cdc86f01e5132931791684f6cc7832c14bb
|
|
BLAKE2b-256 checksum How to use checksums |
c811cc98366180cfca3d199d08e47ac9bab12cc50f2bb64acd8448eccc7e1f1f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.3
|
Release files / wxautox4-41.2.1-cp39-cp39-win_amd64.whl
| Download URL | wxautox4-41.2.1-cp39-cp39-win_amd64.whl |
|---|---|
| Size | 5.5 MB |
| Tags | CPython 3.9 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
d007731e8b58aafd3708501da5d29d8b108134a7984b40b1bfc14a683ddac214
|
|
BLAKE2b-256 checksum How to use checksums |
f2078b0e88d2a7c88464094a18581c30b27dda0456c5fd809a9f013747b1f819
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.3
|