用python實現(xiàn)微信消息防撤回
這個腳本是基于 itchat 庫來實現(xiàn)的。
itchat是一個開源的微信個人號接口维蒙,使用python調(diào)用微信從未如此簡單掰吕。
使用不到三十行的代碼果覆,你就可以完成一個能夠處理所有信息的微信機(jī)器人颅痊。
當(dāng)然,該api的使用遠(yuǎn)不止一個機(jī)器人局待,更多的功能等著你來發(fā)現(xiàn)斑响,比如這些。
該接口與公眾號接口itchatmp共享類似的操作方式钳榨,學(xué)習(xí)一次掌握兩個工具舰罚。
如今微信已經(jīng)成為了個人社交的很大一部分,希望這個項目能夠幫助你擴(kuò)展你的個人的微信號薛耻、方便自己的生活营罢。
準(zhǔn)備環(huán)境:
- 安裝itchat庫:
pip install itchat
代碼實現(xiàn):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re
import shutil
import time
import itchat
from itchat.content import *
# 定義一個字典,保存消息的信息饼齿。
msg_dict = {}
# 創(chuàng)建一個目錄饲漾,用于存放消息臨時文件。
rec_tmp_dir = "/Users/matianhe/itchat/rec_tmp/"
if not os.path.exists(rec_tmp_dir):
os.mkdir(rec_tmp_dir)
face_bug = None
# 注冊消息接收器
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING,
ATTACHMENT, VIDEO])
def handler_receive_msg(msg):
global face_bug
msg_time_rec = time.strftime("%Y-%m-%d %H:%M%S", time.localtime())
msg_id = msg['MsgId']
msg_time = msg['CreateTime']
msg_from = (itchat.search_friends(userName=msg['FromUserName']
))["NickName"]
msg_content = None
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']
msg['Text'](rec_tmp_dir + msg['FileName'])
elif msg['Type'] == 'Card':
msg_content = msg['RecommendInfo']['NickName'] + r" 的名片"
elif msg['Type'] == 'Map':
x, y, location = re.search("<location x=\"(.*?)\" y=\"(.*?\".*lable= \
\"(.*?)\".*", msg['OriContent']).group(1, 2,
3)
if location is None:
msg_content = r"緯度->" + x.__str__() + " 經(jīng)度->" + y.__str__()
else:
msg_content = r"" + location
elif msg['Type'] == 'Sharing':
msg_content = msg['Text']
msg_share_url = msg['Url']
face_bug = msg_content
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
}
})
@itchat.msg_register([NOTE])
def send_msg_helper(msg):
global face_bug
if re.search(r"\<\!\[CDATA\[.*撤回了一條消息\]\]\>", msg['Content']) \
is not None:
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(rec_tmp_dir + face_bug,
toUserName='filehelper')
os.remove(rev_tmp_dir + face_bug)
else:
msg_body = "有人撤回消息" + "\n" \
+ old_msg.get('msg_from') + " 撤回了 " \
+ old_msg.get('msg_type') + " 消息" + "\n" \
+ old_msg.get('msg_time_rec') + "\n" \
+ r"" + old_msg.get('msg_content')
if old_msg['msg_type'] == "Sharing":
msg_body += "\n就是這個連接->" + old_msg.get('msg_share_url')
itchat.send(msg_body, toUserName='filehelper')
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' % (rec_tmp_dir + old_msg['msg_content'])
itchat.send(msg=file, toUserName='filehelper')
os.remove(rec_tmp_dir + old_msg['msg_content'])
msg_dict.pop(old_msg_id)
if __name__ == "__main__":
itchat.auto_login(hotReload=True, enableCmdQR=2)
itchat.run()
運行程序缕溉,掃碼登錄
實現(xiàn)思路:
注冊兩個消息接收器考传,一個接收平時消息,并把消息信息存到字典里证鸥。另一個監(jiān)聽是否有撤回操作僚楞,如果有撤回操作則取出第一個監(jiān)聽器內(nèi)的消息,用文件傳輸助手發(fā)送到手機(jī)上枉层。