• 微信防撤回


      1 # -*-encoding:utf-8-*-
      2 import os
      3 import re
      4 import shutil
      5 import time
      6 import itchat
      7 from itchat.content import *
      8 
      9 # 说明:可以撤回的有文本文字、语音、视频、图片、位置、名片、分享、附件
     10 
     11 # {msg_id:(msg_from,msg_to,msg_time,msg_time_rec,msg_type,msg_content,msg_share_url)}
     12 msg_dict = {}
     13 
     14 # 文件存储临时目录
     15 rev_tmp_dir = "D:PycharmProjectsCRM_System临时文件"
     16 if not os.path.exists(rev_tmp_dir): os.mkdir(rev_tmp_dir)
     17 
     18 # 表情有一个问题 | 接受信息和接受note的msg_id不一致 巧合解决方案
     19 face_bug = None
     20 
     21 
     22 # 将接收到的消息存放在字典中,当接收到新消息时对字典中超时的消息进行清理 | 不接受不具有撤回功能的信息
     23 # [TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS, NOTE]
     24 @itchat.msg_register([TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO])
     25 def handler_receive_msg(msg):
     26     global face_bug
     27     # 获取的是本地时间戳并格式化本地时间戳 e: 2017-04-21 21:30:08
     28     msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
     29     # 消息ID
     30     msg_id = msg['MsgId']
     31     # 消息时间
     32     msg_time = msg['CreateTime']
     33     # 消息发送人昵称 | 这里也可以使用RemarkName备注 但是自己或者没有备注的人为None
     34     msg_from = (itchat.search_friends(userName=msg['FromUserName']))["NickName"]
     35     # 消息内容
     36     msg_content = None
     37     # 分享的链接
     38     msg_share_url = None
     39     if msg['Type'] == 'Text' 
     40             or msg['Type'] == 'Friends':
     41         msg_content = msg['Text']
     42     elif msg['Type'] == 'Recording' 
     43             or msg['Type'] == 'Attachment' 
     44             or msg['Type'] == 'Video' 
     45             or msg['Type'] == 'Picture':
     46         msg_content = r"" + msg['FileName']
     47         # 保存文件
     48         msg['Text'](rev_tmp_dir + msg['FileName'])
     49     elif msg['Type'] == 'Card':
     50         msg_content = msg['RecommendInfo']['NickName'] + r" 的名片"
     51     elif msg['Type'] == 'Map':
     52         x, y, location = re.search(
     53             "<location x="(.*?)" y="(.*?)".*label="(.*?)".*", msg['OriContent']).group(1, 2, 3)
     54         if location is None:
     55             msg_content = r"纬度->" + x.__str__() + " 经度->" + y.__str__()
     56         else:
     57             msg_content = r"" + location
     58     elif msg['Type'] == 'Sharing':
     59         msg_content = msg['Text']
     60         msg_share_url = msg['Url']
     61     face_bug = msg_content
     62     # 更新字典
     63     msg_dict.update(
     64         {
     65             msg_id: {
     66                 "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec,
     67                 "msg_type": msg["Type"],
     68                 "msg_content": msg_content, "msg_share_url": msg_share_url
     69             }
     70         }
     71     )
     72 
     73 # 收到note通知类消息,判断是不是撤回并进行相应操作
     74 @itchat.msg_register([NOTE])
     75 def send_msg_helper(msg):
     76     global face_bug
     77     if re.search(r"<![CDATA[.*撤回了一条消息]]>", msg['Content']) is not None:
     78         # 获取消息的id
     79         old_msg_id = re.search("<msgid>(.*?)</msgid>", msg['Content']).group(1)
     80         old_msg = msg_dict.get(old_msg_id, {})
     81         if len(old_msg_id) < 11:
     82             itchat.send_file(rev_tmp_dir + face_bug, toUserName='filehelper')
     83             os.remove(rev_tmp_dir + face_bug)
     84         else:
     85             msg_body = "告诉你一个秘密~" + "
    " 
     86                        + old_msg.get('msg_from') + " 撤回了 " + old_msg.get("msg_type") + " 消息" + "
    " 
     87                        + old_msg.get('msg_time_rec') + "
    " 
     88                        + "撤回了什么 ⇣" + "
    " 
     89                        + r"" + old_msg.get('msg_content')
     90             # 如果是分享存在链接
     91             if old_msg['msg_type'] == "Sharing": msg_body += "
    就是这个链接➣ " + old_msg.get('msg_share_url')
     92 
     93             # 将撤回消息发送到文件助手
     94             itchat.send(msg_body, toUserName='filehelper')
     95             # 有文件的话也要将文件发送回去
     96             if old_msg["msg_type"] == "Picture" 
     97                     or old_msg["msg_type"] == "Recording" 
     98                     or old_msg["msg_type"] == "Video" 
     99                     or old_msg["msg_type"] == "Attachment":
    100                 file = '@fil@%s' % (rev_tmp_dir + old_msg['msg_content'])
    101                 itchat.send(msg=file, toUserName='filehelper')
    102                 os.remove(rev_tmp_dir + old_msg['msg_content'])
    103             # 删除字典旧消息
    104             msg_dict.pop(old_msg_id)
    105 
    106 
    107 if __name__ == '__main__':
    108     itchat.auto_login(hotReload=True)
    109     itchat.run()
  • 相关阅读:
    [css3]搜索框focus时变长
    [css3]文字过多以省略号显示
    HTML5表单新增属性
    [JS]getYear()和getFullYear()方法区别
    红包彩带动画效果
    ios下input focus弹出软键盘造成fixed元素位置移位
    旋转效果
    移动端前端开发
    如何加快页面加载速度
    centos7/rhel7下安装redis4.0集群
  • 原文地址:https://www.cnblogs.com/bingpan/p/8722138.html
Copyright © 2020-2023  润新知