python微信回复消息

本文介绍了如何使用Python编写脚本,结合Uiautomation库对微信消息进行自动化操作,包括图像识别、消息列表处理和关键字匹配自动回复。
摘要由CSDN通过智能技术生成

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

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值