Python 實現(xiàn)微信防撤回功能
在微信上突然看到“XXX撤回一條消息”的時候,心里癢不癢?現(xiàn)在就教你如何實現(xiàn)消息防撤回的功能。不限手機(jī)型號、手機(jī)無需root,只要微信號可以登上網(wǎng)頁版就可以了(部分不常用和新申請的賬號會登不上網(wǎng)頁版)。
環(huán)境
Centos7
python2.7
itchat
代碼
# coding:utf-8
import itchat
from itchat.content import TEXT
from itchat.content import *
import sys
import time
import re
reload(sys)
sys.setdefaultencoding('utf8')
import os
msg_information = {}
face_bug = None # 針對表情包的內(nèi)容
@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'] # 在好友列表中查詢發(fā)送信息的好友昵稱
msg_time = msg['CreateTime'] # 信息發(fā)送的時間
msg_id = msg['MsgId'] # 每條信息的id
msg_content = None # 儲存信息的內(nèi)容
msg_share_url = None # 儲存分享的鏈接,比如分享的文章和音樂
print msg['Type']
print msg['MsgId']
if msg['Type'] == 'Text' or msg['Type'] == 'Friends': # 如果發(fā)送的消息是文本或者好友推薦
msg_content = msg['Text']
print msg_content
# 如果發(fā)送的消息是附件、視屏、圖片、語音
elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \
or msg['Type'] == 'Picture' \
or msg['Type'] == 'Recording':
msg_content = msg['FileName'] # 內(nèi)容就是他們的文件名
msg['Text'](str(msg_content)) # 下載文件
# print msg_content
elif msg['Type'] == 'Card': # 如果消息是推薦的名片
msg_content = msg['RecommendInfo']['NickName'] + '的名片' # 內(nèi)容就是推薦人的昵稱和性別
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 location is None:
msg_content = r"緯度->" + x.__str__() + " 經(jīng)度->" + y.__str__() # 內(nèi)容為詳細(xì)的地址
else:
msg_content = r"" + location
elif msg['Type'] == 'Sharing': # 如果消息為分享的音樂或者文章,詳細(xì)的內(nèi)容為文章的標(biāo)題或者是分享的名字
msg_content = msg['Text']
msg_share_url = msg['Url'] # 記錄分享的url
print msg_share_url
face_bug = msg_content
##將信息存儲在字典中,每一個msg_id對應(yīng)一條信息
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
}
}
)
##這個是用于監(jiān)聽是否有friend消息撤回
@itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True)
def information(msg):
# 這里如果這里的msg['Content']中包含消息撤回和id,就執(zhí)行下面的語句
if '撤回了一條消息' in msg['Content']:
old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) # 在返回的content查找撤回的消息的id
old_msg = msg_information.get(old_msg_id) # 得到消息
print old_msg
if len(old_msg_id) < 11: # 如果發(fā)送的是表情包
itchat.send_file(face_bug, toUserName='filehelper')
else: # 發(fā)送撤回的提示給文件助手
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中發(fā)送給文件助手
if old_msg['msg_type'] == "Sharing":
msg_body += "\n就是這個鏈接➣ " + old_msg.get('msg_share_url')
# 將撤回消息發(fā)送到文件助手
itchat.send_msg(msg_body, toUserName='filehelper')
# 有文件的話也要將文件發(fā)送回去
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.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isGroupChat=True)
def handle_receive_msg(msg):
global face_bug
msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 接受消息的時間
# groupid = msg['FromUserName']
# chatroom = itchat.search_chatrooms(userName=groupid)
msg_Actual_from = msg['ActualNickName']
# msg_Actual_from = msg['User']
# msg_from = msg_Actual_from['Self']['NickName']
msg_from = msg_Actual_from
msg_time = msg['CreateTime'] # 信息發(fā)送的時間
msg_id = msg['MsgId'] # 每條信息的id
msg_content = None # 儲存信息的內(nèi)容
msg_share_url = None # 儲存分享的鏈接,比如分享的文章和音樂
print msg['Type']
print msg['MsgId']
if msg['Type'] == 'Text' or msg['Type'] == 'Friends': # 如果發(fā)送的消息是文本或者好友推薦
msg_content = msg['Text']
print msg_content
# 如果發(fā)送的消息是附件、視屏、圖片、語音
elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \
or msg['Type'] == 'Picture' \
or msg['Type'] == 'Recording':
msg_content = msg['FileName'] # 內(nèi)容就是他們的文件名
msg['Text'](str(msg_content)) # 下載文件
# print msg_content
elif msg['Type'] == 'Card': # 如果消息是推薦的名片
msg_content = msg['RecommendInfo']['NickName'] + '的名片' # 內(nèi)容就是推薦人的昵稱和性別
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 location is None:
msg_content = r"緯度->" + x.__str__() + " 經(jīng)度->" + y.__str__() # 內(nèi)容為詳細(xì)的地址
else:
msg_content = r"" + location
elif msg['Type'] == 'Sharing': # 如果消息為分享的音樂或者文章,詳細(xì)的內(nèi)容為文章的標(biāo)題或者是分享的名字
msg_content = msg['Text']
msg_share_url = msg['Url'] # 記錄分享的url
print msg_share_url
face_bug = msg_content
##將信息存儲在字典中,每一個msg_id對應(yīng)一條信息
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
}
}
)
##這個是用于監(jiān)聽是否有Group消息撤回
@itchat.msg_register(NOTE, isGroupChat=True, isMpChat=True)
def information(msg):
# 這里如果這里的msg['Content']中包含消息撤回和id,就執(zhí)行下面的語句
if '撤回了一條消息' in msg['Content']:
old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) # 在返回的content查找撤回的消息的id
old_msg = msg_information.get(old_msg_id) # 得到消息
print old_msg
if len(old_msg_id) < 11: # 如果發(fā)送的是表情包
itchat.send_file(face_bug, toUserName='filehelper')
else: # 發(fā)送撤回的提示給文件助手
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中發(fā)送給文件助手
if old_msg['msg_type'] == "Sharing":
msg_body += "\n就是這個鏈接➣ " + old_msg.get('msg_share_url')
# 將撤回消息發(fā)送到文件助手
itchat.send_msg(msg_body, toUserName='filehelper')
# 有文件的話也要將文件發(fā)送回去
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)
# Main (enableCmdQr = True 時,將會生成二維碼圖片,如 =2 時二維碼亂碼的話 改為1 即可
itchat.auto_login(enableCmdQR=2, hotReload=True)
itchat.run()
如果想看懂代碼的話,在有python一點基礎(chǔ)的前提下搞懂 Python 中的 裝飾器 概念。下面是用到itchat中主要的一段代碼
def msg_register(self, msgType, isFriendChat=False, isGroupChat=False, isMpChat=False): ''' a decorator constructor return a specific decorator based on information given ''' if not (isinstance(msgType, list) or isinstance(msgType, tuple)): msgType = [msgType] def _msg_register(fn): for _msgType in msgType: if isFriendChat: self.functionDict['FriendChat'][_msgType] = fn if isGroupChat: self.functionDict['GroupChat'][_msgType] = fn if isMpChat: self.functionDict['MpChat'][_msgType] = fn if not any((isFriendChat, isGroupChat, isMpChat)): self.functionDict['FriendChat'][_msgType] = fn return fn return _msg_register
運(yùn)行
1.連接云服務(wù)器,安裝pip

