溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python 實現(xiàn)微信防撤回功能

發(fā)布時間:2020-09-24 09:56:28 來源:腳本之家 閱讀:168 作者:壹言 欄目:開發(fā)技術(shù)

在微信上突然看到“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 

Python 實現(xiàn)微信防撤回功能

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

Python 實現(xiàn)微信防撤回功能

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

Python 實現(xiàn)微信防撤回功能

4.運(yùn)行代碼

1)掃碼登錄

Python 實現(xiàn)微信防撤回功能

2)登錄成功

Python 實現(xiàn)微信防撤回功能

效果

發(fā)送消息 

Python 實現(xiàn)微信防撤回功能 

收到消息 

Python 實現(xiàn)微信防撤回功能 

開始撤回 

 Python 實現(xiàn)微信防撤回功能 

撤回通知

Python 實現(xiàn)微信防撤回功能

總結(jié)

以上所述是小編給大家介紹的Python 實現(xiàn)微信防撤回功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI