摘要
腾讯微信团队重磅发布 Mac / PC 微信 4.0 测试版,全新采用 QT+C++原生跨平台架构,大版本号更新,带来了大量新特性。
这也让以前的自动化脚本失效了,因为很多控件内容根本获取不到了。
不过还是有方法的,uiautomation 这个库还是可以获取到群消息的。
上代码
import uiautomation as auto
import time
from win10toast import ToastNotifier
import traceback
# 创建通知器
toaster = ToastNotifier()
# 已经通知过的消息集合
notified = set()
def get_chat_texts(ctrl, results=None):
if results is None:
results = []
try:
if ctrl.ClassName == "mmui::ChatTextItemView":
text = (ctrl.Name or "").strip()
if text:
results.append(text)
for child in ctrl.GetChildren():
get_chat_texts(child, results)
except Exception as e:
print("[错误] 获取消息失败:", e)
traceback.print_exc()
return results
while True:
try:
# 查找所有窗口
windows = auto.GetRootControl().GetChildren()
for win in windows:
try:
if win.ClassName == "mmui::ChatSingleWindow":
group_name = win.Name or "未知群聊" # 群聊标题
messages = get_chat_texts(win)
for msg in messages:
key = f"{group_name}_{msg}"
if key not in notified:
notified.add(key)
print(f"[新消息] {group_name}: {msg}")
try:
toaster.show_toast(
f"新消息 - {group_name}",
msg,
duration=5,
threaded=False
)
except Exception as e:
print("[错误] 弹出通知失败:", e)
except Exception as e:
print("[错误] 处理窗口时出错:", e)
traceback.print_exc()
except Exception as e:
print("[致命错误] 主循环异常:", e)
traceback.print_exc()
time.sleep(2)截图
获取到消息会在右下角弹出,需要注意的是,需要单独将需要获取消息的群拉出来,可以最小化,但是不能关闭窗口。
本文作者
TANKING
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用。你还可以使用@来通知其他用户。