Operations 8 min read

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.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Automating WeChat Message Sending at Midnight with Python, pyautogui, and APScheduler

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:

<code>pip install apscheduler</code><code>pip install pyautogui</code>

Import the libraries:

<code>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 BackgroundScheduler</code>

pyautogui basics

Set a pause between actions:

<code>pyautogui.PAUSE = 1  # seconds</code>

Obtain the screen coordinates of the WeChat task‑bar icon and the chat input box:

<code>print(pyautogui.position())  # e.g., Point(x=148, y=879)</code><code>icon_position = pyautogui.position()</code>
<code>print(pyautogui.position())  # e.g., Point(x=174, y=751)</code><code>entry_position = pyautogui.position()</code>

Click those positions to focus WeChat:

<code>pyautogui.click(icon_position)  # click the icon</code><code>pyautogui.click(entry_position)  # click the input box</code>

Type 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:

<code>pyautogui.typewrite([*list('zhengzai '), *list('jinxing '), 'shift', *list('pyautogui'), 'shift', *list('shiyan '), 'enter'], 0.1)</code>

To make the automation look more human‑like, move the mouse with a duration before clicking:

<code>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)</code>

If the message is long, you can copy it to the clipboard and paste:

<code>import pyperclip</code><code>pyperclip.copy('正在进行发中文试验,看到请忽略,更不要骂傻逼')</code><code>pyautogui.hotkey('ctrl', 'v')</code><code>pyautogui.press('enter')</code>

Scheduling with APScheduler

APScheduler allows you to schedule the above automation to run at a specific time:

<code>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()</code>

The main function contains the pyautogui actions shown earlier.

Full script

<code>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()</code>

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.

automationschedulingWeChatapschedulerpyautogui
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.