2.使用pip 安裝 itchat庫 (一個利用微信網(wǎng)頁版api接口寫的,有興趣的話可以鼓搗鼓搗)

3.將腳本文件上傳到云服務(wù)器

4.運(yùn)行代碼
1)掃碼登錄

2)登錄成功

效果
發(fā)送消息
收到消息
開始撤回
撤回通知

總結(jié)
以上所述是小編給大家介紹的Python 實現(xiàn)微信防撤回功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
python數(shù)據(jù)可視化matplotlib繪制折線圖示例
這篇文章主要為大家介紹了python數(shù)據(jù)可視化matplotlib繪制折線圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Python實現(xiàn)刪除list列表重復(fù)元素的方法總結(jié)
在Python編程中,我們經(jīng)常需要處理列表中的重復(fù)元素,這篇文章為大家介紹了五種高效的方法來刪除列表中的重復(fù)元素,希望對大家有所幫助2023-07-07
python強(qiáng)大的hook函數(shù)使用及應(yīng)用場景
鉤子函數(shù)(hook function)是把我們自己實現(xiàn)的hook函數(shù)在某一時刻掛接到目標(biāo)掛載點上,這篇文章主要介紹了python強(qiáng)大的hook函數(shù)使用及應(yīng)用場景,需要的朋友可以參考下2023-05-05
python中pivot()函數(shù)基礎(chǔ)知識點
在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python中pivot()函數(shù)基礎(chǔ)知識點內(nèi)容,對此有興趣的朋友們可以參考學(xué)習(xí)下。2021-01-01

