An automated WeChat message response tool implemented with Python + PyAutoGUI, which completes the full automated process of message monitoring and replying through screen recognition and keyboard/mouse simulation.
- Automatically locate and activate the WeChat window
- Monitor changes in the WeChat chat interface and trigger automatic replies when new messages are detected
- Automatically call Baidu ERNIE Bot (chat.baidu.com) to generate reply content
- Complete button clicks, content copy/paste, and other operations through screen image recognition
- Support keyboard and mouse simulation on Windows systems with a retry mechanism to prevent click failures
Only supports Windows systems (restricted by os.name == "nt" in the script; MacOS and Linux systems will exit directly)
- Ensure Python 3.7+ is installed
- Install core dependency packages:
pip install pyautogui opencv-python pillow pywin32- Note:
pyautoguidepends on OpenCV. If installation fails, try:
pip install opencv-python==4.8.0.74The script depends on the following two custom modules (ensure they are in the same directory as the script):
up.py: Contains theup_window()function for activating/keeping specified windows (e.g., WeChat) on topmessage.py: Containsget_screen_roi()anddetect_screen_change()functions for monitoring screen area changescheck.py: Containsis_clipboard_image()for detecting successful copying
-
Prepare image files for recognition (must be in the same directory as the script; pre-prepared):
button.png: Screenshot of the button to be clickedchinese.png: Screenshot of the Chinese input method status indicator (used to determine input method status)copy_icon.png: Screenshot of the copy button (used to copy generated reply content)copy.png: Alternate version of copy_icon.png
-
WeChat Settings:
- Ensure WeChat is open and its icon is visible in the taskbar
- Place the script file (
auto_reply.py), custom modules, and recognition images in the same directory - Open WeChat
- Run the script:
python auto_reply.py- After startup, the script will start a 5-second countdown before automatically beginning monitoring
- Terminate the script: Press
Ctrl + Cor move the mouse to the top-left corner (0,0) to stop running
- Open the Baidu ERNIE Bot webpage on first run; switch windows via
Alt + Tabon subsequent runs - Detect input method status; automatically press
Shiftto switch to Chinese if not already active - Input preset text, paste clipboard content, and send the generated reply
- Identify and click the copy button to copy the reply content to the clipboard
- Switch back to the WeChat window, paste and send the reply content
- Continuously monitor screen changes and repeat the above process when new messages are detected
- Screen Resolution: Coordinates in the script (e.g.,
520, 911) are based on a specific resolution; re-adjust coordinate values after changing devices/resolutions - Image Recognition Accuracy:
confidence=0.9means 90% image matching accuracy; lower the value (e.g., 0.8) if recognition fails, but excessively low values may cause misrecognition - Permission Issues: Run as a regular user on Windows; administrator privileges may cause window activation failures
- Anti-Failure Mechanism: The script checks for successful button clicks and triggers alternate click logic if failed
- Safety Note: PyAutoGUI's
FAILSAFE = Truemeans moving the mouse to the top-left corner of the screen triggers an emergency stop to prevent script out-of-control
- Check if image files exist and are named correctly
- Confirm the
up_window("WeChat")function inup.pycan correctly identify the WeChat window name - Close multiple WeChat instances and keep only one WeChat window running
- Ensure the system default input method is Chinese and
chinese.pngcan correctly identify Chinese status - The script includes
pyautogui.press("shift")to switch input methods; manually switch if this fails, then re-run the script
This script is for learning and personal use only. Do not use it for commercial purposes or in scenarios that violate WeChat's User Agreement. The user assumes all risks arising from its use.