Stories

Detail Return Return

微信電腦版4.X新版獲取羣消息的自動化腳本 - Stories Detail

摘要

騰訊微信團隊重磅發佈 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

user avatar nihaojob Avatar pulsgarney Avatar qingzhan Avatar skyselang Avatar eolink Avatar yanyue404 Avatar huanjinliu Avatar ichu Avatar yanwushu Avatar aigoto Avatar jerryc Avatar phytium_developers Avatar
Favorites 37 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.