任务:使用电脑自动检测微信未读消息,如果接收到消息为“场地”则自动回复info中的信息。
需满足条件:
- 微信程序一直挂着
- 需要有一个不需要监控的对话框置顶在最上面
- 电脑不能锁屏
from uiautomation import WindowControl # 引入uiautomation库中的WindowControl类,用来进行图像识别和模拟操作
import pyperclip
import pyautogui
from wxauto import *
# 绑定微信主窗口
wx = WindowControl(
Name='微信',
searchDepth=1
)
# 切换窗口
wx.ListControl()
wx.SwitchToThisWindow()
# 寻找会话控件绑定
hw = wx.ListControl(Name='会话')
info = '''8/19 周一:11号场 19:15-21:15
8/20 周二:无
8/21 周三:11号场 19:15-21:15'''
# 死循环接收消息
while True:
# 从查找未读消息
we = hw.TextControl(searchDepth=4)
# 死循环维持,没有超时报错
while not we.Exists():
pass
# 存在未读消息
if we.Name:
# 点击未读消息
we.Click(simulateMove=False)
# 读取最后一条消息
last_msg = wx.ListControl(Name='消息').GetChildren()[-1].Name
print(last_msg)
# 选择联系人(小瓜改为相应的联系人)
select_user = wx.ListControl(Name='消息').GetChildren()[-1].ButtonControl().Name
print(select_user)
if select_user == '小瓜':
if last_msg == "场次":
pyperclip.copy(info) # 使用pyperclip将消息复制到剪贴板
pyautogui.hotkey('ctrl', 'v') # 使用快捷键粘贴消息
wx.SendKeys('{Enter}', waitTime=0)
# 点击上面的窗口,以备下一次循环
pyautogui.press('up')
# 没有匹配到数据时
else:
# 点击上面的窗口,以备下一次循环
pyautogui.press('up')