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, "测试内容", "眠羊")
file = r'D:\花生搬家\python项目\有趣小工具\wifi.png'
who = '眠羊'
wechat.send_file_to_single_user(wechat,file=file, user=who)
# whos = ['对方1', '对方2']
# 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)
python自动发送微信信息和文件
最新推荐文章于 2025-04-05 09:06:41 发布