Skip to main content

wxauto plus beta for 4.0

Project description

wxautox4 - WeChat自动化工具

Version Python Platform WeChat License

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): 发送后是否清空编辑框,默认 True
  • at (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

使用限制

  1. 本工具仅供学习和个人使用
  2. 请遵守微信服务条款和相关法律法规
  3. 不建议用于商业用途或大规模自动化
  4. 使用前请备份重要数据

常见问题

Q: 提示"微信版本不兼容"怎么办? A: 请确保使用微信 4.0.5 版本,其他版本暂不支持。

Q: 授权失败怎么办? A: 使用 wxautox4 --export 导出机器码,联系管理员获取授权文件。

Q: 消息发送失败? A: 检查微信是否登录,目标用户是否存在,网络是否正常。

Q: 监听消息不生效? A: 确保已调用 AddListenChat() 并且微信窗口处于活动状态。

📄 许可证

MIT License - 详见 LICENSE 文件

🤝 贡献

欢迎提交 Issue 和 Pull Request!

📞 联系方式


免责声明: 本工具仅用于学习和研究目的,使用者应当遵守相关法律法规,作者不承担任何因使用本工具而产生的法律责任。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

wxautox4-40.1.13-cp313-cp313-win_amd64.whl (3.0 MB view details)

Uploaded CPython 3.13Windows x86-64

wxautox4-40.1.13-cp312-cp312-win_amd64.whl (3.9 MB view details)

Uploaded CPython 3.12Windows x86-64

wxautox4-40.1.13-cp311-cp311-win_amd64.whl (3.9 MB view details)

Uploaded CPython 3.11Windows x86-64

wxautox4-40.1.13-cp310-cp310-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.10Windows x86-64

wxautox4-40.1.13-cp39-cp39-win_amd64.whl (3.7 MB view details)

Uploaded CPython 3.9Windows x86-64

File details

Details for the file wxautox4-40.1.13-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: wxautox4-40.1.13-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for wxautox4-40.1.13-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 717cf2e87eb361bf075807f2bf4c8534a2c46597655d7cf67ba68edeee8b50ef
MD5 0aa8773bfeb395aba7f7602eecb49114
BLAKE2b-256 e9d5a035368f89be5972652d9e7fac2c5ddb83c70d2e39e3c0ab2a05432d64e6

See more details on using hashes here.

File details

Details for the file wxautox4-40.1.13-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: wxautox4-40.1.13-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 3.9 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for wxautox4-40.1.13-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6a2a95d1e165f5988ce039b42f39d3d5387d5e563cf1973440dcab30cac78d25
MD5 ba2d76afe01c313d4fc5fe9f51b3c801
BLAKE2b-256 784a27ffa7d08bc5b5d741b793eeddbb3f98362eb2f24a778aa132a1a076a0bc

See more details on using hashes here.

File details

Details for the file wxautox4-40.1.13-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: wxautox4-40.1.13-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 3.9 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for wxautox4-40.1.13-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 26fb43dba69d321e844542e93255aeb7a095e933292ce20ae8a97f1f7dd87dbe
MD5 5d364d27a86c0e05d72892582dbf40e6
BLAKE2b-256 1e7bdda68d6dd1a55a0523437defc1f2db1ecf6139aff6c62b1176e6e3918aae

See more details on using hashes here.

File details

Details for the file wxautox4-40.1.13-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: wxautox4-40.1.13-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 3.7 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for wxautox4-40.1.13-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f93a959f4fd064b552c0710f57773f7dda85ec6bf8fab37e9e0af529c9f5969a
MD5 bcd51ccc946922268bcfcd0504ad0e66
BLAKE2b-256 4dc5925fd9292662165e778f4e08472984bf6f7674658721bcff8de334bedd6c

See more details on using hashes here.

File details

Details for the file wxautox4-40.1.13-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: wxautox4-40.1.13-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 3.7 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for wxautox4-40.1.13-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 3a88cc4ca90a8814f9bcd3dce1007468dcc52298b072dd8dbf9fdd4b99eb3859
MD5 e223c786469cf79fdc299bdf67277288
BLAKE2b-256 6d5ea9b51672f8a9f92e99294be1b2d1d9eacccba173788d04233d91ae6fd5bc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page