Backend Development 8 min read

Python itchat Tutorial: Capturing and Saving Recalled WeChat Messages

This article walks through using the Python itchat library to log into WeChat, monitor incoming messages, detect message recall events, and automatically save the recalled text, images, audio, or video files to a local directory, complete with full source code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python itchat Tutorial: Capturing and Saving Recalled WeChat Messages

The guide demonstrates how to reproduce a WeChat message recall detection tool using the itchat Python library. It starts by installing the library with pip install itchat and logging in via itchat.auto_login(True) .

After logging in, the script uses itchat.search_friends() and itchat.send() to verify basic functionality, then registers message handlers with @itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO]) to capture various message types and store their metadata in a dictionary.

A second handler @itchat.msg_register(NOTE) listens for system notifications indicating a message recall. When a recall is detected, the XML content of the notification is parsed to extract the original message ID, which is then used to retrieve the stored message details from the dictionary. Depending on the message type (text, picture, recording, etc.), the script saves the content to a predefined folder and forwards a summary and the saved file to the WeChat file helper.

The article also covers handling of image and audio files, creating the storage directory with os.mkdir() , and cleaning up the dictionary after processing each recalled message.

Finally, the complete source code is provided:

import itchat
from itchat.content import *
import os
import time
import xml.dom.minidom  # XML parsing

temp = '/Users/yourname/Documents/itchat' + '/' + '撤回的消息'
if not os.path.exists(temp):
    os.mkdir(temp)

itchat.auto_login(True)  # Auto login

dict = {}

@itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO])
def resever_info(msg):
    global dict
    info = msg['Text']
    msgId = msg['MsgId']
    info_type = msg['Type']
    name = msg['FileName']
    fromUser = itchat.search_friends(userName=msg['FromUserName'])['NickName']
    ticks = msg['CreateTime']
    time_local = time.localtime(ticks)
    dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
    dict[msgId] = {"info": info, "info_type": info_type, "name": name, "fromUser": fromUser, "dt": dt}

@itchat.msg_register(NOTE)
def note_info(msg):
    if '撤回了一条消息' in msg['Text']:
        content = msg['Content']
        doc = xml.dom.minidom.parseString(content)
        result = doc.getElementsByTagName("msgid")
        msgId = result[0].childNodes[0].nodeValue
        msg_type = dict[msgId]['info_type']
        if msg_type == 'Recording':
            recording_info = dict[msgId]['info']
            info_name = dict[msgId]['name']
            fromUser = dict[msgId]['fromUser']
            dt = dict[msgId]['dt']
            recording_info(temp + '/' + info_name)
            send_msg = '【发送人:】' + fromUser + '\n' + '发送时间:' + dt + '\n' + '撤回了一条语音'
            itchat.send(send_msg, 'filehelper')
            itchat.send_file(temp + '/' + info_name, 'filehelper')
            del dict[msgId]
            print("保存语音")
        elif msg_type == 'Text':
            text_info = dict[msgId]['info']
            fromUser = dict[msgId]['fromUser']
            dt = dict[msgId]['dt']
            send_msg = '【发送人:】' + fromUser + '\n' + '发送时间:' + dt + '\n' + '撤回内容:' + text_info
            itchat.send(send_msg, 'filehelper')
            del dict[msgId]
            print("保存文本")
        elif msg_type == 'Picture':
            picture_info = dict[msgId]['info']
            fromUser = dict[msgId]['fromUser']
            dt = dict[msgId]['dt']
            info_name = dict[msgId]['name']
            picture_info(temp + '/' + info_name)
            send_msg = '【发送人:】' + fromUser + '\n' + '发送时间:' + dt + '\n' + '撤回了一张图片'
            itchat.send(send_msg, 'filehelper')
            itchat.send_file(temp + '/' + info_name, 'filehelper')
            del dict[msgId]
            print("保存图片")

itchat.run()

Running the script results in real‑time detection of recalled messages, with the original content saved locally and a notification sent to the file helper, providing a practical solution for preserving important chat information.

backendAutomationWeChatitchatmessage-recall
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.