page contents

3步实现用Python防止微信消息撤回功能

本文讲述了3步实现用python防止微信消息撤回功能,具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2023-10-GIj8DtXi65409b6086fd3.jpg本文讲述了3步实现用python防止微信消息撤回功能,具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

我们在使用微信时,如果发错消息,可以撤回,但你有没有想过在某些特殊情况下,防止对方撤回消息呢?今天我们就使用Python实现一下查看好友撤回的微信消息的功能。运行程序,手机端微信扫描弹出的二维码即可登录网页版微信,程序会将网页版微信收到的所有消息都缓存下来,当检测到有消息(语音、文字、图片等)撤回时,将撤回消息的缓存版本通过文件传输助手发送到自己的手机上,如下图所示。

实现微信消息防撤回功能主要通过Python内置的os模块、re模块、time模块、platform模块,以及第三方模块itchat实现。具体步骤如下:

(1)由于使用了第三方模块itchat,所以需要先安装该模块。使用pip命令安装模块的命令如下:

pip install itchat

(2)导入程序中需要使用的模块,具体代码如下:

import re

import os

import time

import itchat

import platform

from itchat.content import *

(3)实现逻辑。首先使用platform模块获取操作系统底层的数据,根据不同的操作系统传入不同的登录参数,登陆成功后,缓存接收到的所有信息并监听是否有消息撤回。代码如下:

import re

import os

import time

import itchat

import platform

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 handle_rsg(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'] == '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' \

                        '内容:' + 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()

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2023-10-31 14:15
  • 阅读 ( 216 )
  • 分类:Python开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1316 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章