查看微信撤回消息

Posted on Posted in python

微信查看撤回的消息, 发送到文件助手

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

import os
import re
import time
import itchat
from itchat.content import TEXT, PICTURE, \
    MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, NOTE

# 可以撤回的类型: 文本/语音/视频/图片/位置/名片/分享/附件

msg_dict = {}

# 文件临时存储目录
rev_tmp_dir = "./tmp/"
if not os.path.exists(rev_tmp_dir):
    os.mkdir(rev_tmp_dir)

# 解决表情的问题
face_bug = None


# 将接受的消息存储到字典中, 接受到新消息时对字典中超时的消息进行清理
# 不接受不具有撤回功能的信息
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING,
                      ATTACHMENT, VIDEO])
def handler_receive_msg(msg):
    global face_bug
    # 获取本地时间
    msg_time_rec = time.strftime('%Y-%m-%d %X', time.localtime(time.time()))
    # 消息 ID
    msg_id = msg['MsgId']
    # 消息时间
    msg_time = msg['CreateTime']
    # 发件人昵称
    msg_from = (itchat.search_friends(
        userName=msg['FromUserName']))["NickName"]
    # 消息内容
    msg_content = None
    # 分享的链接
    msg_share_url = None
    if msg['Type'] == 'Text' or msg['Type'] == 'Friends':
        msg_content = msg['Text']
    elif msg['Type'] == 'Recording' or msg['Type'] == 'Attachment' \
            or msg['Type'] == 'Video' or msg['Type'] == 'Picture':
        msg_content = r""+msg['FileName']
        # 保存文件
        msg['Text'](rev_tmp_dir + msg['FileName'])
    elif msg['Type'] == 'Card':
        msg_content == msg['RecommendInfo']['NickName'] + r" 的名片"
    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']
    face_bug = msg_content
    # 更新字典
    msg_dict.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
            }
        }
    )


@itchat.msg_register([NOTE])
def send_msg_helper(msg):
    global face_bug
    if re.search(r"撤回了一条消息]]>", msg['Content']) is not None:
        # 获取消息 id
        old_msg_id = re.search("<msgid>(.*?)</msgid>", msg['Content']).group(1)
        old_msg = msg_dict.get(old_msg_id, {})
        if len(old_msg_id) < 11:
            itchat.send_file(rev_tmp_dir + face_bug, toUserName='filehelper')
            os.remove(rev_tmp_dir + face_bug)
        else:
            msg_body = "告诉你一个秘密" + " " \
                + old_msg.get('msg_from') + "撤回了" + old_msg.get("msg_type")\
                + "消息" + " " + old_msg.get('msg_time_rec') + " "\
                + "撤回了什么→" + " " + r"" + old_msg.get('msg_content')
            # 处理分享链接
            if old_msg['msg_type'] == "Sharing":
                msg_body += "就是这个链接→" + old_msg.get("msg_share_url")
            # 发送到文件助手
            itchat.send(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' % (rev_tmp_dir + old_msg['msg_content'])
                itchat.send(msg=file, toUserName='filehelper')
                os.remove(rev_tmp_dir + old_msg['msg_content'])
            # 删除字典旧消息
            msg_dict.pop(old_msg_id)


if __name__ == '__main__':
    itchat.auto_login(hotReload=True, enableCmdQR=2)
    itchat.run()
» 转载请注明来源:呢喃 » 查看微信撤回消息

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × 3 =