前置条件
1、需要windows保持开机
2、需要微信保持在前台显示```
3、相对应的python库下载
```bash
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 == 'hello':
wx.SendKeys('hello,joy{ENTER}')
fuwei(getHwnd())
elif last_msg == '今天阳光明媚':
wx.SendKeys('适合外出{ENTER}')
fuwei(getHwnd())
elif last_msg == '学习新思想':
wx.SendKeys('学习新思想,争做新青年{ENTER}')
fuwei(getHwnd())
else:
wx.SendKeys('不知道如何进行回复{ENTER}')
fuwei(getHwnd())
实现起来也是极其容易的,使用uiautomation这个库来识别微信电脑版的控件内容,
通过持续循环来检测消息列表是否存在未读消息,如果存在未读消息,
那么就自动点击进入这个消息的窗口,然后检索窗口最后一条消息,
检索自动回复的关键词库是否有这个关键词,如果存在此关键词,则直接回复。