
目前有一个项目 WechatPCAPI 可以对微信进行操作,简单来说它是直接操作 PC 版微信客户端的,当然它有一定不足之处就是:PC 版微信客户端和 Python 都需要使用指定版本的,本文我们使用的 Python 版本为 3.7.6 ,微信客户端使用版本为 2.6.8.52 ,WechatPCAPI 的 GitHub 地址为:
消息防撤回
我们在使用微信和好友聊天时,对方有时会有撤回消息的情况,正常情况下,我们是不知道好友撤回的消息是什么的,通过 WechatPCAPI 就可以实现消息防撤回的功能。
我们知道通常撤回的消息是点击撤回操作前一步发送的内容,当然也可能撤回的是前两步、三步 … 的消息,这里我们只对撤回前一步的消息做处理,基本思路是:我们将撤回前一步发送的消息存一下,当对方点击撤回操作时,我们再将前一步的消息再次返回给自己。
下面看一下实现代码:
logging.basicConfig(level=logging.INFO)
queue_recved_event = Queue()
def on_message(msg):
queue_recved_event.put(msg)
def login():
pre_msg = ''
# 初始化微信实例
wx_inst = WechatPCAPI(on_message=on_message, log=logging)
# 启动微信
wx_inst.start_wechat(block=True)
# 等待登陆成功,此时需要人为扫码登录微信
while not wx_inst.get_myself():
time.sleep(5)
print('登陆成功')
while True:
msg = queue_recved_event.get()
data = msg.get('data')
sendinfo = data.get('sendinfo')
data_type = str(data.get('data_type'))
msgcontent = str(data.get('msgcontent'))
is_recv = data.get('is_recv')
print(msg)
if data_type == '1' and 'revokemsg' not in msgcontent:
pre_msg = msgcontent
if sendinfo is not None and 'revokemsg' in msgcontent:
user = str(sendinfo.get('wx_id_search'))
recall = '撤回的消息:' + pre_msg
wx_inst.send_text(to_user=user, msg=recall)
看一下操作

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试



