python电脑端微信自动化(python使用wxpy实现微信消息防撤回脚本)
类别:脚本大全 浏览量:1353
时间:2021-10-10 00:14:33 python电脑端微信自动化
python使用wxpy实现微信消息防撤回脚本本文实例为大家分享了python实现微信消息防撤回的具体代码,供大家参考,具体内容如下
使用了sqlite3保存数据,当有人撤回消息时取出数据发送到文件传输助手。
文件的话会先保存到本地,语音会以文件的方式发送。
wxpy 和 itchat很久没更新了,有些功能没法用了,web微信也不知道什么时候会凉。
帮助信息在注释里。
|
# -*- coding: utf-8 -*- # 使用sqlite3保存message,当有人撤回消息时在数据库中通过id检索该消息是否存在,如果存在则将撤回的消息发送到文件助手里。 # 目前只支持 text picture map sharing recording video attachment 类型的消息。 import wxpy import sqlite3 import os import re # 准备工作 # 创建attachment目录用于存储 图像、地图/位置、分享、语音、视频、文件 if not os.path.isdir( 'attachment' ): os.mkdir( 'attachment' ) attachment_path = os.path.join(os.getcwd(), 'attachment' ) bot = wxpy.bot() # 用于获取msg id pattern = re. compile (r '\d{19}' ) # 测试wxpy能否正常工作 myself = bot.friends()[ 0 ] myself.send( 'hello?' ) # 创建数据库和message表 try : conn = sqlite3.connect( 'wxpy.db' ) cursor = conn.cursor() # cursor.execute('drop table messages') cursor.execute( """create table if not exists messages (id integer primary key autoincrement, msg_id integer not null, msg_text text, create_time date not null, revoke_time date, attachment_path text, msg_sender text not null, msg_type text not null, msg_url text, msg_raw_data text not null)""" ) # print('establish successfully') finally : conn.commit() cursor.close() conn.close() # 注册所有消息,在程序运行期间将插入所有支持的信息 @bot .register() def store_data(msg): # print(msg.raw) # 如果消息是支持的类型就将数据插入数据库 if msg. type in [wxpy.text, wxpy.recording, wxpy.picture, wxpy.attachment, wxpy.video, wxpy.sharing, wxpy. map ]: insert_data(msg) # 撤回的消息类型是note elif msg. type = = wxpy.note: send_revoke(msg) # 插入数据 def insert_data(msg): try : conn = sqlite3.connect( 'wxpy.db' ) cursor = conn.cursor() if msg. type = = wxpy.text: cursor.execute("insert into messages (msg_id, msg_text, create_time, msg_sender, msg_type, msg_raw_data)\ values (?, ?, ?, ?, ?, ?)", (msg. id , msg.text, msg.create_time, str (msg.sender)[ 9 : - 1 ], msg. type , str (msg.raw))) # 将录音/图像/文件/视频下载到本地,插入保存路径。 elif msg. type in [wxpy.recording, wxpy.picture, wxpy.attachment, wxpy.video]: save_path = os.path.join(attachment_path, msg.file_name) msg.get_file(save_path) cursor.execute('insert into messages (msg_id, create_time, attachment_path, msg_sender, msg_type,\ msg_raw_data) values (?, ?, ?, ?, ?, ?)', (msg. id , msg.create_time, save_path, str (msg.sender)[ 9 : - 1 ], msg. type , str (msg.raw))) # 插入分享/位置链接 elif msg. type in [wxpy.sharing, wxpy. map ]: cursor.execute('insert into messages (msg_id, msg_text, create_time, msg_sender, msg_type, msg_url,\ msg_raw_data) values (?, ?, ?, ?, ?, ?, ?)', (msg. id , msg.text, msg.create_time, str (msg.sender)[ 9 : - 1 ], msg. type , str (msg.url), str (msg.raw))) # print('insert data successfully') finally : conn.commit() cursor.close() conn.close() # 在数据库中检索消息是否存在,如果存在则将被撤回的消息发送到文件传输助手。 def send_revoke(message): msg_id = pattern.search(message.raw[ 'content' ]).group() try : conn = sqlite3.connect( 'wxpy.db' ) cursor = conn.cursor() cursor.execute('insert into messages (msg_id, create_time, msg_sender, msg_type, msg_raw_data)\ values (?, ?, ?, ?, ?)', (message. id , message.create_time, str (message.sender)[ 9 : - 1 ], message. type , str (message.raw))) msg_data = cursor.execute( 'select * from messages where msg_id=?' , (msg_id, )).fetchall() # print('take out data successfully') finally : conn.commit() cursor.close() conn.close() if msg_data[ 0 ][ 7 ] = = 'text' : msg_info = '告诉你一个秘密 {} 在 {} 撤回了文本\n{}' . format (msg_data[ 0 ][ 6 ], msg_data[ 0 ][ 3 ], msg_data[ 0 ][ 2 ]) bot.file_helper.send(msg_info) else : send_revoke_nontext(msg_data) # 非文本信息发送 def send_revoke_nontext(msg_data): if msg_data[ 0 ][ 7 ] = = 'picture' : if msg_data[ 0 ][ 5 ][ - 4 :] = = '.gif' : # 现在wxpy & itchat发不了gif了 bot.file_helper( '很抱歉,暂时不支持表情(gif)的撤回重发。' ) else : msg_info = '告诉你一个秘密 {} 在 {} 撤回了图像' . format (msg_data[ 0 ][ 6 ], msg_data[ 0 ][ 3 ]) bot.file_helper.send(msg_info) bot.file_helper.send_image(msg_data[ 0 ][ 5 ]) elif msg_data[ 0 ][ 7 ] = = 'recording' : msg_info = '告诉你一个秘密 {} 在 {} 撤回了语音' . format (msg_data[ 0 ][ 6 ], msg_data[ 0 ][ 3 ]) bot.file_helper.send(msg_info) bot.file_helper.send_file(msg_data[ 0 ][ 5 ]) elif msg_data[ 0 ][ 7 ] = = 'attachment' : msg_info = '告诉你一个秘密 {} 在 {} 撤回了文件' . format (msg_data[ 0 ][ 6 ], msg_data[ 0 ][ 3 ]) bot.file_helper.send(msg_info) bot.file_helper.send_file(msg_data[ 0 ][ 5 ]) elif msg_data[ 0 ][ 7 ] = = 'video' : msg_info = '告诉你一个秘密 {} 在 {} 撤回了视频' . format (msg_data[ 0 ][ 6 ], msg_data[ 0 ][ 3 ]) bot.file_helper.send(msg_info) bot.file_helper.send_video(msg_data[ 0 ][ 5 ]) elif msg_data[ 0 ][ 7 ] = = 'sharing' : msg_info = '告诉你一个秘密 {} 在 {} 撤回了分享\n{}\n{}' . format (msg_data[ 0 ][ 6 ], msg_data[ 0 ][ 3 ], msg_data[ 0 ][ 2 ],\ msg_data[ 0 ][ 8 ]) bot.file_helper.send(msg_info) elif msg_data[ 0 ][ 7 ] = = 'map' : msg_info = '告诉你一个秘密 {} 在 {} 撤回了位置\n{}\n{}' . format (msg_data[ 0 ][ 6 ], msg_data[ 0 ][ 3 ], msg_data[ 0 ][ 2 ],\ msg_data[ 0 ][ 8 ]) bot.file_helper.send(msg_info) wxpy.embed() |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/babaili_/article/details/85947411
您可能感兴趣
- python中对象方法和顶级方法(Python3.5面向对象程序设计之类的继承和多态详解)
- python在dataframe添加数据(使用Python向DataFrame中指定位置添加一列或多列的方法)
- python如何使用multiprocess(Python multiprocessing多进程原理与应用示例)
- python人脸识别库(20行python代码实现人脸识别)
- python如何判断是否为回文数(对python判断是否回文数的实例详解)
- 怎么用python做随机矩阵(python实现杨氏矩阵查找)
- python3列表的使用教程(对Python3 pyc 文件的使用详解)
- python对列表排序(Python实现对特定列表进行从小到大排序操作示例)
- python语言案例教程单元测试(Python单元和文档测试实例详解)
- python复杂的验证码处理(Python 通过打码平台实现验证码的实现)
- python3.5 tkinter教程(解决python3.5 正常安装 却不能直接使用Tkinter包的问题)
- 如何使用python定时运行(Python实现定时执行任务的三种方式简单示例)
- ubuntu python3.7安装(详解Ubuntu16.04安装Python3.7及其pip3并切换为默认版本)
- python电脑自动发送邮件(python学习--使用QQ邮箱发送邮件代码实例)
- nginxpython编写模块(Python开发之Nginx+uWSGI+virtualenv多项目部署教程)
- python里字典常用方法(Python使用字典的嵌套功能详解)
- 七年前的这部剧有毒,全剧只有女主红到发紫,男主至今无人认识(七年前的这部剧有毒)
- 宋轶除了演过于曼丽,原来还演过一个青楼女子(宋轶除了演过于曼丽)
- 赵丽颖第一部当女主的戏,主角配角个个都是实力演员(赵丽颖第一部当女主的戏)
- 乾隆为何这么喜爱白塔原因是什么(乾隆为何这么喜爱白塔原因是什么)
- 逐渐消失的东北八大怪现象,进步的社会里我们遗失的是什么(逐渐消失的东北八大怪现象)
- 今日大雪,大雪养生这样做,一年都有好气色(大雪养生这样做)
热门推荐
- http服务器使用教程(HTTP与HTTP协作的Web服务器访问流程图解)
- python获取网关ip地址(python实现扫描局域网指定网段ip的方法)
- wamp环境下源码如何安装(从wamp到xampp的升级之路)
- sql 多列求和 当某列有null值时的处理
- divcss页面布局步骤(横向两列布局左列固定,右列自适应的4种CSS实现方式)
- css浮动小例子教程(使用css transition属性实现一个带动画显隐的微信小程序部件)
- web压力测试工具app(十款Web服务器性能压力测试工具)
- yii2和laravel框架哪个比较简单(关于Yii中模型场景的一些简单介绍)
- nginx做转发配置weight啥意思(Nginx中break与last的区别详析)
- ASP.NET将WORD、PDF、PPT转为图片
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9