This article is participating in Python Theme Month. See the link for details

I don’t know if you have tried not to pay attention to wechat information for a while, and the person who sent the message withdrew it in seconds. Then you wonder: What did you withdraw? I’m here to help you solve that problem.

So what has the Queen withdrawn?

Understand itchat

Here’s a new one: ItChat. What is this thing? To put it simply, it is a wechat robot. Itchat is an open source wechat id interface, and using Python to call wechat has never been easier. With less than 30 lines of code, you can build a wechat robot that can process all the information.

With ItChat, if you want to send a message to the File Transfer Assistant, just do this:

import itchat

itchat.auto_login()

itchat.send('Hello, filehelper', toUserName='filehelper')
Copy the code

If you want to reply to a text message sent to yourself, just do this:

import itchat
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
    return msg.text

itchat.auto_login()
itchat.run()
Copy the code

For more examples, you can go to the official website to view, here is its API address

The next thing you need to do is use this API to find the retraction information.

Listen for information and save information

The most important code interface is this: @itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO]) Def resever_info(MSG): This checks the received message, including text, language, and image. What we need to do is save everything we listen to.

Iid = MSG ['MsgId'] # itype = MSG ['Type'] # sname = MSG ['FileName'] # Susername = itchat.search_friends(userName= MSG ['FromUserName'])['NickName'] stime = MSG ['CreateTime'] # time_local = time.localtime(ticks)Copy the code

All this information can be saved in a dictionary.

Monitor system prompts to extract saved information

Next we need to monitor system prompts

@itchat.msg_register(NOTE) # Listen for system prompts

def note_info(msg):

Def note_info(MSG): def note_info(MSG): def note_info(MSG): Content = MSG [' content '] # . Doc = XML parsing XML dom. Minidom. ParseString (content) # remove msgid label value result = doc. GetElementsByTagName (" msgid ") # The msgId is the withdrawn message id, MsgId = result[0].childNodes[0].nodeValue # Retrieve message type msg_type = dict[msgId]['info_type'] if from dictionary Msg_type == 'Recording': # Retrieving messages for voice.... Elif msg_type == 'Text':# Withdraw the message as Text.... Elif msg_type == 'Picture':#Copy the code

The above idea is quite clear. It is to use itchat to monitor and save information, and then monitor the system prompt, and finally find the information that has been withdrawn from the system prompt.

Need to get the full source code, please go to the public number: poetic code. Since come in, original is not easy. Just give me a “like” and leave.