要說微信最讓人惡心的發明,消息撤回絕對能上榜。
比如你現在正和女朋友用微信聊着天,或者跟自己喜歡的女孩子聊着天,一個不留神,你沒注意到對方發的消息就被她及時撤回了,這時你很好奇,好奇她到底發了什麼?于是你打算問問她發了什麼,結果她回一句"沒什麼"。這一回複,讓你的好奇心更加強烈了,頓時就感覺消息撤回這一功能就是用來折磨人的
那麼有沒有什麼辦法能夠知道你心愛的她(他)到底撤回了什麼呢?不要着急,Python幫你搞定
話不多說,直接上。
首先介紹itchat庫
itchat是一個開源的微信個人号接口,通過itchat可以實現微信(好友或微信群)的信息處理,包括文本、圖片、小視頻、地理位置消息、名片消息、語音消息、動畫表情、普通鍊接、音樂鍊接、群消息、紅包消息、系統消息等,可以對微信的消息進行獲取和回複。
itchat庫安裝:pip install itchat
代碼:
'''
Createdon 2019-7-10
@author: 北京-宏哥 QQ交流群:707699217
Project:學習和使用python
'''
# 3.導入模塊
import itchat
from itchat.content import *
import time
import re
import os
msg_information = {}
# 針對表情包的内容
face_bug = None
@itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isFriendChat=True,
isMpChat=True)
def handle_receive_msg(msg):
global face_bug
# 接收消息的時間
msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 在好友列表列表中查詢發送信息的好友昵稱
msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName']
# 信息發送的時間
msg_time = msg['CreateTime']
# 每條信息的ID
msg_id = msg['MsgId']
# 儲存信息的内容
msg_content = None
# 儲存分享的連接,比如分享的文章和音樂
msg_share_url = None
# 如果發送的消息是文本或者好友推薦
if msg['Type'] == 'Text' or msg['Type'] == 'Friends':
msg_content = msg['Text']
print(msg_content)
# 如果發送的消息是附件,視頻,圖片,語音
elif msg['Type'] == 'Attachment' or msg['Type'] == 'Video' \
or msg['Type'] == 'Picture' \
or msg['Type'] == 'Recording':
# 内容為下載文件名
msg_content = msg['FileName']
msg['Text'](str(msg_content))
# 如果消息是推薦的名片
elif msg['Type'] == 'Card':
# 内容是推薦人的昵稱和性别
msg_content = msg['RecommendInfo']['NickName'] '的名片'
if msg['RecommendInfo']['Sex'] == 1:
msg_content = '性别為男'
else:
msg_content = '性别為女'
print(msg_content)
# 如果消息為分享的位置信息
elif msg['Type'] == 'Map':
x, y, location = re.search(
"<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3)
if locationis None:
# 内容為詳細地址
msg_content = r'緯度->' x.__str__() "經度->" y.__str__()
else:
msg_content = r"" location
# 如果消息是分享的音樂或者文章,詳細的内容為文章的标題或者分享的名字
elif msg['Type'] == 'Sharing':
msg_content = msg['Text']
msg_share_url = msg['Url']
print(msg_share_url)
face_bug = msg_content
# 将信息存儲在字典中,每一個msg_id對應一條消息
msg_information.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
}
}
)
# 這個是用于監聽是否有friend消息撤回
@itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True)
def information(msg):
# 這裡如果這裡的msg['Content']中包含消息撤回和id,就執行下面的語句
if '撤回了一條消息' in msg['Content']:
old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1)
# 得到消息
old_msg = msg_information.get(old_msg_id)
print(old_msg)
# 如果發送的是表情
if len(old_msg_id) < 11:
itchat.send_file(face_bug, toUserName='filehelper')
# 發送撤回的提示給文件助手
else:
msg_body = "【" \
old_msg.get('msg_from') "撤回了】\n" \
old_msg.get("msg_type") "消息:" "\n" \
old_msg.get("msg_time_rec") "\n" \
r"" old_msg.get("msg_content")
# 如果分享的文件被撤回了,那麼就将分享的url加在msg_body中發送給文件助手
if old_msg['msg_type'] == "Sharing":
msg_body = "\n就是這個鍊接>" old_msg.get('msg_share_url')
# 将撤回消息發送到文件助手
itchat.send_msg(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" % (old_msg['msg_content'])
itchat.send(msg=file, toUserName='filehelper')
os.remove(old_msg['msg_content'])
# 删除字典舊信息
msg_information.pop(old_msg_id)
itchat.auto_login(hotReload=True)
itchat.run()
運行之後需要掃描二維碼登錄,登錄後微信手機助手就能監測到對方撤回的消息
掃碼登錄日志撤回測試:
下面介紹.py文件如何生成.exe文件,方便做成exe發送給其他人使用
pycharm編輯器Terminal窗口使用
pyinstaller -F .py文件所在絕對路徑
成功之後會在python安裝路徑下的dist文件看到生成的exe文件
注意:python3.6版本下的才能轉換成功
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!