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)