2
头图

Introduction

There are actually many ways to implement WeChat automatic reply. For example, the ipad protocol and Hook WeChat are relatively common. The ipad protocol is expensive and not suitable for personal use. Because Hook WeChat intercepts memory, it has the risk of being blocked. Although the risk is relatively small, this problem It still exists, and Hook WeChat depends on the version. Once the version is upgraded, you have to rewrite it, unless you keep using this version yourself.

I'm learning automation recently, so I want to use WeChat to practice my hands, so I made a small function for automatic reply here, but this still has certain problems:

1. Windows needs to be kept on
2. Wechat needs to be displayed in the foreground

code

wxautoreply.py

 import pandas as pd
import numpy as np
from uiautomation import WindowControl, MenuControl
import win32gui
import win32con
import pyautogui
 
# 获取窗口句柄并打开窗口
def getHwnd():
        hwnd = win32gui.FindWindow('WeChatMainWndForPC','微信')
        win32gui.ShowWindow(hwnd, win32con.SW_SHOWMINIMIZED)
        win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
        win32gui.ShowWindow(hwnd, win32con.SW_SHOW)
        win32gui.SetForegroundWindow(hwnd)
        return hwnd
 
# 复位(自动回复之后自动点击消息列表第二个聊天窗口)
def fuwei(hwnd):
        left, top, right, bottom = win32gui.GetWindowRect(hwnd)
        leftpoint = left+155
        toppoint = top+150
        pyautogui.moveTo(leftpoint, toppoint)
        pyautogui.click()
        return '已复位'
 
getHwnd()
wx = WindowControl(Name="微信")
wx.SwitchToThisWindow()
hw = wx.ListControl(Name="会话")
 
# 持续循环监听未读消息
while True:
        we = hw.TextControl(searchDepth=4)
        # 如果存在未读消息
        if we.Name:
                we.Click(simulateMove=False)
                # 获取当前最后一条消息
                last_msg = wx.ListControl(Name='消息').GetChildren()[-1].Name
                if last_msg == '你好':
                        wx.SendKeys('你好,才是真的好{ENTER}')
                        fuwei(getHwnd())
                elif last_msg == '123':
                        wx.SendKeys('456{ENTER}')
                        fuwei(getHwnd())
                elif last_msg == '测试':
                        wx.SendKeys('测试成功{ENTER}')
                        fuwei(getHwnd())
                else:
                        wx.SendKeys('无法匹配{ENTER}')
                        fuwei(getHwnd())

Realize ideas

It is also extremely easy to implement. Use the uiautomation library to identify the control content of the WeChat computer version, and detect whether there are unread messages in the message list through continuous looping. If there are unread messages, then automatically click to enter the message window, and then The last message in the search window, search whether the keyword library of automatic reply has this keyword, if there is this keyword, it will reply directly.

Video demo

https://share.weiyun.com/A3lxVd8B

It is relatively rough, mainly to share the implementation principle, and I will make a relatively stable tool with an interface in the future when I am proficient in automatic learning~

author

TANKING

Blog

https://www.likeyunba.com

WeChat

sansure2016


TANKING
4.8k 声望493 粉丝

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