python抄了一个回复微信消息的脚本,改了一下bu,记录一下
import numpy as np # 引入numpy库,目的是将读取的数据转换为列表
import pandas as pd # 引入pandas库,用来读取csv数据
# 引入uiautomation库中的WindowControl类,用来进行图像识别和模拟操作
from uiautomation import WindowControl
import time
import requests
# 创建一个微信窗口对象,searchDepth=1表示只搜索当前窗口下的子窗口,Name="微信"表示搜索名称为"微信"的窗口
wx = WindowControl(searchDepth=1, Name="微信")
wx.ListControl() # 获取微信窗口下的ListControl对象,ListControl表示一个列表控件,用于显示消息列表
wx.SwitchToThisWindow() # 将焦点切换到微信窗口,以便进行操作
# 获取微信窗口下的ListControl对象,Name = '会话'表示搜索名称为"会话"的列表控件
hw = wx.ListControl(Name='会话')
# 读取csv数据,encoding='utf-8'表示使用utf-8编码读取数据
df = pd.read_csv('回复数据.csv', encoding='utf-8')
conversations = hw.GetChildren() # 获取会话列表中的所有会话
jilu_num = 0
for conversation in conversations:
contact_name = conversation.Name
if '正经工作群' in contact_name: # 判断contact_name是否包含'正经工作群' # 想要联系的 name
conversation .Click(simulateMove=False) # 点击会话,进入聊天界面
messageList = wx.ListControl(Name="消息").GetChildren() # 获取消息列表
jilu_num = len(messageList)
print(f'原本有{jilu_num}条记录')
last_msg = '' # 记录上一次获取的最后一条消息
# 死循环接收消息
# while True:
# 设置每次执行的次数
num_executions = 1000
# 设置每次执行之间的间隔时间(单位:秒)
interval = 10
# 计数器初始化
count = 0
while count < num_executions:
conversations = hw.GetChildren()
for conversation in conversations:
contact_name = conversation.Name
if '正经工作群' in contact_name: # 想要联系的 name
# conversation.Click(simulateMove=False)
message_list = wx.ListControl(Name="消息").GetChildren()
if message_list: # 确保消息列表非空
new_last_msg = message_list[-1].Name # 获取当前最后一条消息
print(f"最后一条消息是:{last_msg}")
else:
print("消息列表为空")
if new_last_msg != last_msg: # 与上一次获取的消息进行比较
# 判断关键字
msg = df.apply(
lambda x: x['回复内容'] if x['关键字'] in new_last_msg else None, axis=1)
# 返回的结果是一个包含处理结果的Series对象,msg和列表差不多
# print(f"匹配到的回复内容:{msg}")
# 这行代码移除回复内容中的空数据(NaN值)
msg.dropna(axis=0, how='any', inplace=True)
ar = np.array(msg).tolist() # 这行代码将筛选后的回复内容转换为列表
# 匹配到数据时
if ar:
# 将数据输入
# 替换换行符号
wx.SendKeys(ar[0].replace(
'{br}', '{Shift}{Enter}'), waitTime=0)
# 发送消息,回车键
wx.SendKeys('{Enter}', waitTime=1)
# 通过消息匹配检索会话栏的联系人
print(f"回复内容是 {ar[0]}")
last_msg = ar[0] # 更新 last_msg
# break
else:
wx.SendKeys('我不理解你什么意思', waitTime=1)
wx.SendKeys('{Enter}', waitTime=1)
last_msg = '我不理解你什么意思' # 更新 last_msg
else:
print("没有新消息")
last_msg = new_last_msg # 更新 last_msg
# 计数器递增
count += 1