下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:7819
代码说明:这个微信自动化工具包含三个主要模块,主程序实现微信基本操作功能,定时模块支持计划任务,图像处理模块提供辅助功能。使用时需要先准备微信界面元素的截图模板,通过图像识别定位界面元素位置。
import time
import pyautogui
import pyperclip
import keyboard
from PIL import ImageGrab
import cv2
import numpy as np
import threading
class WeChatAuto:
def __init__(self):
self.running = False
self.message_queue = []
self.contact_positions = {}
self.template_images = {
'search_icon': cv2.imread('search_icon.png', 0),
'send_button': cv2.imread('send_button.png', 0)
}
def find_image_position(self, template, threshold=0.8):
screenshot = ImageGrab.grab()
screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
gray_screenshot = cv2.cvtColor(screenshot, cv2.COLOR_BGR2GRAY)
result = cv2.matchTemplate(gray_screenshot, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
if max_val >= threshold:
return max_loc
return None
def activate_wechat(self):
pyautogui.hotkey('ctrl', 'alt', 'w')
time.sleep(1)
def search_contact(self, contact_name):
search_pos = self.find_image_position(self.template_images['search_icon'])
if search_pos:
pyautogui.click(search_pos[0]+10, search_pos[1]+10)
time.sleep(0.5)
pyperclip.copy(contact_name)
pyautogui.hotkey('ctrl', 'v')
time.sleep(1)
pyautogui.press('enter')
time.sleep(1)
return True
return False
def send_message(self, message):
pyperclip.copy(message)
pyautogui.hotkey('ctrl', 'v')
time.sleep(0.5)
send_pos = self.find_image_position(self.template_images['send_button'])
if send_pos:
pyautogui.click(send_pos[0]+15, send_pos[1]+15)
return True
return False
def auto_reply(self, trigger_words, reply_message):
while self.running:
latest_msg = self.get_latest_message()
if latest_msg and any(word in latest_msg for word in trigger_words):
self.send_message(reply_message)
time.sleep(1)
def get_latest_message(self):
# 实现消息监控逻辑
pass
def start_auto_reply(self, trigger_words, reply_message):
self.running = True
thread = threading.Thread(
target=self.auto_reply,
args=(trigger_words, reply_message)
)
thread.start()
def stop_auto_reply(self):
self.running = False
def batch_send_messages(self, contact_list, messages, interval=1):
for contact in contact_list:
if self.search_contact(contact):
for msg in messages:
self.send_message(msg)
time.sleep(interval)
time.sleep(1)
schedule
import time
from datetime import datetime
from wechat_auto import WeChatAuto
class MessageScheduler:
def __init__(self):
self.wechat = WeChatAuto()
self.scheduled_tasks = []
def add_daily_task(self, contact, message, hour, minute):
def job():
print(f"Sending message to {contact} at {datetime.now()}")
self.wechat.activate_wechat()
if self.wechat.search_contact(contact):
self.wechat.send_message(message)
schedule.every().day.at(f"{hour:02d}:{minute:02d}").do(job)
self.scheduled_tasks.append({
'contact': contact,
'message': message,
'time': f"{hour:02d}:{minute:02d}",
'type': 'daily'
})
def add_weekly_task(self, contact, message, day_of_week, hour, minute):
def job():
print(f"Sending weekly message to {contact}")
self.wechat.activate_wechat()
if self.wechat.search_contact(contact):
self.wechat.send_message(message)
schedule.every().day.at(f"{hour:02d}:{minute:02d}").do(job)
self.scheduled_tasks.append({
'contact': contact,
'message': message,
'day': day_of_week,
'time': f"{hour:02d}:{minute:02d}",
'type': 'weekly'
})
def run_pending(self):
while True:
schedule.run_pending()
time.sleep(1)
def get_scheduled_tasks(self):
return self.scheduled_tasks
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。