SoFunction
Updated on 2024-11-19

How to view WeChat message withdrawals in python

In this article, we share the specific code of python to view the withdrawal of WeChat messages for your reference, the specific content is as follows

1.Install itchat

itchat is an open source python wechat library , support for sending messages , pictures , videos , maps , business cards , files and so on , but also can realize the auto-reply and many other functions .

Saw one(computer) fileNot bad.

1). First install a pythondownload address

2). After successful installation, go to the directory

Install itchat: install itchat The picture below shows mine already installed (note: use version 3 and above to execute).

3. Open cmd and drag it in and press enter.

code implementation

Create a file with the following code to import

# -*-encoding:utf-8-*-
import os
import re
import shutil
import time
import itchat
from  import *

# Description: Can be withdrawn with text text, voice, video, image, location, business card, share, attachment

# {msg_id:(msg_from,msg_to,msg_time,msg_time_rec,msg_type,msg_content,msg_share_url)}
msg_dict = {}

# Temporary directory for file storage
rev_tmp_dir = "/RevDir/"
if not (rev_tmp_dir): (rev_tmp_dir)

# Expression has a problem | The msg_id of the accept message and the accept note do not match Coincidence Solution
face_bug = None


# Store incoming messages in a dictionary and clean up messages that time out in the dictionary when a new message is received | Do not accept messages that do not have a retraction feature
# [TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS, NOTE]
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO])
def handler_receive_msg(msg):
  global face_bug
  # Getting a local timestamp and formatting the local timestamp e: 2017-04-21 21:30:08
  msg_time_rec = ("%Y-%m-%d %H:%M:%S", ())
  # Message ID
  msg_id = msg['MsgId']
  # Message time
  msg_time = msg['CreateTime']
  # Nickname of the message sender | You can also use the RemarkName comment here, but yourself or anyone without a comment is None.
  msg_from = (itchat.search_friends(userName=msg['FromUserName']))["NickName"]
  # Message content
  msg_content = None
  # Links shared
  msg_share_url = None
  if msg['Type'] == 'Text' \
      or msg['Type'] == 'Friends':
    msg_content = msg['Text']
  elif msg['Type'] == 'Recording' \
      or msg['Type'] == 'Attachment' \
      or msg['Type'] == 'Video' \
      or msg['Type'] == 'Picture':
    msg_content = r"" + msg['FileName']
    # Save the document
    msg['Text'](rev_tmp_dir + msg['FileName'])
  elif msg['Type'] == 'Card':
    msg_content = msg['RecommendInfo']['NickName'] + r"'s business card."
  elif msg['Type'] == 'Map':
    x, y, location = (
      "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3)
    if location is None:
      msg_content = r"Latitude->" + x.__str__() + " Longitude->" + y.__str__()
    else:
      msg_content = r"" + location
  elif msg['Type'] == 'Sharing':
    msg_content = msg['Text']
    msg_share_url = msg['Url']
  face_bug = msg_content
  # Update the dictionary
  msg_dict.update(
    {
      msg_id: {
        "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec,
        "msg_type": msg["Type"],
        "msg_content": msg_content, "msg_share_url": msg_share_url
      }
    }
  )


# Receive a note notification class message, determine if it is withdrawn and act accordingly
@itchat.msg_register([NOTE])
def send_msg_helper(msg):
  global face_bug
  if (r"\<\! \[CDATA\[. *withdrew a message\]\]\>", msg['Content']) is not None:
    # Get the id of the message
    old_msg_id = ("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1)
    old_msg = msg_dict.get(old_msg_id, {})
    if len(old_msg_id) < 11:
      itchat.send_file(rev_tmp_dir + face_bug, toUserName='filehelper')
      (rev_tmp_dir + face_bug)
    else:
      msg_body = "Love you Rae Rae ⇣" + "\n" \
            + old_msg.get('msg_from') + " Withdrawn." + old_msg.get("msg_type") + " News " + "\n" \
            + old_msg.get('msg_time_rec') + "\n" \
            + "Content ⇣" + "\n" \
            + r"" + old_msg.get('msg_content')
      # If it's sharing a link that exists #
      if old_msg['msg_type'] == "Sharing": msg_body += "\n is this link ➣ " + old_msg.get('msg_share_url')

      # Send withdrawn messages to the file assistant
      (msg_body, toUserName='filehelper')
      # Send the file back if you have one #
      if old_msg["msg_type"] == "Picture" \
          or old_msg["msg_type"] == "Recording" \
          or old_msg["msg_type"] == "Video" \
          or old_msg["msg_type"] == "Attachment":
        file = '@fil@%s' % (rev_tmp_dir + old_msg['msg_content'])
        (msg=file, toUserName='filehelper')
        (rev_tmp_dir + old_msg['msg_content'])
      # Delete old dictionary messages
      msg_dict.pop(old_msg_id)


if __name__ == '__main__':
  itchat.auto_login(hotReload=True,enableCmdQR=1)
  ()

Just open cmd and drag the file in.

This is the whole content of this article.