#!/usr/bin/python3
# -*- coding: utf-8 -*-import numpy as np
import pandas as pd
from uiautomation import WindowControl
import csvwx = WindowControl(Name='微信',searchDepth=1
)
# 切換窗口
wx.ListControl()
wx.SwitchToThisWindow()
# 尋找會話控件綁定
hw = wx.ListControl(Name='會話')
# 通過pd讀取數據
df = pd.read_csv('回復數據.csv', encoding='UTF-8')
print(df)
# 死循環接收消息
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# 判斷關鍵字msg = df.apply(lambda x: x['回復內容'] if x['關鍵詞'] in last_msg else None, axis=1)print(msg)# 數據篩選,移除空數據msg.dropna(axis=0, how='any', inplace=True)# 做成列表ar = np.array(msg).tolist()# 能夠匹配到數據時if ar:# 將數據輸入# 替換換行符號wx.SendKeys(ar[0].replace('{br}', '{Shift}{Enter}'), waitTime=1)# 發送消息 回車鍵wx.SendKeys('{Enter}', waitTime=1)# 通過消息匹配檢索會話欄的聯系人wx.TextControl(SubName=ar[0][:5]).RightClick()# 沒有匹配到數據時else:wx.SendKeys('我沒有理解你的意思', waitTime=1)wx.SendKeys('{Enter}', waitTime=1)wx.TextControl(SubName=last_msg[:5]).RightClick()
回復數據.csv
序號,關鍵詞,回復內容
1,你好,你好,我也好
2,哈哈哈,哈哈哈哈哈哈哈哈哈哈
3,告辭,再見
4,哎呦,你干嘛
5,你干嘛,哎喲
6,我喜歡你,我也喜歡你
7,拜拜,拜拜!
?