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所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照下面的知识点去找对应的学习资源,保证自己学得较为全面。

img
img

二、Python必备开发工具

工具都帮大家整理好了,安装就可直接上手!img

三、最新Python学习笔记

当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。

img

四、Python视频合集

观看全面零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

img

五、实战案例

纸上得来终觉浅,要学会跟着视频一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

img

六、面试宝典

在这里插入图片描述

在这里插入图片描述

简历模板在这里插入图片描述

若有侵权,请联系删除

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值