How to Automate WeChat Messaging with Python’s itchat: Step‑by‑Step Guide
This tutorial walks you through installing the itchat Python library, logging into WeChat, searching friends, sending messages, registering message handlers for text, images and recordings, storing media files, and capturing withdrawn messages, all with clear code examples and screenshots.
1. Install itchat
First, install the itchat library using pip: pip install itchat After the installation finishes, you will see a confirmation message.
2. itchat.login()
Run itchat.login() to open the WeChat web login page. If the web API is blocked, the console will warn that the account cannot use the web version and suggest using the desktop client.
After several attempts, a simple workaround allows the login to succeed.
3. itchat.search_friends() + send()
Once logged in, you can search for a friend and send a message. The following code (shown in the screenshot) performs the search and sends the text:
The result screenshot confirms that the message was sent successfully.
4. @itchat.msg_register()
To listen for incoming messages, register a handler with @itchat.msg_register(). The example code (shown in the screenshot) captures any text sent by friends and prints it to the console.
The console displays the received messages as expected.
5. def reserver_info(msg)
The next step reproduces a function from the original article (shown in the screenshot). The function processes incoming messages and extracts relevant information.
6. @itchat.msg_register([TEXT, PICTURE, RECORDING])
After handling text, extend the listener to process pictures and voice recordings. The code snippet (image) shows how to register these additional message types.
The author notes that the implementation works locally, but the result screenshots are omitted.
7. os.mkdir() + info()
To store received images and audio files, create a directory using os.mkdir() and save the media with info(). The code (image) demonstrates this process, and the execution screenshot confirms the files are saved.
8. note_info(msg)
Finally, to capture withdrawn messages, monitor system messages of type NOTE. The following code (image) shows how to detect and store these messages for later review.
The implementation runs successfully on the author's machine.
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.
