python查看微信消息撤回


准備環境

  • python語言環境
  • python解釋器-pycharm

itchat介紹

itchat是一個開源的微信個人號接口,通過itchat可以實現微信(好友或微信群)的信息處理,包括文本、圖片、小視頻、地理位置消息、名片消息、語音消息、動畫表情、普通鏈接、音樂鏈接、群消息、紅包消息、系統消息等,可以對微信的消息進行獲取和回復。

itchat使用

  • itchat庫的安裝
pip install itchat

pycharm解釋器寫入代碼

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import itchat
from itchat.content import *
import time
import re
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']
    # 每條信息的ID
    msg_id = msg['MsgId']
    # 儲存信息的內容
    msg_content = None
    # 儲存分享的連接,比如分享的文章和音樂
    msg_share_url = None

    # 如果發送的消息是文本或者好友推薦
    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))

    # 如果消息是推薦的名片
    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']
        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)
        # 得到消息
        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 = "@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.auto_login(hotReload=True)
itchat.run()

運行之后需要掃描二維碼登錄,登錄后微信手機助手就能監測到對方撤回的消息

下面介紹.py文件如何生成.exe文件,方便做成exe發送給其他人使用
pycharm編輯器Terminal窗口使用

pyinstaller -F .py文件所在絕對路徑

成功之后會在python安裝路徑下的dist文件看到生成的exe文件
PS:python3.6版本下的才能轉換成功


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM