SoFunction
Updated on 2024-11-19

python implementation of the file assistant in the view wechat withdrawal message

Using python to realize anti-withdrawal, the other party's withdrawn message can be viewed in their own wechat file transfer assistant.

If you want to turn it into an executable file and run it on your computer, use pyinstaller to package this program into an exe file.

pyinstaller filename.py -F

After executing the program, message anti-withdrawal is activated.

Full code of the program

# -*-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 = r"G:\python code\WeChat\withdraw to file assistant"
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 = "Tell you a secret~" + "\n" \
      + old_msg.get('msg_from') + " Withdrawn." + old_msg.get("msg_type") + " News " + "\n" \
      + old_msg.get('msg_time_rec') + "\n" \
      + "Withdrew what ⇣" + "\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()
 itchat.get_QR
 ()

This is the whole content of this article.