下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:3827
这个代码模拟了微信自动群发的基本流程,包括加载群列表、设置消息内容、定位群聊和发送消息等功能。实际实现需要考虑更多异常情况和微信的防自动化机制。请注意这仅用于技术研究,实际使用可能违反微信用户协议。
import time
import random
from typing import List, Dict
import pyautogui
import pyperclip
class WeChatAutoSender:
def __init__(self):
self.group_list = []
self.message_content = ""
self.delay = 2.0
self.sent_count = 0
def load_groups(self, file_path: str) -> None:
"""从文件加载微信群列表"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
self.group_list = [line.strip() for line in f if line.strip()]
print(f"成功加载 {len(self.group_list)} 个微信群")
except Exception as e:
print(f"加载群列表失败: {str(e)}")
def set_message(self, content: str) -> None:
"""设置要发送的消息内容"""
self.message_content = content
print("消息内容已设置")
def set_delay(self, seconds: float) -> None:
"""设置发送间隔时间"""
self.delay = max(1.0, seconds)
def locate_group(self, group_name: str) -> bool:
"""模拟定位微信群"""
print(f"正在定位群: {group_name}")
time.sleep(self.delay * 0.5)
return random.random() > 0.1 # 模拟90%成功率
def send_message(self) -> bool:
"""模拟发送消息"""
try:
pyperclip.copy(self.message_content)
time.sleep(0.5)
pyautogui.hotkey('ctrl', 'v')
time.sleep(0.3)
pyautogui.press('enter')
self.sent_count += 1
return True
except Exception as e:
print(f"发送失败: {str(e)}")
return False
def run(self) -> Dict[str, int]:
"""执行批量发送"""
if not self.group_list:
print("错误: 群列表为空")
return {"total": 0, "success": 0}
if not self.message_content:
print("错误: 消息内容为空")
return {"total": 0, "success": 0}
print(f"准备向 {len(self.group_list)} 个群发送消息...")
results = {"total": len(self.group_list), "success": 0}
for group in self.group_list:
if self.locate_group(group):
if self.send_message():
results["success"] += 1
print(f"已发送至 {group} (总计: {results['success']}/{results['total']})")
time.sleep(self.delay)
else:
print(f"定位群 {group} 失败")
return results
if __name__ == "__main__":
sender = WeChatAutoSender()
sender.load_groups("groups.txt")
sender.set_message("这是一条测试消息,请勿回复。")
sender.set_delay(3.0)
result = sender.run()
print(f"发送完成. 成功: {result['success']}, 失败: {result['total'] - result['success']}")
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。