78 lines of Python code to help you reproduce WeChat message withdrawal!

78 lines of Python code to help you reproduce WeChat message withdrawal!

Python once said to me: "Time is running out, hurry up and use Python." So I saw an open source WeChat library based on Python: itchat. I played with it for a day and made a program that can collect the withdrawn private chat information and send it to the file transfer assistant of personal WeChat, including:

  • who :who sent it
  • when: when the message is sent
  • what: what information
  • which: which type of information, including: text, pictures, voice, video, sharing, location, attachments...

01 Code Implementation

  1. # -*-encoding:utf-8-*-
  2. import os
  3. import re
  4. import shutil
  5. import time  
  6. import itchat
  7. from itchat.content import *
  8.  
  9. # Note: Text, voice, video, picture, location, business card, share, and attachment can be withdrawn
  10.  
  11. # {msg_id:(msg_from,msg_to,msg_time,msg_time_rec,msg_type,msg_content,msg_share_url)}
  12. msg_dict = {}
  13.  
  14. # Temporary directory for file storage
  15. rev_tmp_dir = "/home/alic/RevDir/"  
  16. if not os.path.exists(rev_tmp_dir): os.mkdir(rev_tmp_dir)
  17.  
  18. # There is a problem with the expression | The msg_id of receiving the message and receiving the note are inconsistent. Coincidental solution
  19. face_bug = None
  20.  
  21. # Store the received messages in the dictionary. When a new message is received, clear the timed messages in the dictionary. | Do not accept messages that do not have the recall function.
  22.  
  23. # [TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS, NOTE]
  24. @itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO])
  25. def handler_receive_msg(msg):
  26. global face_bug
  27. # Get the local timestamp and format the local timestamp e: 2017-04-21 21:30:08
  28. msg_time_rec = time .strftime( "%Y-%m-%d %H:%M:%S" , time .localtime())
  29. # Message ID
  30. msg_id = msg[ 'MsgId' ]
  31. # Message time
  32. msg_time = msg[ 'CreateTime' ]
  33. # Nickname of the message sender | You can also use RemarkName here, but for yourself or someone without a remark, it is None
  34. msg_from = (itchat.search_friends(userName=msg[ 'FromUserName' ]))[ "NickName" ]
  35. # Message content
  36. msg_content = None
  37. # Shared link
  38. msg_share_url = None
  39. if msg[ 'Type' ] == 'Text' \
  40. or msg[ 'Type' ] == 'Friends' :
  41. msg_content = msg[ 'Text' ]
  42. elif msg[ 'Type' ] == 'Recording' \
  43. or msg[ 'Type' ] == 'Attachment' \
  44. or msg[ 'Type' ] == 'Video' \
  45. or msg[ 'Type' ] == 'Picture' :
  46. msg_content = r "" + msg[ 'FileName' ]
  47. # Save the file
  48. msg[ 'Text' ](rev_tmp_dir + msg[ 'FileName' ])
  49. elif msg[ 'Type' ] == 'Card' :
  50. msg_content = msg[ 'RecommendInfo' ][ 'NickName' ] + r "Business card"  
  51. elif msg[ 'Type' ] == 'Map' :
  52. x, y, location = re.search(
  53. "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*" , msg[ 'OriContent' ]). group (1, 2, 3)
  54. if location is None:
  55. msg_content = r "latitude->" + x.__str__() + "longitude->" + y.__str__()
  56. else :
  57. msg_content = r "" + location
  58. elif msg[ 'Type' ] == 'Sharing' :
  59. msg_content = msg[ 'Text' ]
  60. msg_share_url = msg[ 'Url' ]
  61. face_bug = msg_content
  62. # Update dictionary
  63. msg_dict.update (
  64. {
  65. msg_id: {
  66. "msg_from" : msg_from, "msg_time" : msg_time, "msg_time_rec" : msg_time_rec,
  67. "msg_type" : msg[ "Type" ],
  68. "msg_content" : msg_content, "msg_share_url" : msg_share_url
  69. }
  70. }
  71. )
  72.  
  73. # Receive note notification messages, determine whether to withdraw and perform corresponding operations
  74.  
  75. @itchat.msg_register([NOTE])
  76. def send_msg_helper(msg):
  77. global face_bug
  78. if re.search(r "\<\!\[CDATA\[.*Retracted a message\]\]\>" , msg[ 'Content' ]) is   not None:
  79. # Get the message id
  80. old_msg_id = re.search( "\<msgid\>(.*?)\<\/msgid\>" , msg[ 'Content' ]). group (1)
  81. old_msg = msg_dict.get(old_msg_id, {})
  82. if len(old_msg_id) < 11:
  83. itchat.send_file(rev_tmp_dir + face_bug, toUserName= 'filehelper' )
  84. os.remove(rev_tmp_dir + face_bug)
  85. else :
  86. msg_body = "Tell you a secret~" + "\n" \
  87. + old_msg.get( 'msg_from' ) + "Withdrawn" + old_msg.get( "msg_type" ) + "Message" + "\n" \
  88. + old_msg.get( 'msg_time_rec' ) + "\n" \
  89. + "What was withdrawn ⇣" + "\n" \
  90. + r "" + old_msg.get( 'msg_content' )
  91. # If the link exists,
  92. if old_msg[ 'msg_type' ] == "Sharing" : msg_body += "\nThis is the link ➣ " + old_msg.get( 'msg_share_url' )
  93.  
  94. # Send the recall message to the file assistant
  95. itchat.send(msg_body, toUserName= 'filehelper' )
  96. # If there is a file, send it back
  97. if old_msg[ "msg_type" ] == "Picture" \
  98. or old_msg[ "msg_type" ] == "Recording" \
  99. or old_msg[ "msg_type" ] == "Video" \
  100. or old_msg[ "msg_type" ] == "Attachment" :
  101. file = '@fil@%s' % (rev_tmp_dir + old_msg[ 'msg_content' ])
  102. itchat.send(msg=file, toUserName= 'filehelper' )
  103. os.remove(rev_tmp_dir + old_msg[ 'msg_content' ])
  104. # Delete old dictionary messages
  105. msg_dict.pop(old_msg_id)
  106.  
  107. if __name__ == '__main__' :
  108. itchat.auto_login(hotReload= True ,enableCmdQR=2)
  109. itchat.run()

