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 random函数在哪个包(Python3.5内置模块之random模块用法实例分析)
- python 后台django(Python Django给admin添加Action的方法实例详解)
- python获取游戏画面信息(python游戏开发之视频转彩色字符动画)
- python3html怎么转换成pdf(Python实现html转换为pdf报告生成pdf报告功能示例)
- python如何读取文件(Python从文件中读取数据的方法讲解)
- python3d旋转特效(python实现小球弹跳效果)
- python微信窗口内容获取(python使用wxpy轻松实现微信防撤回的方法)
- pythonyield使用场景(Yii框架核心组件类实例详解)
- python闭包的讲解(详解python函数的闭包问题内部函数与外部函数详述)
- python中字典的值怎么应用(对python中字典keys,values,items的使用详解)
- 怎么查看python的安装路径(查看python安装路径及pip安装的包列表及路径)
- pythonpandas提取列索引(Python3.5 Pandas模块缺失值处理和层次索引实例详解)
- 怎么用python分析足球(使用Python进行体育竞技分析预测球队成绩)
- python基础教学之125 装饰器简介(python3 property装饰器实现原理与用法示例)
- python列表精讲33节(Python列表知识应知应会)
- python包和模块管理(python的依赖管理的实现)
- 太鸡贼了,这老小区轻松搞定了停车问题(这老小区轻松搞定了停车问题)
- 节日我在岗|警景相融 平安相伴(节日我在岗警景相融)
- 战 疫 时刻 致敬每一位石化大学的 守护者(战疫时刻)
- 老弄堂里的市井味道(老弄堂里的市井味道)
- 小米音乐可绑定QQ音乐, QQ音乐 真的会消失在小米的设备上吗(小米音乐可绑定QQ音乐)
- 小米Watch S1评测 或许能成为小米冲击高端可穿戴设备的里程碑(小米WatchS1评测或许能成为小米冲击高端可穿戴设备的里程碑)
热门推荐
- css 宽度继承(CSS样式中大于号的使用及Css中处理继承方法)
- 搭建php和mysql的运行环境(Windows环境开发PHP完整配置教程Apache+Mysql+PHP)
- CSS中的伪类与伪元素
- php生成随机数讲解(PHP生成随机字符串实例代码字母+数字)
- python程序开发过程(python调用外部程序的实操步骤)
- elasticsearch docker(在Docker中安装Elasticsearch7.6.2的教程)
- windows10怎么设置iis框架(Windows10系统如何安装IIS服务器)
- tensorflow做识别分类(使用TensorFlow实现二分类的方法示例)
- sublime text 安装package control,方便其它插件安装
- mysql 8.0.22 winx64安装配置图文教程(mysql 8.0.22 winx64安装配置图文教程)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9