Skip to content

Hello-Mr-Crab/pywechat

Repository files navigation

pywechat🥇

image

🍬🍬微信RPA工具,支持4.1+微信自动化

pywechat是一个基于pywinauto实现的Windows系统下PC微信自动化(pure uiautomation)的项目(不涉及逆向Hook操作),可以用来收发消息和数据获取。 注意,本项目开发的初衷为使用UI自动化和分享UI自动化技术,忌勿将该工具用于违反微信使用条例的操作!

适用环境

  1. 微信版本:3.9.12.x,4.1.6.x
  2. 操作系统:🪟7 🪟10 🪟11
  3. python版本:3.10+(支持TypeHint)
  4. 支持语言:简体中文,English,繁體中文

pyweixin 与 pywechat 项目结构(pywechat只能用于32位x86🪟10,32位x86🪟7)

pyweixin结构

pywechat只支持旧版本微信(大部分方法仍然稳定可用),现在主要维护与更新支持4.1系列自动化的pyweixin pywechat使用说明

Skill使用

目前,可以使用的Skill的Agent主流平台:

Skill主流平台

具体使用方法可见QuickStart.md

openclaw评价

使用示例:

pyweixin-rpa测试

实际效果:

openclaw测试案例

快速上手✌️

点击查看QuickStart.md

pyweixin模块介绍(适用于4.1+微信)

pyweixin内所有方法需要先导入模块下的类然后调用内部方法☸︎

from pyweixin import xx(class)
xx(class).yy(method)

WechatTools🌪️🌪️

class包括
  • Tools:关于PC微信的一些工具,微信路径,运行状态,以及内部一些UI相关的判别方法。
  • Navigator:打开微信内部一切可以打开的界面。

WechatAuto🛏️🛏️

class(类) 包括
  • AutoReply:自动回复操作。(一种简单的实现方式,如果对此感兴趣可以使用pyweixin内部的一些方法自行尝试)
  • Call: 给某个好友打视频或语音电话。
  • Contacts: 获取通讯录内各分区(联系人,企业微信联系人,公众号,服务号)好友的名称与详情。
  • Files: 文件发送,聊天文件从本地导出等。
  • FriendSettings: 针对某个好友的一些相关设置。
  • Messages: 消息发送,聊天记录获取,聊天会话导出等。
  • Moments:针对微信朋友圈的一些方法,包括朋友圈内容获取,发布朋友圈。
  • Monitor:打开聊天窗口进行监听消息。

WinSettings🔹🔹

class(类)包括
  • SystemSettings:该模块中提供了一些修改windows系统设置的方法(在自动化过程中)。

utils🍬🍬

内部的一些函数主要用来二次开发,大部分传入的参数是main_window,pywinauto实例化的对象(使用Navigator.open_weixin打开)

func包括
  • At:在群聊中At指定的一些好友
  • At_all:在群聊中At所有人
  • auto_reply_to_friend_decorator:自动回复好友装饰器
  • get_new_message_num:获取新消息总数,微信按钮上的红色数字
  • scan_for_newMessages:会话列表遍历一遍有新消息提示的对象,返回好友名称与数量
  • open_red_packet: 点击打开好友发送的红包
  • language_detector:微信当前使用语言检测(不能禁用WeChatAppex.exe(涉及到公众号,微信内置浏览器,视频号等功能),原理是查询WeChatAppex.exe命令行参数)

pyweixin使用示例

所有自动化操作只需两行代码即可实现,即:

from pyweixin import xxx
xxx.yy

发送语音消息给好友

'''
微信版本>=4.1.9,且配置过虚拟驱动,并且安装sounddevice与soundfile库,具体可见
https://mrcrab.blog.csdn.net/article/details/160481307?fromshare=blogdetail&sharetype=blogdetail&sharerId=160481307&sharerefer=PC&sharesource=weixin_73953650&sharefrom=from_link
''''
from pyweixin import Messages
Messages.send_audios_to_friend(friend='小号测试',audios=[r"E:\Desktop\录音.wav",r"E:\Desktop\音乐.mp3",r"E:\Desktop\音乐.ogg"])

