SoFunction
Updated on 2024-11-19

python using wxpy to implement wechat message anti-withdrawal scripts

In this article, we share the example of python to achieve the WeChat message anti-withdrawal of the specific code for your reference, the specific content is as follows

Used sqlite3 to save the data and when someone withdraws the message take out the data and send it to the file transfer assistant.

The file will be saved locally first, and the voice will be sent as a file.

wxpy and itchat haven't been updated for a long time, some of the features don't work anymore, and I don't know when webchat will be cool.

Helpful information is in the notes.

# -*- coding: utf-8 -*-
 
# Use sqlite3 to save the message, when someone withdraws the message retrieve if the message exists in the database by ID, if it exists then send the withdrawn message to the file helper.
# Currently only supported text picture map sharing recording video attachment Type of message。
 
import wxpy
import sqlite3
import os
import re
 
 
# Preparations
# Create attachment directory for storing images, maps/locations, shares, voice, video, files
if not ('attachment'):
  ('attachment')
attachment_path = ((), 'attachment')
bot = ()
# Used to get the msg ID
pattern = (r'\d{19}')
# Test wxpy to see if it works
myself = ()[0]
('Hello?')
 
# Create database and message table
try:
  conn = ('')
  cursor = ()
  # ('DROP TABLE MESSAGES')
  ("""CREATE TABLE IF NOT EXISTS MESSAGES (id INTEGER PRIMARY KEY AUTOINCREMENT,
           msg_id INTEGER NOT NULL,
           msg_text TEXT,
           create_time DATE NOT NULL,
           revoke_time DATE,
           attachment_path TEXT,
           msg_sender TEXT NOT NULL,
           msg_type TEXT NOT NULL,
           msg_url TEXT,
           msg_raw_data TEXT NOT NULL)""")
  # print('establish successfully')
finally:
  ()
  ()
  ()
 
# Register all messages, all supported messages will be inserted during the run of the program
@()
def store_data(msg):
  # print()
  # Insert data into the database if the message is a supported type
  if  in [, , , , , , ]:
    insert_data(msg)
  # The withdrawn message type is note
  elif  == :
    send_revoke(msg)
 
# Insert data
def insert_data(msg):
  try:
    conn = ('')
    cursor = ()
    if  == :
      ("INSERT INTO MESSAGES (msg_id, msg_text, create_time, msg_sender, msg_type, msg_raw_data)\
              values (?, ?, ?, ?, ?, ?)", (, , msg.create_time, str()[9:-1],
                             , str()))
 
    # Download recordings/images/files/videos locally, insert save path.
    elif  in [, , , ]:
      save_path = (attachment_path, msg.file_name)
      msg.get_file(save_path)
      ('INSERT INTO MESSAGES (msg_id, create_time, attachment_path, msg_sender, msg_type,\
              msg_raw_data) values (?, ?, ?, ?, ?, ?)',
              (, msg.create_time, save_path, str()[9:-1], , str()))
 
    # Insert share/location link
    elif  in [, ]:
      ('INSERT INTO MESSAGES (msg_id, msg_text, create_time, msg_sender, msg_type, msg_url,\
              msg_raw_data) values (?, ?, ?, ?, ?, ?, ?)',
              (, , msg.create_time, str()[9:-1], , str(), str()))
    # print('insert data successfully')
 
  finally:
    ()
    ()
    ()
 
# Retrieve whether the message exists in the database and if so send the withdrawn message to the File Transfer Assistant.
def send_revoke(message):
  msg_id = (['Content']).group()
  try:
    conn = ('')
    cursor = ()
    ('INSERT INTO MESSAGES (msg_id, create_time, msg_sender, msg_type, msg_raw_data)\
            values (?, ?, ?, ?, ?)',
            (, message.create_time, str()[9:-1], , str()))
    msg_data = ('SELECT * FROM MESSAGES WHERE msg_id=?', (msg_id, )).fetchall()
    # print('take out data successfully')
  finally:
    ()
    ()
    ()
  if msg_data[0][7] == 'Text':
    msg_info = 'I'll tell you a secret. {} exist {} Text withdrawn\n{}'.format(msg_data[0][6], msg_data[0][3], msg_data[0][2])
    bot.file_helper.send(msg_info)
  else:
    send_revoke_nontext(msg_data)
 
# of non-text messages sent
def send_revoke_nontext(msg_data):
  if msg_data[0][7] == 'Picture':
    if msg_data[0][5][-4:] == '.gif':
      # Now wxpy & itchat can't send GIFs #
      bot.file_helper('We're sorry, but the withdrawal of emoticons (gifs) for re-posting is not supported for the time being.')
    else:
      msg_info = 'I'll tell you a secret. {} exist {} Image withdrawn'.format(msg_data[0][6], msg_data[0][3])
      bot.file_helper.send(msg_info)
      bot.file_helper.send_image(msg_data[0][5])
  elif msg_data[0][7] == 'Recording':
    msg_info = 'I'll tell you a secret. {} exist {} Voice withdrawn.'.format(msg_data[0][6], msg_data[0][3])
    bot.file_helper.send(msg_info)
    bot.file_helper.send_file(msg_data[0][5])
  elif msg_data[0][7] == 'Attachment':
    msg_info = 'I'll tell you a secret. {} exist {} Document withdrawn'.format(msg_data[0][6], msg_data[0][3])
    bot.file_helper.send(msg_info)
    bot.file_helper.send_file(msg_data[0][5])
  elif msg_data[0][7] == 'Video':
    msg_info = 'I'll tell you a secret. {} exist {} The video was withdrawn'.format(msg_data[0][6], msg_data[0][3])
    bot.file_helper.send(msg_info)
    bot.file_helper.send_video(msg_data[0][5])
  elif msg_data[0][7] == 'Sharing':
    msg_info = 'I'll tell you a secret. {} exist {} Withdrawn from sharing\n{}\n{}'.format(msg_data[0][6], msg_data[0][3], msg_data[0][2],\
                             msg_data[0][8])
    bot.file_helper.send(msg_info)
  elif msg_data[0][7] == 'Map':
    msg_info = 'I'll tell you a secret. {} exist {} Position withdrawn\n{}\n{}'.format(msg_data[0][6], msg_data[0][3], msg_data[0][2],\
                             msg_data[0][8])
    bot.file_helper.send(msg_info)
 
 
()

This is the whole content of this article.