Python 實(shí)現(xiàn)微信防撤回功能
在微信上突然看到“XXX撤回一條消息”的時(shí)候,心里癢不癢?現(xiàn)在就教你如何實(shí)現(xiàn)消息防撤回的功能。不限手機(jī)型號(hào)、手機(jī)無(wú)需root,只要微信號(hào)可以登上網(wǎng)頁(yè)版就可以了(部分不常用和新申請(qǐng)的賬號(hào)會(huì)登不上網(wǎng)頁(yè)版)。
環(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 # 針對(duì)表情包的內(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()) # 接受消息的時(shí)間
msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName'] # 在好友列表中查詢(xún)發(fā)送信息的好友昵稱(chēng)
msg_time = msg['CreateTime'] # 信息發(fā)送的時(shí)間
msg_id = msg['MsgId'] # 每條信息的id
msg_content = None # 儲(chǔ)存信息的內(nèi)容
msg_share_url = None # 儲(chǔ)存分享的鏈接,比如分享的文章和音樂(lè)
print msg['Type']
print msg['MsgId']
if msg['Type'] == 'Text' or msg['Type'] == 'Friends': # 如果發(fā)送的消息是文本或者好友推薦
msg_content = msg['Text']
print msg_content
# 如果發(fā)送的消息是附件、視屏、圖片、語(yǔ)音
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)容就是推薦人的昵稱(chēng)和性別
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': # 如果消息為分享的音樂(lè)或者文章,詳細(xì)的內(nèi)容為文章的標(biāo)題或者是分享的名字
msg_content = msg['Text']
msg_share_url = msg['Url'] # 記錄分享的url
print msg_share_url
face_bug = msg_content
##將信息存儲(chǔ)在字典中,每一個(gè)msg_id對(duì)應(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
}
}
)
##這個(gè)是用于監(jiān)聽(tīng)是否有friend消息撤回
@itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True)
def information(msg):
# 這里如果這里的msg['Content']中包含消息撤回和id,就執(zhí)行下面的語(yǔ)句
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就是這個(gè)鏈接➣ " + 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()) # 接受消息的時(shí)間
# 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ā)送的時(shí)間
msg_id = msg['MsgId'] # 每條信息的id
msg_content = None # 儲(chǔ)存信息的內(nèi)容
msg_share_url = None # 儲(chǔ)存分享的鏈接,比如分享的文章和音樂(lè)
print msg['Type']
print msg['MsgId']
if msg['Type'] == 'Text' or msg['Type'] == 'Friends': # 如果發(fā)送的消息是文本或者好友推薦
msg_content = msg['Text']
print msg_content
# 如果發(fā)送的消息是附件、視屏、圖片、語(yǔ)音
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)容就是推薦人的昵稱(chēng)和性別
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': # 如果消息為分享的音樂(lè)或者文章,詳細(xì)的內(nèi)容為文章的標(biāo)題或者是分享的名字
msg_content = msg['Text']
msg_share_url = msg['Url'] # 記錄分享的url
print msg_share_url
face_bug = msg_content
##將信息存儲(chǔ)在字典中,每一個(gè)msg_id對(duì)應(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
}
}
)
##這個(gè)是用于監(jiān)聽(tīng)是否有Group消息撤回
@itchat.msg_register(NOTE, isGroupChat=True, isMpChat=True)
def information(msg):
# 這里如果這里的msg['Content']中包含消息撤回和id,就執(zhí)行下面的語(yǔ)句
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就是這個(gè)鏈接➣ " + 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 時(shí),將會(huì)生成二維碼圖片,如 =2 時(shí)二維碼亂碼的話 改為1 即可
itchat.auto_login(enableCmdQR=2, hotReload=True)
itchat.run()
如果想看懂代碼的話,在有python一點(diǎn)基礎(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庫(kù) (一個(gè)利用微信網(wǎng)頁(yè)版api接口寫(xiě)的,有興趣的話可以鼓搗鼓搗)

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

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

2)登錄成功

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

總結(jié)
以上所述是小編給大家介紹的Python 實(shí)現(xiàn)微信防撤回功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Pycharm中import torch報(bào)錯(cuò)的快速解決方法
這篇文章主要介紹了Pycharm中import torch報(bào)錯(cuò)的快速解決方法,很多朋友容易碰到這個(gè)問(wèn)題,今天小編特此把解決方案分享到腳本之家平臺(tái)供大家參考,需要的朋友可以參考下2020-03-03
Python面向?qū)ο蟪绦蛟O(shè)計(jì)多繼承和多態(tài)用法示例
這篇文章主要介紹了Python面向?qū)ο蟪绦蛟O(shè)計(jì)多繼承和多態(tài)用法,結(jié)合實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中多繼承、多態(tài)的概念、原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-04-04
python實(shí)現(xiàn)決策樹(shù)C4.5算法詳解(在ID3基礎(chǔ)上改進(jìn))
下面小編就為大家?guī)?lái)一篇python實(shí)現(xiàn)決策樹(shù)C4.5算法詳解(在ID3基礎(chǔ)上改進(jìn))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Python實(shí)現(xiàn)批量下載ts文件并合并為mp4
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Python語(yǔ)言實(shí)現(xiàn)批量下載ts文件并合并為mp4視頻的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-06-06
對(duì)Python3+gdal 讀取tiff格式數(shù)據(jù)的實(shí)例講解
今天小編就為大家分享一篇對(duì)Python3+gdal 讀取tiff格式數(shù)據(jù)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Python異步與定時(shí)任務(wù)提高程序并發(fā)性和定時(shí)執(zhí)行效率
Python異步與定時(shí)任務(wù)是Python編程中常用的兩種技術(shù),異步任務(wù)可用于高效處理I/O密集型任務(wù),提高程序并發(fā)性;定時(shí)任務(wù)可用于定時(shí)執(zhí)行計(jì)劃任務(wù),提高程序的執(zhí)行效率。這兩種技術(shù)的應(yīng)用有助于提升Python程序的性能和效率2023-05-05
python使用tqdm模塊處理文件閱讀進(jìn)度條顯示
這篇文章主要為大家介紹了python使用tqdm模塊處理文件閱讀顯示進(jìn)度條示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
使用Python寫(xiě)一個(gè)貪吃蛇游戲?qū)嵗a
這篇文章主要介紹了使用Python寫(xiě)一個(gè)貪吃蛇游戲?qū)嵗a,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-08-08

