在微信上突然看到“XXX撤回一條消息”的時(shí)候,心里癢不癢?現(xiàn)在就教你如何實(shí)現(xiàn)消息防撤回的功能。不限手機(jī)型號(hào)、手機(jī)無需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'] # 在好友列表中查詢發(fā)送信息的好友昵稱
msg_time = msg['CreateTime'] # 信息發(fā)送的時(shí)間
msg_id = msg['MsgId'] # 每條信息的id
msg_content = None # 儲(chǔ)存信息的內(nèi)容
msg_share_url = None # 儲(chǔ)存分享的鏈接,比如分享的文章和音樂
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
##將信息存儲(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)聽是否有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就是這個(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ǔ)存分享的鏈接,比如分享的文章和音樂
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
##將信息存儲(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)聽是否有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就是這個(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()另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享文章:Python實(shí)現(xiàn)微信防撤回功能-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://chinadenli.net/article2/pccic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、微信公眾號(hào)、網(wǎng)站制作、商城網(wǎng)站、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容