Automating WeChat Message Sending at Midnight with Python, pyautogui, and APScheduler
This tutorial demonstrates how to use Python's pyautogui and APScheduler libraries to automatically open WeChat, locate the chat window, type a message, and send it at a scheduled midnight time, providing a practical example of desktop automation and task scheduling.
This article shows how to create a Python program that automatically sends a WeChat message at midnight, fulfilling a boyfriend’s duty to remind his girlfriend to sleep.
Installation and import
Two modules are required: apscheduler and pyautogui. Install them via:
pip install apscheduler</code><code>pip install pyautoguiImport the libraries:
import pyautogui</code><code>from datetime import datetime</code><code>from apscheduler.schedulers.blocking import BlockingScheduler # blocks the current process</code><code># For a background scheduler you could use:
# from apscheduler.schedulers.background import BackgroundSchedulerpyautogui basics
Set a pause between actions: pyautogui.PAUSE = 1 # seconds Obtain the screen coordinates of the WeChat task‑bar icon and the chat input box:
print(pyautogui.position()) # e.g., Point(x=148, y=879)</code><code>icon_position = pyautogui.position() print(pyautogui.position()) # e.g., Point(x=174, y=751)</code><code>entry_position = pyautogui.position()Click those positions to focus WeChat:
pyautogui.click(icon_position) # click the icon</code><code>pyautogui.click(entry_position) # click the input boxType text using either a list of keys or a plain string (note that Chinese characters must be entered via the input method):
pyautogui.typewrite(['o', 'n', 'e', 'enter']) pyautogui.typewrite('You can type multiple letters in this way')For more complex Chinese input you can combine lists and special keys, for example:
pyautogui.typewrite([*list('zhengzai '), *list('jinxing '), 'shift', *list('pyautogui'), 'shift', *list('shiyan '), 'enter'], 0.1)To make the automation look more human‑like, move the mouse with a duration before clicking:
pyautogui.moveTo(icon_position, duration=2)</code><code>pyautogui.click(icon_position)</code><code>pyautogui.moveTo(entry_position, duration=2)</code><code>pyautogui.click(entry_position)If the message is long, you can copy it to the clipboard and paste:
import pyperclip</code><code>pyperclip.copy('正在进行发中文试验,看到请忽略,更不要骂傻逼')</code><code>pyautogui.hotkey('ctrl', 'v')</code><code>pyautogui.press('enter')Scheduling with APScheduler
APScheduler allows you to schedule the above automation to run at a specific time:
scheduler = BlockingScheduler() # instantiate a scheduler</code><code>scheduler.add_job(main, 'date', run_date=datetime(2021, 8, 18, 24, 0, 0)) # run once at the given datetime</code><code>scheduler.start()The main function contains the pyautogui actions shown earlier.
Full script
import pyautogui</code><code>import pyperclip</code><code>from datetime import datetime</code><code>from apscheduler.schedulers.blocking import BlockingScheduler</code><code>def main():</code><code> pyautogui.PAUSE = 0</code><code> icon_position = pyautogui.Point(x=148, y=879) # task‑bar icon</code><code> entry_position = pyautogui.Point(x=174, y=751) # input box</code><code> pyautogui.moveTo(icon_position, duration=1)</code><code> pyautogui.click(icon_position)</code><code> pyautogui.moveTo(entry_position, duration=0.7)</code><code> pyautogui.click(entry_position)</code><code> pyperclip.copy('快去睡觉')</code><code> pyautogui.hotkey('ctrl', 'v')</code><code> pyautogui.press('enter')</code><code> pyperclip.copy('笨猪')</code><code> pyautogui.hotkey('ctrl', 'v')</code><code> pyautogui.press('enter')</code><code>scheduler = BlockingScheduler()</code><code>scheduler.add_job(main, 'date', run_date=datetime(2021, 8, 18, 24, 0, 0))</code><code>scheduler.start()Running this script will automatically send the prepared messages to the specified WeChat contact at midnight.
Result: The author’s mother scolded him for leaving the computer on at 12 am, but the girlfriend received the reminder, completing the intended task.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
