Python 實現微信防撤回功能
在微信上突然看到“XXX撤回一條訊息”的時候,心裡癢不癢?現在就教你如何實現訊息防撤回的功能。不限手機型號、手機無需root,只要微訊號可以登上網頁版就可以了(部分不常用和新申請的賬號會登不上網頁版)。
環境
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 # 針對表情包的內容
@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'] # 在好友列表中查詢傳送資訊的好友暱稱
msg_time = msg['CreateTime'] # 資訊傳送的時間
msg_id = msg['MsgId'] # 每條資訊的id
msg_content = None # 儲存資訊的內容
msg_share_url = None # 儲存分享的連結,比如分享的文章和音樂
print msg['Type']
print msg['MsgId']
if msg['Type'] == 'Text' or msg['Type'] == 'Friends': # 如果傳送的訊息是文字或者好友推薦
msg_content = msg['Text']
print msg_content
# 如果傳送的訊息是附件、視屏、圖片、語音
elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \
or msg['Type'] == 'Picture' \
or msg['Type'] == 'Recording':
msg_content = msg['FileName'] # 內容就是他們的檔名
msg['Text'](str(msg_content)) # 下載檔案
# print msg_content
elif msg['Type'] == 'Card': # 如果訊息是推薦的名片
msg_content = msg['RecommendInfo']['NickName'] + '的名片' # 內容就是推薦人的暱稱和性別
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__() + " 經度->" + y.__str__() # 內容為詳細的地址
else:
msg_content = r"" + location
elif msg['Type'] == 'Sharing': # 如果訊息為分享的音樂或者文章,詳細的內容為文章的標題或者是分享的名字
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
}
}
)
##這個是用於監聽是否有friend訊息撤回
@itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True)
def information(msg):
# 這裡如果這裡的msg['Content']中包含訊息撤回和id,就執行下面的語句
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: # 如果傳送的是表情包
itchat.send_file(face_bug, toUserName='filehelper')
else: # 傳送撤回的提示給檔案助手
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中傳送給檔案助手
if old_msg['msg_type'] == "Sharing":
msg_body += "\n就是這個連結➣ " + old_msg.get('msg_share_url')
# 將撤回訊息傳送到檔案助手
itchat.send_msg(msg_body, toUserName='filehelper')
# 有檔案的話也要將檔案傳送回去
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 = '@[email protected]%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'] # 資訊傳送的時間
msg_id = msg['MsgId'] # 每條資訊的id
msg_content = None # 儲存資訊的內容
msg_share_url = None # 儲存分享的連結,比如分享的文章和音樂
print msg['Type']
print msg['MsgId']
if msg['Type'] == 'Text' or msg['Type'] == 'Friends': # 如果傳送的訊息是文字或者好友推薦
msg_content = msg['Text']
print msg_content
# 如果傳送的訊息是附件、視屏、圖片、語音
elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \
or msg['Type'] == 'Picture' \
or msg['Type'] == 'Recording':
msg_content = msg['FileName'] # 內容就是他們的檔名
msg['Text'](str(msg_content)) # 下載檔案
# print msg_content
elif msg['Type'] == 'Card': # 如果訊息是推薦的名片
msg_content = msg['RecommendInfo']['NickName'] + '的名片' # 內容就是推薦人的暱稱和性別
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__() + " 經度->" + y.__str__() # 內容為詳細的地址
else:
msg_content = r"" + location
elif msg['Type'] == 'Sharing': # 如果訊息為分享的音樂或者文章,詳細的內容為文章的標題或者是分享的名字
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
}
}
)
##這個是用於監聽是否有Group訊息撤回
@itchat.msg_register(NOTE, isGroupChat=True, isMpChat=True)
def information(msg):
# 這裡如果這裡的msg['Content']中包含訊息撤回和id,就執行下面的語句
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: # 如果傳送的是表情包
itchat.send_file(face_bug, toUserName='filehelper')
else: # 傳送撤回的提示給檔案助手
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中傳送給檔案助手
if old_msg['msg_type'] == "Sharing":
msg_body += "\n就是這個連結➣ " + old_msg.get('msg_share_url')
# 將撤回訊息傳送到檔案助手
itchat.send_msg(msg_body, toUserName='filehelper')
# 有檔案的話也要將檔案傳送回去
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 = '@[email protected]%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
2.使用pip 安裝 itchat庫 (一個利用微信網頁版api介面寫的,有興趣的話可以鼓搗鼓搗)
3.將指令碼檔案上傳到雲伺服器
4.執行程式碼
1)掃碼登入
2)登入成功
效果
傳送訊息
收到訊息
開始撤回
撤回通知
結束
可能好多人會說,我沒有伺服器,該怎麼辦!其實我放在伺服器上是為了二十四小時都在執行,這樣可以一直都在防撤回狀態了。如果你也想這麼做的話就參考下面這篇文章
可以“薅”好幾臺伺服器。不過也可以先在自己電腦上試一試。
相關推薦
Python 實現微信防撤回功能
在微信上突然看到“XXX撤回一條訊息”的時候,心裡癢不癢?現在就教你如何實現訊息防撤回的功能。不限手機型號、手機無需root,只要微訊號可以登上網頁版就可以了(部分不常用和新申請的賬號會登不上網頁版)。 環境 Centos7 python2.
python使用wxpy輕鬆實現微信防撤回
最近比較閒就隨便瞎看,看到了微信防撤回就順便跟著學著實現一下 使用的是wxpy,安裝方法pip install wxpy(我使用的是python2.7),這樣實現起來比較快,反正也只是練手 首現看了兩個別人實現的程式碼,然後看了看wxpy的文件:http://wxpy.r
Python3.5+PyQt5多執行緒+itchat實現微信防撤回桌面版程式碼(二)
weChatThread執行緒類 之前一直不會python多執行緒,寫這個程式的時候,發現不用多執行緒會陷入無限未響應狀態。於是學了半天python多執行緒,但是在主函式裡寫的時候,發現一個問題,Ui主執行緒和工作執行緒沒有分離,使用itchat等庫的時候會堵
Python3.5+PyQt5多執行緒+itchat實現微信防撤回桌面版(一)
前幾日在某乎看到有大神用itchat實現了微信防撤回功能,,覺得很有趣,看到下面評論很多人求桌面版,於是乎,手癢便利用清明節幾天時間做了一個簡陋的桌面程式。廢話不多說,先上圖位敬。 執行環境 win10專業版64位系統1703創造者更新 開發環
Python3.5+PyQt5多執行緒+itchat實現微信防撤回桌面版程式碼
weChatThread執行緒類 之前一直不會python多執行緒,寫這個程式的時候,發現不用多執行緒會陷入無限未響應狀態。於是學了半天python多執行緒,但是在主函式裡寫的時候,發現一個問題,Ui主執行緒和工作執行緒沒有分離,使用itchat等庫的時候會堵塞主執行緒,換句話說PyQt中
Python3.5+PyQt5多線程+itchat實現微信防撤回桌面版代碼
logs rep not cio backup 界面 sel store for weChatThread線程類 之前一直不會python多線程,寫這個程序的時候,發現不用多線程會陷入無限未響應狀態。於是學了半天python多線程,但是在主函數裏寫的時候,發現一個問題,
用Python教你微信防撤回(文字、圖片、語音、視訊、名片等...)
大家在使用微信過程中,有時候訊息還沒看到,就被撤回了。畢竟好奇心大家都有,明知到訊息被撤回了,就更想去看一下是什麼內容心裡想著萬一是女神給我表白了呢.. 今天就用Python來做個微信防撤回的小功能。支援文字、圖片、語音、視訊、名片的防撤回。 思路:利用itchat庫將每次接收到的訊息進行快取。監
Python實現微信小程式支付功能
由於最近自己在做小程式的支付,就在這裡簡單介紹一下講一下用python做小程式支付這個流程。當然在進行開發之前還是建議讀一下具體的流程,清楚支付的過程。 1.支付互動流程 2.獲取openid(微信使用者標識) 1 import requests 2 3 from config import
微信防撤回
con rev hand sts location end pat msg alt 1 # -*-encoding:utf-8-*- 2 import os 3 import re 4 import shutil 5 import time
使用Python實現微信自動化
Python、wxpy、微信Python中可以導入wxpy模塊,在電腦上操作控制微信。 使用Python實現微信自動化
Python實現微信定時發送天氣預報
contents 搜索 day 發送 clas content 網頁 .com time schedule實現定時 1 import requests 2 from requests import exceptions 3 from urllib.reque
Python實現微信自動回復機器人詳細教程
cmd命令 ltr 官網 文本 ply request 多次 reply gist 首先,我們需要安裝並配置好Python環境,並安裝requests和itchat包,我用的是Windows7環境!https://www.python.org/ python官網 下載
使用Python實現微信自動回覆
1. 獲取其一個圖靈機器人的API 可以從下面這個官網上註冊賬號,獲取圖靈機器人的API. http://www.tuling123.com/ 註冊成功後,我們就可以獲得下面這個api的key了. 2.全部Python程式碼如下: 註釋在程式碼內
Python 實現微信聊天機器人
#方法一 # -*- coding=utf-8 -*- import requests import itchat import random import json KEY = '18c2919631d84026912c25970f23286f' def get_response(in
python實現微信訊息群發和微信自動回覆
基於python的第三方庫itchat, 實現微信祝福分組群發和自動回覆好友祝福的功能。 這裡的分組傳送實現原理是這樣的, 首先你想群發訊息給誰, 就把誰拉到一個群聊裡,然後程式碼遍歷群聊裡的所有成員, 挨個私信發祝福。 需要明確的有一下幾點: 1. 
laravel5實現微信第三方登入功能
背景 最近手頭一個專案需要實現使用者在網站的第三方登入(微信和微博),後端框架laravel5.4。 實現過程以微信網頁版第三方登入,其他於此類似,在此不做重複。 準備工作 網站應用微信登入是基於OAuth2.0協議標準構建的微信OAuth2.0授權登入系統。 在進行微信OAuth2
python實現微信付款碼支付(刷卡支付)(純python)
參考連結https://github.com/Jolly23/wx_pay_python https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1 use.py from wx_pay impo
python實現微信轟炸
PYTHON定時發暖心的微信訊息給心愛的她,修改時間可以實現轟炸效果! 假如你朋友叫你準時六點鐘用微信給他發一條訊息叫你提醒他,你該怎麼辦? 一般人,那我就等到六點提醒他! Python程式設計師,我寫好了一個程式,已經要把傳送的內容和時間確定好了,到點自動傳送
利用AccessibilityService實現微信發紅包功能
在AccessibilityService中我們可以做模擬操作,下面記述下通過AccessibilityService實現微信發紅包的功能 1.配置AccessibilityService,需要通過整合AccessibilityService來執行操作。 public c
Python實現微信聊天機器人
# -- coding=utf-8 -- import requests import itchat import random import json KEY = ‘18c2919631d84026912c25970f23286f’ def get_res