Python Script to Detect and Retrieve Recalled WeChat Messages
This article demonstrates how to use Python's itchat library to monitor WeChat Web, cache incoming messages, detect recall events, and resend the withdrawn content—including text, images, videos, and location data—via the filehelper, providing a complete code example.
This guide shows how to build a Python tool that logs into WeChat Web, caches all received messages, watches for recall notifications, and forwards the original content back to the user so that withdrawn messages can be recovered.
First, install the third‑party itchat module:
pip install itchat
Then import the required standard and third‑party modules:
import re import os import time import itchat import platform from itchat.content import *
The script creates a global dictionary msg_info to store message metadata and a variable face_package for possible emoji files.
Message handling is performed by handle_rsg(msg) , which extracts the sender, timestamps, message type, and content. It distinguishes text/friend‑share messages, attachments (pictures, videos, recordings), location maps, and shared links, printing each type and updating msg_info with a structure like:
{ msg_id: { "msg_from": msg_from, "msg_time_send": msg_time_send, "msg_time_receive": msg_time_receive, "msg_type": msg["Type"], "msg_content": msg_content, "msg_link": msg_link } }
Recall detection is registered with @itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True) . The monitor(msg) function checks if the incoming NOTE contains the phrase “撤回了一条消息”. If so, it extracts the recalled message ID, retrieves the original entry from msg_info , and formats a summary that includes the sender, message type, time, and content. For media messages, the script sends the original file back using itchat.send_file or itchat.send , and then removes the temporary file.
The main block decides the login method based on the operating system and starts the WeChat session:
if __name__ == '__main__': if platform.platform()[:7] == 'Windows': itchat.auto_login(enableCmdQR=False, hotReload=True) else: itchat.auto_login(enableCmdQR=True, hotReload=True) itchat.run()
Running this script logs in via QR code, continuously monitors incoming messages, and automatically forwards any recalled content to the filehelper, effectively preventing message loss.
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.