关于微信的基本信息输出

from pyweixin import Tools
print(Tools.about_weixin())

微信基本信息

多线性监听某个聊天窗口消息

from concurrent.futures import ThreadPoolExecutor
from pyweixin import Navigator,Monitor
#先打开所有好友的独立窗口
dialog_windows=[]
friends=['Hello,Mr Crab','Pywechat测试群']
durations=['1min']*len(friends)
for friend in friends:
    dialog_window=Navigator.open_seperate_dialog_window(friend=friend,window_minimize=True,close_weixin=True)
    dialog_windows.append(dialog_window)
with ThreadPoolExecutor() as pool:
    results=pool.map(lambda args: Monitor.listen_on_chat(*args),list(zip(dialog_windows,durations)))
for friend,result in zip(friends,results):
    print(friend,result)
#返回值 {'新消息总数':x,'文本数量':x,'文件数量':x,'图片数量':x,'视频数量':x,'链接数量':x,'文本内容':x,'消息发送人':x}

listen_on_chat

监听会话列表内的新消息

from pyweixin import Monitor
#maxPages设置为0不滚动,只在顶部可见部分监听,全屏+maxPages为0+需要监听对象置顶=稳定安全
result=Monitor.listen_on_newMessages(duration='30s',maxPages=0)
print(result)

"监听会话列表"

"监听会话列表结果"

自动回复会话列表内的新消息

from pyweixin import AutoReply
def reply_func(friend,newMessage):
    return f'我在自动回复:{friend},这是对方发送的新消息:{newMessage}'
#maxPages设置为0不滚动,只在顶部可见部分自动回复,全屏+maxPages为0+需要回复对象置顶=稳定安全
responded_detail=AutoReply.auto_reply_messages(callback=reply_func,duration='1min',maxPages=0)
print(responded_detail)

"自动回复会话列表新消息"

朋友圈内容导出

from pyweixin import Moments
#获取并导出今日前10个好友发布的朋友圈的具体内容
posts=Moments.dump_recent_posts(recent='Today',save_detail=True,number=10)
for dic in posts:
    print(dic)

朋友圈内容导出

发布朋友圈

from pyweixin import Moments
Moments.post_moments(texts='''发布朋友圈测试[旺柴]''',medias=[r"E:\Desktop\test0.png",r"E:\Desktop\test1.png"])

发朋友圈


好友朋友圈内容导出

from pyweixin import Moments
Moments.dump_friend_posts(friend='xxx',number=3,save_detail=True,target_folder=r"E:\Desktop\好友朋友圈内容导出")

好友朋友圈内容导出 好友朋友圈内容

好友朋友圈自定义评论

from pyweixin import Moments
def comment_func(content):
    if 'xxx' in content:
        return 'yyy'
    return 'zzz'
Moments.like_friend_posts(friend='xxx',number=20,callback=comment_func)

公众号文章url获取

#注意,该方法不稳定!
from pyweixin import Collections
Collections.collect_offAcc_articles(name='新华社',number=10)
urls=Collections.cardLink_to_url(number=10)
for url,text in urls.items():
    print(f'{text}\n{url}')

公众号文章url获取

收藏内的笔记导出为MarkDown

from pyweixin import Collections
Collections.save_notes(number=1)

微信笔记导出

此外pyweixin内所有方法及函数的一些位置参数支持全局设定,be like

from pyweixin import Navigator,GlobalConfig
GlobalConfig.load_delay=2.5
GlobalConfig.is_maximize=True
GlobalConfig.close_weixin=False
Navigator.search_channels(search_content='微信4.0')
Navigator.search_miniprogram(name='问卷星')
Navigator.search_official_account(name='微信')

其他使用方法可见代码中详细的文档注释以及pyweixin操作手册.docx

注意

👎👎请勿将pywechat用于任何非法商业活动,因此造成的一切后果由使用者自行承担!

本项目相关教程博客


作者CSDN主页:Hello,Mr Crab

About

仍然可用的微信RPA!支持4.0系列微信pywechat是一个基于pywinauto实现的windows桌面微信自动化操作工具,基本实现了PC微信内置的各项操作。

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors