本文实例为大家分享了python实现微信防撤回神器的具体代码,供大家参考,具体内容如下

手写辛苦,希望给赞

#!/usr/local/bin/python3

# coding=utf-8

import os

import re

import time

import _thread

import itchat

from itchat.content import *

# 可以撤回的消息格式:文本、语音、视频、图片、位置、名片、分享、附件

# 存储收到的消息

# 格式:{msg_id:{msg_from,msg_to,msg_time,msg_time_rec,msg_tye,msg_content,msg_share_url}}

msg_dict = {}

# 存储消息中文件的临时目录,程序启动时,先清空

rev_tmp_dir = "/Users/chenlong/d1/wechat/rev/"

if not os.path.exists(rev_tmp_dir):

os.mkdir(rev_tmp_dir)

else:

for f in os.listdir(rev_tmp_dir):

path = os.path.join(rev_tmp_dir, f)

if os.path.isfile(path):

os.remove(path)

# 表情有一个问题:消息和撤回提示的msg_id不一致

face_bug = None

# 监听微信消息(只限可撤回的消息类型),存储到本地,并清除超时的消息

# 可撤回的消息类型:TEXT、PICTURE、MAP、CARD、SHARING、RECORDING、ATTACHMENT、VIDEO、FRIENDS、NOTE

@itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS, NOTE],

isFriendChat=True, isGroupChat=True, isMpChat=True)

def handler_reveive_msg(msg):

global face_bug

msg_time_rev = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

msg_id = msg['MsgId']

msg_time = msg['CreateTime']

msg_share_url = None

group_name = None

# 获取发送人

if 'ActualNickName' in msg:

sender_info = set_sender_group_chat(msg)

msg_from = sender_info['msg_from']

group_name = sender_info['group_name']

else:

msg_from = (itchat.search_friends(userName=msg['FromUserName']))['RemarkName'] # 优先使用备注

if msg_from is None:

msg_from = msg['FromUserName']

# 获取消息内容

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("

2,

3)

if location is None:

msg_content = r"维度->" + x + " 经度->" + y

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_rev": msg_time_rev,

"msg_type": msg['Type'],

"msg_content": msg_content,

"msg_share_url": msg_share_url,

"group_name": group_name

}

})

# 遍历本地消息字典,清除2分钟之前的消息,并删除缓存的消息对应的文件

def clear_timeout_msg():

need_del_msg_ids = []

for m in msg_dict:

msg_time = msg_dict[m]['msg_time']

if int(time.time()) - msg_time > 120:

need_del_msg_ids.append(m)

if len(need_del_msg_ids) > 0:

for i in need_del_msg_ids:

old_msg = msg_dict.get(i)

if old_msg['msg_type'] == PICTURE or old_msg['msg_type'] == RECORDING or old_msg['msg_type'] == VIDEO \

or old_msg['msg_type'] == ATTACHMENT:

os.remove(rev_tmp_dir + old_msg['msg_content'])

msg_dict.pop(i)

# 设置发送人,当消息是群消息的时候

def set_sender_group_chat(msg):

msg_from = msg['ActualNickName']

# 查找用户备注名称

friends = itchat.get_friends(update=True)

from_user = msg['ActualUserName']

for f in friends:

if from_user == f['UserName']:

msg_from = f['RemarkName'] or f['NickName']

break

groups = itchat.get_chatrooms(update=True)

for g in groups:

if msg['FromUserName'] == g['UserName']:

group_name = g['NickName']

break

return {'msg_from': msg_from, 'group_name': group_name}

# 监听通知,判断是撤回通知,则将消息发给文件助手

@itchat.msg_register([NOTE], isFriendChat=True, isGroupChat=True, isMpChat=True)

def send_msg_helper(msg):

global face_bug

if re.search(r"\", msg['Content']) is not None:

old_msg_id = re.search("\(.*?)\", 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') \

+ "消息\n" \

+ old_msg.get('msg_time_rev') + "\n" \

+ old_msg.get('msg_content')

if old_msg.get('group_name') is not None:

msg_body = old_msg.get('group_name') + ">" + msg_body

if old_msg['msg_type'] == "Sharing":

msg_body += "\n" + 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()

# 子线程清除超时消息

_thread.start_new_thread(clear_timeout_msg)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

微信防撤回python代码_python实现微信防撤回神器相关推荐

  1. 微信防撤回python代码_Python实现微信防撤回

    微信(WeChat)是腾讯公司于2011年1月21日推出的一款社交软件,8年时间微信做到日活10亿,日消息量450亿.在此期间微信也推出了不少的功能如:"摇一摇"."漂流 ...

  2. 微信防撤回python代码_python实现微信消息防撤回

    微信(WeChat)是腾讯公司于2011年1月21日推出的一款社交软件,8年时间微信做到日活10亿,日消息量450亿.在此期间微信也推出了不少的功能如:"摇一摇"."漂 ...

  3. python塔防小游戏代码_Python制作塔防小游戏

    开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块. ​ 原理介绍 游戏规则简介: 玩家通过建造箭塔抵御敌人的进攻. 每隔一段时间,将会有一波敌人从 ...

  4. python实现微信hook_GitHub - gemgin/wechathook: 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取...

    wechathook 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取 注入器 注入dll进程序中 DLL 实现hook功能,申请内存,修改call,在里 ...

  5. python实现微信hook_GitHub - redtips/wechathook: 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取...

    wechathook 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取 注入器 注入dll进程序中 DLL 实现hook功能,申请内存,修改call,在里 ...

  6. python实现微信hook_GitHub - zhouxionger/wechathook: 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取...

    wechathook 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取 注入器 注入dll进程序中 DLL 实现hook功能,申请内存,修改call,在里 ...

  7. python hook微信_GitHub - 15993248973/wechathook: 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取...

    wechathook 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取 注入器 注入dll进程序中 DLL 实现hook功能,申请内存,修改call,在里 ...

  8. hook微信 python_GitHub - zkqiang/wechathook: 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取...

    wechathook 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取 注入器 注入dll进程序中 DLL 实现hook功能,申请内存,修改call,在里 ...

  9. 50行Python代码玩转微信小游戏颜色王者

    50行Python代码玩转微信小游戏"颜色王者" 游戏模式 在微信小程序里搜索"颜色王者",即可找到该游戏. 游戏的目标比拼色彩敏感度.点击图片中不一样的色块即 ...

最新文章

  1. TCP/IP详解学习笔记(8)-DNS域名系统
  2. rtmp协议 java_基于rtmp协议的java多线程服务器
  3. c语言求数组中绝对值最小值,(C语言)简单的绝对值排序
  4. 使用Node.JS,如何将JSON文件读入(服务器)内存?
  5. 用GPU进行TensorFlow计算加速
  6. 音视频开发著作《Android音视频开发》终于发售了,先来一波签名送书福利!
  7. 油猴安装错误问题(下载中断问题)及脚本安装
  8. seaborn无法下载数据的问题
  9. 已经被研发出来的AI芯片有哪些?
  10. HTML中美化页面,10 使用CSS美化页面
  11. 计算机论文中期考核报告,(硕士学位论文中期考核报告范文.doc
  12. mysql 分区表 归档_详解 MySQL 数据库冷数据归档
  13. 计算机图形学基础第七章ppt,计算机图形学 -第七章讲义ppt课件
  14. 【博学谷学习记录】超强总结,用心分享|js语法基础(一)
  15. AutoDL论文解读(五):可微分方法的NAS
  16. 【shell案例】CentOS7安装MySQL脚本案例
  17. 无线网技术——复习(2)
  18. 图片转素描的工具汇总
  19. 安装并配置TrueNas存储平台
  20. confirm确认对话框 弹出确定 提交 取消

热门文章

  1. 【shell】echo不显示变量中的多个空格
  2. windows 安装jenkins
  3. selenium3 + python - js处理readonly属性
  4. Linux关键字查询
  5. (三)svn 服务器端之创建仓库
  6. 数据结构 5排序算法
  7. [转载] python中pow可以计算负数吗_Python数学.pow()计算失误
  8. [转载] 包含对象的json格式_如何把JSON数据格式转换为Python的类对象?
  9. [转载] python set 集合详解
  10. [转载] 10 个最值得 Python 新人练手的有趣项目