Python查看微信好友撤回的消息

公衆號:Charles的皮卡丘
做者:Charles緩存

開發工具:
Python版本:3.6.4
相關模塊:
itchat模塊;
以及一些Python自帶的模塊。微信

環境搭建:
安裝Python並添加到環境變量,pip安裝須要的相關模塊便可。ide

原理簡介:
思路比較簡單,利用itchat模塊登陸網頁版微信,將本身微信收到的全部消息都緩存下來,當檢測到有消息撤回時,將撤回消息的緩存版本經過文件傳輸助手發送到本身的手機上。
所以,你必須保證腳本24小時運行才能夠一直監視別人有沒有撤回消息。
具體實現過程詳見源代碼。工具

視頻演示:
https://mp.weixin.qq.com/s/Ch...開發工具

源代碼:spa

# Python查看微信撤回消息
# 公衆號: Charles的皮卡丘
# 做者: Charles
import re
import os
import time
import itchat
import platform
from itchat.content import TEXT
from itchat.content import *


msg_info = {}
face_package = None


# 處理接收到的信息
@itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isFriendChat=True, isMpChat=True)
def handleRMsg(msg):
    global face_package
    # 接收消息的時間
    msg_time_receive = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    # 發信人
    try:
        msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName']
    except:
        msg_from = 'WeChat Official Accounts'
    # 發信時間
    msg_time_send = msg['CreateTime']
    # 信息ID
    msg_id = msg['MsgId']
    msg_content = None
    msg_link = None
    # 文本或者好友推薦
    if msg['Type'] == 'Text' or msg['Type'] == 'Friends':
        msg_content = msg['Text']
        print('[Text/Friends]: %s' % 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('[Attachment/Video/Picture/Recording]: %s' % msg_content)
    # 推薦名片
    elif msg['Type'] == 'Card':
        msg_content = msg['RecommendInfo']['NickName'] + '的推薦名片,'
        if msg['RecommendInfo']['Sex'] == 1:
            msg_content += '性別男。'
        else:
            msg_content += '性別女。'
        print('[Card]: %s' % 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
        print('[Map]: %s' % msg_content)
    # 分析的音樂/文章
    elif msg['Type'] == 'Sharing':
        msg_content = msg['Text']
        msg_link = msg['Url']
        print('[Sharing]: %s' % msg_content)
    msg_info.update(
            {
                msg_id: {
                    "msg_from": msg_from,
                    "msg_time_send": msg_time_send,
                    "msg_time_receive": msg_time_receive,
                    "msg_type": msg["Type"],
                    "msg_content": msg_content,
                    "msg_link": msg_link
                }
            }
        )
    face_package = msg_content


# 監聽是否有消息撤回
@itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True)
def monitor(msg):
    if '撤回了一條消息' in msg['Content']:
        recall_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1)
        recall_msg = msg_info.get(recall_msg_id)
        print('[Recall]: %s' % recall_msg)
        # 表情包
        if len(recall_msg_id) < 11:
            itchat.send_file(face_package, toUserName='filehelper')
        else:
            msg_prime = '---' + recall_msg.get('msg_from') + '撤回了一條消息---\n' \
                        '消息類型:' + recall_msg.get('msg_type') + '\n' \
                        '時間:' + recall_msg.get('msg_time_receive') + '\n' \
                        r'內容:' + recall_msg.get('msg_content')
            if recall_msg['msg_type'] == 'Sharing':
                msg_prime += '\n連接:' + recall_msg.get('msg_link')
            itchat.send_msg(msg_prime, toUserName='filehelper')
            if recall_msg['msg_type'] == 'Attachment' or recall_msg['msg_type'] == "Video" or recall_msg['msg_type'] == 'Picture' or recall_msg['msg_type'] == 'Recording':
                file = '@fil@%s' % (recall_msg['msg_content'])
                itchat.send(msg=file, toUserName='filehelper')
                os.remove(recall_msg['msg_content'])
            msg_info.pop(recall_msg_id)


if __name__ == '__main__':
    if platform.platform()[:7] == 'Windows':
        itchat.auto_login(enableCmdQR=False, hotReload=True)
    else:
        itchat.auto_login(enableCmdQR=True, hotReload=True)
    itchat.run()

歡迎關注個人我的公衆號:Charles的皮卡丘
圖片描述code

相關文章
相關標籤/搜索