I came to the library early this morning , Think about a simple and powerful tool of wechat , There are still a lot of news on wechat recently , such as : Applet
、 time axis
etc. , This is not the point , The point is to see one based on python Wechat open source library :itchat
, Played all day .Python Once said to me :" There is not much time , Hasten to use Python".
So let's use
itchat
Make such a program :undefined The information withdrawn from private chat can be collected and sent to the file assistant of personal wechat , Include :undefined(1) who : Who sent undefined(2) when : When was the message sent undefined(3) what: What information undefined(4) which: What kind of information , Include : Text 、 picture 、 voice 、 video 、 Share 、 Location 、 The attachment undefined...
# -*-encoding:utf-8-*- import os import re import shutil import time import itchat from itchat.content import * # explain : There is text that can be withdrawn 、 voice 、 video 、 picture 、 Location 、 Business card 、 Share 、 The attachment # {msg_id:(msg_from,msg_to,msg_time,msg_time_rec,msg_type,msg_content,msg_share_url)} msg_dict = {} # File storage temporary directory rev_tmp_dir = "/home/alic/RevDir/" if not os.path.exists(rev_tmp_dir): os.mkdir(rev_tmp_dir) # There's a problem with the expression | Receive information and accept note Of msg_id atypism Coincidence solution face_bug = None # Store the received message in the dictionary , When a new message is received, the time-out message in the dictionary is cleaned up | Do not accept information without withdrawal function # [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 # Get the local timestamp and format the local timestamp e: 2017-04-21 21:30:08 msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # news ID msg_id = msg['MsgId'] # Time of news msg_time = msg['CreateTime'] # Sender's nickname | It can also be used here RemarkName remarks But yourself or someone without a note None msg_from = (itchat.search_friends(userName=msg['FromUserName']))["NickName"] # The message content msg_content = None # Shared Links 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 file msg['Text'](rev_tmp_dir + msg['FileName']) elif msg['Type'] == 'Card': msg_content = msg['RecommendInfo']['NickName'] + r" Business card of " elif msg['Type'] == 'Map': x, y, location = re.search( "<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 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 } } ) # received note Notification messages , Determine whether to withdraw and operate accordingly @itchat.msg_register([NOTE]) def send_msg_helper(msg): global face_bug if re.search(r"\<\!\[CDATA\[.* Recalled a message \]\]\>", msg['Content']) is not None: # Get the message id old_msg_id = re.search("\<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') os.remove(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" \ + " Withdraw what ⇣" + "\n" \ + r"" + old_msg.get('msg_content') # If it's sharing, there are links if old_msg['msg_type'] == "Sharing": msg_body += "\n This is the link * " + old_msg.get('msg_share_url') # Send the recall message to the file assistant itchat.send(msg_body, toUserName='filehelper') # If you have documents, send them back 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 = '@[email protected]%s' % (rev_tmp_dir + old_msg['msg_content']) itchat.send(msg=file, toUserName='filehelper') os.remove(rev_tmp_dir + old_msg['msg_content']) # Delete dictionary old messages msg_dict.pop(old_msg_id) if __name__ == '__main__': itchat.auto_login(hotReload=True,enableCmdQR=2) itchat.run()
The program can be run directly at the terminal , After scanning the code on the terminal, you can log in successfully , It can also be packaged in window System operation ( Pay attention to modifying the path , Relative path is recommended ).
* ~ python wx.py Getting uuid of QR code. Downloading QR code. Please scan the QR code to log in. Please press confirm on your phone. Loading the contact, this may take a little while. �[3;J Login successfully as AlicFeng Start auto replying.
It's all about programming logic , I'd better record itchat Wechat is an open source library .
pip3 install itchat
import itchat itchat.auto_login(hotReload=True) itchat.send('Hello AlicFeng', toUserName='filehelper')
If you find something wrong or don't understand , In the comments section , Let's talk !
If the article helps you , give the thumbs-up + Focus on , Your support is my greatest motivation