The program can be run directly in the terminal. You can log in successfully by scanning the code in the terminal. It can also be packaged and run in the window system (note to modify the path, it is recommended to use a relative path).

  1. ➜ ~ python wx.py
  2. Getting uuid of QR code.
  3. Downloading QR code.
  4. Please scan the QR code to log in .
  5. Please press confirm on your phone.
  6. Loading the contact, this may take a little while.
  7. �[3;J
  8. Login successfully as AlicFeng
  9. Start auto replying.

02 Rendering

03 itchat

The above are all trivial things in programming logic. I will still record the itchat WeChat open source library.

1. Introduction

itchat is an open source WeChat personal account interface. It is very simple to call WeChat using Python. It is easy to use itchat code to build an instant messaging based on WeChat. What is more, it is convenient to expand the communication functions of personal WeChat on other platforms.

2. Installation

pip3 install itchat

3. itchat - Helloworld

Just three lines of code to send a message to the file transfer assistant.

  1. import itchat
  2. itchat.auto_login(hotReload= True )
  3. itchat.send( 'Hello AlicFeng' , toUserName= 'filehelper' )

4. View the client

[[237838]]

The most important thing to learn is the API manual:

Github for itchat:

  • https://github.com/liduanwei/ItChat

Chinese API:

  • http://itchat.readthedocs.io/zh/latest/

<<:  What does the Android system API level mean? What benefits can it bring to users?

>>:  Want to become an independent iOS developer? These learning resources will help you get started faster

Recommend

How to use Vim on Android to improve development efficiency

background Although I am an Android development e...

“Three small screens” divert the number of people watching TV

With the development of the Internet and intellig...

Better than the built-in file manager in Windows 11? Files App

I think everyone is using Windows' file explo...

Sogou Promotion PC multi-picture promotion style display!

Sogou Promotion PC Multi-Picture Promotion Style ...

World Water Day: Here’s what you need to know about groundwater

There is a kind of water, deep underground It is ...

When there is a traffic jam, what is the first car doing?

The Spring Festival holiday is over and it is the...

5 core skills that a novice needs to advance to an operations expert

In recent years, operations positions have been v...