python基于itchat模块实现微信防撤回


Posted in Python onApril 29, 2019

有时候,女神发来一条消息,说约你看电影,她考虑了一下,又撤回了,不约你了…而你又想知道她究竟发了什么,该怎么办?微信防撤回了解一下。

环境要求

Python3
电脑

安装itchat

pip install itchat

使用代码

新建chehui.py,拷贝以下代码

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

__author__ = 'jiangwenwen'

import itchat
from itchat.content import *
import time
import re
import os

print("该程序由里客云资源站开发,网址:likeyunba.com")
print("作者:TANKING")
print("打开程序会弹出一个二维码,微信扫码")
print("如果二维码弹不出,那就在你这个程序的同一个目录下找到QR.png双击打开扫码")
print("扫码后,出现Start auto replying就可以实时监控消息了...")

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

CMD运行即可。

考虑到有一些人没有Python环境,我已经打包成可执行文件了,直接双击exe就可以在电脑运行。

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

Python 相关文章推荐
Python中for循环详解
Jan 17 Python
Python编程中用close()方法关闭文件的教程
May 24 Python
利用python实现简单的循环购物车功能示例代码
Jul 05 Python
对Python中range()函数和list的比较
Apr 19 Python
python-pyinstaller、打包后获取路径的实例
Jun 10 Python
python如何实现代码检查
Jun 28 Python
Django xadmin开启搜索功能的实现
Nov 15 Python
Python数据持久化存储实现方法分析
Dec 21 Python
浅析Python 简单工厂模式和工厂方法模式的优缺点
Jul 13 Python
使用sublime text3搭建Python编辑环境的实现
Jan 12 Python
python爬取企查查企业信息之selenium自动模拟登录企查查
Apr 08 Python
Python机器学习实战之k-近邻算法的实现
Nov 27 Python
手把手教你使用Python创建微信机器人
Apr 29 #Python
python实现微信防撤回神器
Apr 29 #Python
python实现文件助手中查看微信撤回消息
Apr 29 #Python
Python实现微信消息防撤回功能的实例代码
Apr 29 #Python
python控制nao机器人身体动作实例详解
Apr 29 #Python
python实现nao机器人身体躯干和腿部动作操作
Apr 29 #Python
解决Python找不到ssl模块问题 No module named _ssl的方法
Apr 29 #Python
You might like
php面向对象 字段的声明与使用
2012/06/14 PHP
邮箱正则表达式实现代码(针对php)
2013/06/21 PHP
php遍历文件夹和文件列表示例分享
2014/03/11 PHP
2款PHP无限级分类实例代码
2015/11/11 PHP
PHP 中魔术常量的实例详解
2017/10/26 PHP
PHP二维数组实现去除重复项的方法【保留各个键值】
2017/12/21 PHP
jquery 表单取值常用代码
2009/12/22 Javascript
使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
2010/05/07 Javascript
javascript中日期转换成时间戳的小例子
2013/03/21 Javascript
前端开发过程中浏览器版本的两种判定方法
2013/10/30 Javascript
JavaScript学习笔记之内置对象
2015/01/22 Javascript
JavaScript中通过prototype属性共享属性和方法的技巧实例
2015/03/13 Javascript
AngularJS模块管理问题的非常规处理方法
2015/04/29 Javascript
jQuery使用经验小技巧(推荐)
2016/05/31 Javascript
浅谈Jquery中Ajax异步请求中的async参数的作用
2016/06/06 Javascript
浅谈JavaScript 覆盖原型以及更改原型
2016/08/31 Javascript
Javascript 跨域知识详细介绍
2016/10/30 Javascript
js实现自动图片轮播代码
2017/03/22 Javascript
JS检测是否可以访问公网服务器功能代码
2017/06/19 Javascript
vue2中引用及使用 better-scroll的方法详解
2018/11/15 Javascript
jQuery实现的简单歌词滚动功能示例
2019/01/07 jQuery
详解element-ui设置下拉选择切换必填和非必填
2019/06/17 Javascript
详解Vue3 Teleport 的实践及原理
2020/12/02 Vue.js
python UNIX_TIMESTAMP时间处理方法分析
2016/04/18 Python
关于django 数据库迁移(migrate)应该知道的一些事
2018/05/27 Python
Python零基础入门学习之输入与输出
2019/04/03 Python
python模拟菜刀反弹shell绕过限制【推荐】
2019/06/25 Python
对Python中一维向量和一维向量转置相乘的方法详解
2019/08/26 Python
Python爬虫库requests获取响应内容、响应状态码、响应头
2020/01/25 Python
浅析Python面向对象编程
2020/07/10 Python
俄罗斯建筑和装饰材料在线商店:Stroilandia
2020/07/25 全球购物
linux面试题参考答案(9)
2016/01/29 面试题
药学职务聘任书
2014/03/29 职场文书
美食节目策划方案
2014/05/31 职场文书
三好学生竞选稿范文
2019/08/21 职场文书
NoSQL优缺点与MongoDB数据库简介
2022/06/05 MongoDB