最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2019年04月29日 10:07:01   作者:壹言  
在微信上突然看到“XXX撤回一條消息”的時候,心里癢不癢?現(xiàn)在就教你如何使用python實現(xiàn)消息防撤回的功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,感興趣的朋友一起看看吧

在微信上突然看到“XXX撤回一條消息”的時候,心里癢不癢?現(xiàn)在就教你如何實現(xiàn)消息防撤回的功能。不限手機型號、手機無需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)容為詳細的地址
 else:
  msg_content = r"" + location
 elif msg['Type'] == 'Sharing': # 如果消息為分享的音樂或者文章,詳細的內(nèi)容為文章的標題或者是分享的名字
 msg_content = msg['Text']
 msg_share_url = msg['Url'] # 記錄分享的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
  }
 }
 )
##這個是用于監(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)容為詳細的地址
 else:
  msg_content = r"" + location
 elif msg['Type'] == 'Sharing': # 如果消息為分享的音樂或者文章,詳細的內(nèi)容為文章的標題或者是分享的名字
 msg_content = msg['Text']
 msg_share_url = msg['Url'] # 記錄分享的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
  }
 }
 )
##這個是用于監(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一點基礎的前提下搞懂 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

運行

1.連接云服務器,安裝pip 

安裝pip

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

安裝itchat

3.將腳本文件上傳到云服務器

上傳

4.運行代碼

1)掃碼登錄

掃碼

2)登錄成功

運行

效果

發(fā)送消息 

發(fā)送 

收到消息 

收到 

開始撤回 

 撤回 

撤回通知

效果

總結

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

相關文章

  • python數(shù)據(jù)可視化matplotlib繪制折線圖示例

    python數(shù)據(jù)可視化matplotlib繪制折線圖示例

    這篇文章主要為大家介紹了python數(shù)據(jù)可視化matplotlib繪制折線圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Python?numpy生成矩陣基礎用法實例代碼

    Python?numpy生成矩陣基礎用法實例代碼

    矩陣是matrix類型的對象,該類繼承自numpy.ndarray,任何針對ndarray的操作,對矩陣對象同樣有效,下面這篇文章主要給大家介紹了關于Python?numpy生成矩陣基礎的相關資料,需要的朋友可以參考下
    2022-08-08
  • Python實現(xiàn)刪除list列表重復元素的方法總結

    Python實現(xiàn)刪除list列表重復元素的方法總結

    在Python編程中,我們經(jīng)常需要處理列表中的重復元素,這篇文章為大家介紹了五種高效的方法來刪除列表中的重復元素,希望對大家有所幫助
    2023-07-07
  • Python實現(xiàn)簡單猜數(shù)字游戲

    Python實現(xiàn)簡單猜數(shù)字游戲

    這篇文章主要為大家詳細介紹了Python實現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • 如何用Python合并lmdb文件

    如何用Python合并lmdb文件

    這篇文章主要為大家詳細介紹了如何用Python合并lmdb文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • python強大的hook函數(shù)使用及應用場景

    python強大的hook函數(shù)使用及應用場景

    鉤子函數(shù)(hook function)是把我們自己實現(xiàn)的hook函數(shù)在某一時刻掛接到目標掛載點上,這篇文章主要介紹了python強大的hook函數(shù)使用及應用場景,需要的朋友可以參考下
    2023-05-05
  • 詳解Python的Django框架中的中間件

    詳解Python的Django框架中的中間件

    這篇文章主要介紹了詳解Python的Django框架中的中間件,包括中間件的安裝等內(nèi)容,需要的朋友可以參考下
    2015-07-07
  • python中pivot()函數(shù)基礎知識點

    python中pivot()函數(shù)基礎知識點

    在本篇內(nèi)容里小編給大家分享的是一篇關于python中pivot()函數(shù)基礎知識點內(nèi)容,對此有興趣的朋友們可以參考學習下。
    2021-01-01
  • Python如何爬取b站熱門視頻并導入Excel

    Python如何爬取b站熱門視頻并導入Excel

    這篇文章主要介紹了Python如何爬取b站熱門視頻并導入Excel,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • Python中的min及返回最小值索引的操作

    Python中的min及返回最小值索引的操作

    這篇文章主要介紹了Python中的min及返回最小值索引的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05

最新評論

通化市| 临邑县| 栾城县| 潮安县| 调兵山市| 绥滨县| 黔西县| 巴中市| 泸水县| 沅陵县| 吴桥县| 南木林县| 太白县| 云南省| 武威市| 渭源县| 健康| 民和| 门源| 巩义市| 芦山县| 洮南市| 日土县| 八宿县| 农安县| 郁南县| 昔阳县| 江永县| 札达县| 虎林市| 宁明县| 潍坊市| 江川县| 阿克陶县| 涪陵区| 中西区| 建湖县| 黎城县| 商都县| 安达市| 安吉县|