这段代码实现了一个在网页版本微信上自动回复消息的工具。 首先这个工具会打开你的微信网页版。它能和一个外部的智能服务进行交互,当你在微信上收到新消息的时候,它会把这个消息传给外部服务,然后外部服务会给出一个回复。这个工具就会把这个回复发送到微信上,就好像有一个小助手在帮你自动回复消息一样。 如果在处理新消息的过程中出了问题,比如等得太久没反应,它会去点击文件传输助手,让微信保持活跃状态。要是遇到了错误,它还会尝试刷新页面或者重新启动浏览器驱动来解决问题。 你可以通过按特定的键来退出这个自动回复程序。总之,它能帮你在微信上自动回复消息,让你不用一直手动回复,省了不少事儿呢。
它其实还可以加入发送预设图片、word文档、excel表格等功能!
我还能为你的办公软件加入新的功能,后期我会慢慢将我大学刚毕业时就已经完善的办公软件自动化功能慢慢发布出来,来日方长。
看我的代码需要配歌:网易云音乐
import logging
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
import json
import sys
import msvcrt
import time
import requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import (
NoSuchElementException,
TimeoutException,
WebDriverException
)
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('wechat_auto_reply.log'),
logging.StreamHandler()
]
)
@dataclass
class Config:
"""配置类,用于管理所有配置参数"""
chromedriver_path: str
extension_path: str
api_url: str
api_key: str
wait_timeout: int = 30
retry_interval: int = 1
@classmethod
def load_from_file(cls, config_path: str = 'config.json') -> 'Config':
"""从配置文件加载配置"""
try:
with open(config_path, 'r', encoding='utf-8') as f:
config_data = json.load(f)
return cls(**config_data)
except FileNotFoundError:
logging.error(f"配置文件 {config_path} 不存在")
raise
except json.JSONDecodeError:
logging.error(f"配置文件 {config_path} 格式错误")
raise
class WeChatAutoReply:
def __init__(self, config: Config):
self.config = config
self.driver = None
self.wait = None
self.setup_driver()
def setup_driver(self):
"""设置并初始化WebDriver"""
try:
service = Service(executable_path=self.config.chromedriver_path)
options = webdriver.ChromeOptions()
if Path(self.config.extension_path).exists():
options.add_argument(f'--load-extension={self.config.extension_path}')
self.driver = webdriver.Chrome(service=service, options=options)
self.wait = WebDriverWait(self.driver, self.config.wait_timeout)
self.driver.get('https://wx.qq.com/?target=t')
logging.info("WebDriver初始化成功")
except WebDriverException as e:
logging.error(f"WebDriver初始化失败: {e}")
raise
def get_api_response(self, message: str) -> Optional[str]:
"""获取API响应"""
try:
url = f'{self.config.api_url}?key={self.config.api_key}&appid=0&msg={message}'
response = requests.get(url, timeout=10)
response.raise_for_status()
content = response.json().get('content', '').replace('(br)', '')
return content
except requests.RequestException as e:
logging.error(f"API请求失败: {e}")
return None
def send_message(self, content: str):
"""发送消息"""
try:
message_input = self.wait.until(
EC.presence_of_element_located((By.ID, 'editArea'))
)
message_input.clear()
message_input.send_keys(content)
send_button = self.wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.btn.btn_send'))
)
send_button.click()
logging.info(f"成功发送消息: {content[:50]}...")
except (TimeoutException, NoSuchElementException) as e:
logging.error(f"发送消息失败: {e}")
raise
def handle_new_message(self):
"""处理新消息"""
try:
# 检查新消息提示
new_message = self.wait.until(
EC.presence_of_element_located(
(By.XPATH, "//i[contains(@class, 'icon web_wechat_reddot_middle ng-binding ng-scope')]")
)
)
ActionChains(self.driver).move_to_element(new_message).click().perform()
# 获取消息内容
message_element = self.wait.until(
EC.presence_of_element_located(
(By.XPATH, "//pre[contains(@class, 'js_message_plain ng-binding') and contains(text(),'你好')]")
)
)
message_content = message_element.text
# 获取并发送回复
response = self.get_api_response(message_content)
if response:
self.send_message(response)
except TimeoutException:
self.click_file_helper()
except Exception as e:
logging.error(f"处理消息时出错: {e}")
self.handle_error()
def click_file_helper(self):
"""点击文件传输助手,保持会话活跃"""
try:
file_helper = self.wait.until(
EC.presence_of_element_located(
(By.XPATH, "//span[contains(@class, 'nickname_text ng-binding') and contains(text(),'文件传输助手')]")
)
)
file_helper.click()
except (TimeoutException, NoSuchElementException) as e:
logging.warning(f"点击文件传输助手失败: {e}")
def handle_error(self):
"""错误处理"""
try:
self.driver.refresh()
time.sleep(self.config.retry_interval)
except WebDriverException as e:
logging.error(f"页面刷新失败: {e}")
self.restart_driver()
def restart_driver(self):
"""重启WebDriver"""
try:
if self.driver:
self.driver.quit()
self.setup_driver()
except WebDriverException as e:
logging.error(f"重启WebDriver失败: {e}")
raise
def run(self):
"""运行主循环"""
try:
logging.info("自动回复程序启动")
while True:
if msvcrt.kbhit() and msvcrt.getch() == b'\x1b':
logging.info("收到退出信号,程序正在终止...")
break
self.handle_new_message()
except KeyboardInterrupt:
logging.info("程序被手动中断")
finally:
if self.driver:
self.driver.quit()
logging.info("程序已终止")
def main():
try:
config = Config.load_from_file()
auto_reply = WeChatAutoReply(config)
auto_reply.run()
except Exception as e:
logging.critical(f"程序发生致命错误: {e}")
sys.exit(1)
if __name__ == "__main__":
main()