Automate Midnight WeChat Messages with Python, pyautogui, and APScheduler
This tutorial shows how to use Python's pyautogui and APScheduler libraries to automatically open WeChat, locate the chat window, type a message, and send it precisely at midnight, providing a practical example of desktop automation and scheduled tasks.
The article demonstrates a Python solution for automatically sending a WeChat message at 12 AM, fulfilling a boyfriend's promise to remind his girlfriend to sleep.
Installation and Import
Two required modules are apscheduler and pyautogui . Install them with:
pip install apscheduler
pip install pyautoguiImport the necessary packages:
import pyautogui
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingSchedulerUsing pyautogui for Mouse and Keyboard Automation
Set a pause between actions to mimic human speed: pyautogui.PAUSE = 1 Obtain the screen coordinates of the WeChat task‑bar icon and the message input box:
print(pyautogui.position()) # e.g., Point(x=148, y=879)
icon_position = pyautogui.position()
print(pyautogui.position()) # e.g., Point(x=174, y=751)
entry_position = pyautogui.position()Move the cursor to each point and click:
pyautogui.moveTo(icon_position, duration=2)
pyautogui.click(icon_position)
pyautogui.moveTo(entry_position, duration=2)
pyautogui.click(entry_position)Enter text either by passing a list of characters or a string. For Chinese input, rely on the active input method:
pyautogui.typewrite([*list('zhengzai '), *list('jinxing '), 'shift', *list('pyautogui'), 'shift', *list('shiyan '), 'enter'], 0.1)For longer messages, copy the text to the clipboard and paste it:
import pyperclip
pyperclip.copy('正在进行发中文试验,看到请忽略,更不要骂傻逼')
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')Scheduling with APScheduler
APScheduler allows you to schedule the above automation to run at a specific date and time. Create a blocking scheduler, add the job, and start it:
scheduler = BlockingScheduler()
scheduler.add_job(main, 'date', run_date=datetime(2021, 8, 18, 24, 0, 0))
scheduler.start()Complete Script
import pyautogui
import pyperclip
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
def main():
pyautogui.PAUSE = 0
icon_position = pyautogui.Point(x=148, y=879) # task‑bar icon
entry_position = pyautogui.Point(x=174, y=751) # message box
pyautogui.moveTo(icon_position, duration=1)
pyautogui.click(icon_position)
pyautogui.moveTo(entry_position, duration=0.7)
pyautogui.click(entry_position)
pyperclip.copy('快去睡觉')
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')
pyperclip.copy('笨猪')
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')
scheduler = BlockingScheduler()
scheduler.add_job(main, 'date', run_date=datetime(2021, 8, 18, 24, 0, 0))
scheduler.start()Running the script sends the predefined messages to the girlfriend at midnight. The author reports that the next morning the computer was still on, his mother questioned the late‑night activity, but the girlfriend received the reminder as intended.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
