Python 实现 微信自动发消息

itchat已不可用

itchat本质上是用的微信的网页端的接口来进行数据交互的。

目前实测了一下,使用本人自己的账号登录失败。

wxauto

环境

Windows 10

Python 3.11.9

安装Python3的第三方库wxauto

pip uninstall wxauto

每隔10秒,发一次消息

from wxauto import *
import schedule
import time

class WeChatAuto:
    def __init__(self):
        # 获取当前微信客户端
        self.wx = WeChat()
        # 获取会话列表
        self.wx.GetSessionList()

    # 向单个用户发消息
    @staticmethod
    def send_msg_to_single_user(self, msg, user):
        try:
            print(f"向用户`{user}`发送消息:{msg}")
            self.wx.ChatWith(user)  # 打开`文件传输助手`聊天窗口
            self.wx.SendMsg(msg)
            print("发送完毕")
        except Exception as e:
            print("发送失败,原因:", e)

    # 函数功能: 单个用户 单个文件发送
    @staticmethod
    def send_file_to_single_user(self,file, user):
        try:
            print(f"向用户`{user}`发送文件:{file}")
            self.wx.ChatWith(user)  # 打开`文件传输助手`聊天窗口
            self.wx.SendFiles(file)
            print("发送完毕")
        except Exception as e:
            print("发送失败,原因:", e)

    # 向多个用户发多个文件
    @staticmethod
    def send_files_to_mul_user(self,files, users):
        for who in users:
            self.wx.ChatWith(who)
            for file in files:
                print(f"向用户`{who}`发送文件:{file}")
                self.wx.SendFiles(file)
        print("发送成功")

if __name__ == '__main__':
    wechat =WeChatAuto()

    # 定时任务,每10秒发一次消息
    def send_msg_job():
        wechat.send_msg_to_single_user(wechat, "Welcome" ,"文件传输助手")

        # file = r'C:\Users\111\Desktop\1.txt'
        # who = '文件传输助手'
        # wechat.send_file_to_single_user(wechat,file=file, user=who)

        # whos = ['文件传输助手', '微信团队']
        # files = [r'C:\Users\111\Desktop\1.jpg',
        #          r'C:\Users\111\Desktop\2.jpg']
        # wechat.send_files_to_mul_user(wechat, files=files, users=whos)
    schedule.every(10).seconds.do(send_msg_job)
    while True:
        schedule.run_pending()
        time.sleep(1)


  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值