头图

摘要

腾讯微信团队重磅发布 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)

截图

获取到消息会在右下角弹出,需要注意的是,需要单独将需要获取消息的群拉出来,可以最小化,但是不能关闭窗口。

461e70ffad569a9802e712f06965145e.png

本文作者

TANKING


TANKING
4.9k 声望611 粉丝

热爱分享,热爱创作,热爱研究。