動態

詳情 返回 返回

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

摘要

騰訊微信團隊重磅發佈 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 頭像 pulsgarney 頭像 qingzhan 頭像 skyselang 頭像 eolink 頭像 yanyue404 頭像 huanjinliu 頭像 ichu 頭像 yanwushu 頭像 aigoto 頭像 jerryc 頭像 phytium_developers 頭像
點贊 37 用戶, 點贊了這篇動態!
點贊

Add a new 評論

Some HTML is okay.