Capture Deleted WeChat Messages with Python: A Step‑by‑Step Guide
This article explains how to use Python and the wxpy library to detect and forward WeChat messages that have been revoked, detailing the underlying message format, the retrieval logic, and the code needed to automatically forward the original content to your file helper.
Background
WeChat, launched by Tencent in 2011, now has over a billion daily active users and many features such as Moments, Mini Programs, and more. In 2014 it introduced the 撤回消息 (message revocation) feature, which replaces a withdrawn message with a system notice.
Design
The goal is to capture a revoked message as soon as it occurs and forward the original content and sender information to the file helper (or optionally back to the sender), making it easy to review.
Implementation
1. How WeChat Revokes Messages
When a user clicks revoke, the client sends a system message like "xxx撤回了一条消息". The receiving client parses this message and replaces the original display.
"xxx撤回了一条消息" is a system notification that contains the original message ID and replacement text.
2. Finding the Revoked Message
By comparing the msgid in the revocation notice with the IDs of recent messages cached by the wxpy library (default cache of the last 200 messages), we can locate the original message. If the message volume exceeds 200 within two minutes, increase the max_history parameter.
3. Forwarding the Revoked Message
Once the original message is identified, forwarding is a single line of code using wxpy:
msg.forward(bot.file_helper)4. Full Code Example
The complete script (with detailed comments) is provided below. It sets up the bot, listens for system messages of type revokemsg, extracts the original msgid, retrieves the matching message from the cache, and forwards it.
# Example code (simplified)
from wxpy import Bot
bot = Bot()
@bot.register(msg_types=NOTE)
def on_revoke(msg):
# parse XML, get original msgid, find in cache, forward
pass
bot.join()Verification
Running the script shows that when a friend revokes a message, the original text appears in the file helper, confirming the detection works.
Conclusion
The WeChat anti‑revoke feature is simple yet useful. Current limitations include the need to run the script on a continuously active machine. Future improvements could involve deploying the bot on a cloud server or packaging it as an APK for easier use.
Project repository: https://github.com/pig6/wxrobot
Documentation for wxpy: https://wxpy.readthedocs.io/zh/latest
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 Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
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.
