zoukankan      html  css  js  c++  java
  • 微信防撤回python3

    微信防撤回
    #reload(sys) #sys.setdefaultencoding('utf8')

    python3应该改为下面内容 import importlib importlib.reload(sys) import os

    防撤回,发送到原处

      1 # coding:utf-8
      2 import itchat
      3 from itchat.content import TEXT
      4 from itchat.content import *
      5 import sys
      6 import time
      7 import re
      8 
      9 #reload(sys)
     10 #sys.setdefaultencoding('utf8')
     11 import importlib
     12 importlib.reload(sys)
     13 import os
     14 
     15 msg_information = {}
     16 face_bug = None  # 针对表情包的内容
     17 
     18 
     19 @itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isFriendChat=True,
     20                      isMpChat=True)
     21 def handle_receive_msg(msg):
     22     global face_bug
     23     msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  # 接受消息的时间
     24     msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName']  # 在好友列表中查询发送信息的好友昵称
     25     msg_time = msg['CreateTime']  # 信息发送的时间
     26     msg_id = msg['MsgId']  # 每条信息的id
     27     msg_content = None  # 储存信息的内容
     28     msg_share_url = None  # 储存分享的链接,比如分享的文章和音乐
     29     print(msg['Type'])
     30     print(msg['MsgId'])
     31     if msg['Type'] == 'Text' or msg['Type'] == 'Friends':  # 如果发送的消息是文本或者好友推荐
     32         msg_content = msg['Text']
     33         print(msg_content)
     34 
     35     # 如果发送的消息是附件、视屏、图片、语音
     36     elif msg['Type'] == "Attachment" or msg['Type'] == "Video" 
     37             or msg['Type'] == 'Picture' 
     38             or msg['Type'] == 'Recording':
     39         msg_content = msg['FileName']  # 内容就是他们的文件名
     40         msg['Text'](str(msg_content))  # 下载文件
     41         # print msg_content
     42     elif msg['Type'] == 'Card':  # 如果消息是推荐的名片
     43         msg_content = msg['RecommendInfo']['NickName'] + '的名片'  # 内容就是推荐人的昵称和性别
     44         if msg['RecommendInfo']['Sex'] == 1:
     45             msg_content += '性别为男'
     46         else:
     47             msg_content += '性别为女'
     48 
     49         print(msg_content)
     50     elif msg['Type'] == 'Map':  # 如果消息为分享的位置信息
     51         x, y, location = re.search(
     52             "<location x="(.*?)" y="(.*?)".*label="(.*?)".*", msg['OriContent']).group(1, 2, 3)
     53         if location is None:
     54             msg_content = r"纬度->" + x.__str__() + " 经度->" + y.__str__()  # 内容为详细的地址
     55         else:
     56             msg_content = r"" + location
     57     elif msg['Type'] == 'Sharing':  # 如果消息为分享的音乐或者文章,详细的内容为文章的标题或者是分享的名字
     58         msg_content = msg['Text']
     59         msg_share_url = msg['Url']  # 记录分享的url
     60         print(msg_share_url)
     61     face_bug = msg_content
     62 
     63     ##将信息存储在字典中,每一个msg_id对应一条信息
     64     msg_information.update(
     65         {
     66             msg_id: {
     67                 "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec,
     68                 "msg_type": msg["Type"],
     69                 "msg_content": msg_content, "msg_share_url": msg_share_url
     70             }
     71         }
     72     )
     73 
     74 
     75 ##这个是用于监听是否有friend消息撤回
     76 @itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True)
     77 def information(msg):
     78     # 这里如果这里的msg['Content']中包含消息撤回和id,就执行下面的语句
     79     if '撤回了一条消息' in msg['Content']:
     80         old_msg_id = re.search("<msgid>(.*?)</msgid>", msg['Content']).group(1)  # 在返回的content查找撤回的消息的id
     81         old_msg = msg_information.get(old_msg_id)  # 得到消息
     82         print(old_msg)
     83         if len(old_msg_id) < 11:  # 如果发送的是表情包
     84             itchat.send_file(face_bug, toUserName=msg['FromUserName'])
     85         else:  # 发送撤回的提示给文件助手
     86             msg_body = "" 
     87                        + old_msg.get('msg_from') + " 撤回了 】
    " 
     88                        + old_msg.get("msg_type") + " 消息:" + "
    " 
     89                        + old_msg.get('msg_time_rec') + "
    " 
     90                        + r"" + old_msg.get('msg_content')
     91             # 如果是分享的文件被撤回了,那么就将分享的url加在msg_body中发送给文件助手
     92             if old_msg['msg_type'] == "Sharing":
     93                 msg_body += "
    就是这个链接➣ " + old_msg.get('msg_share_url')
     94 
     95             # 将撤回消息发送到文件助手
     96             itchat.send_msg(msg_body, toUserName=msg['FromUserName'])
     97             # 有文件的话也要将文件发送回去
     98             if old_msg["msg_type"] == "Picture" 
     99                     or old_msg["msg_type"] == "Recording" 
    100                     or old_msg["msg_type"] == "Video" 
    101                     or old_msg["msg_type"] == "Attachment":
    102                 file = '@fil@%s' % (old_msg['msg_content'])
    103                 itchat.send(msg=file, toUserName=msg['FromUserName'])
    104                 os.remove(old_msg['msg_content'])
    105             # 删除字典旧消息
    106             msg_information.pop(old_msg_id)
    107 
    108 
    109 @itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isGroupChat=True)
    110 def handle_receive_msg(msg):
    111     global face_bug
    112     msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  # 接受消息的时间
    113     # groupid = msg['FromUserName']
    114     # chatroom = itchat.search_chatrooms(userName=groupid)
    115     msg_Actual_from = msg['ActualNickName']
    116     # msg_Actual_from = msg['User']
    117     # msg_from = msg_Actual_from['Self']['NickName']
    118     msg_from = msg_Actual_from
    119     msg_time = msg['CreateTime']  # 信息发送的时间
    120     msg_id = msg['MsgId']  # 每条信息的id
    121     msg_content = None  # 储存信息的内容
    122     msg_share_url = None  # 储存分享的链接,比如分享的文章和音乐
    123     print(msg['Type'])
    124     print(msg['MsgId'])
    125     if msg['Type'] == 'Text' or msg['Type'] == 'Friends':  # 如果发送的消息是文本或者好友推荐
    126         msg_content = msg['Text']
    127         print(msg_content)
    128 
    129     # 如果发送的消息是附件、视屏、图片、语音
    130     elif msg['Type'] == "Attachment" or msg['Type'] == "Video" 
    131             or msg['Type'] == 'Picture' 
    132             or msg['Type'] == 'Recording':
    133         msg_content = msg['FileName']  # 内容就是他们的文件名
    134         msg['Text'](str(msg_content))  # 下载文件
    135         # print msg_content
    136     elif msg['Type'] == 'Card':  # 如果消息是推荐的名片
    137         msg_content = msg['RecommendInfo']['NickName'] + '的名片'  # 内容就是推荐人的昵称和性别
    138         if msg['RecommendInfo']['Sex'] == 1:
    139             msg_content += '性别为男'
    140         else:
    141             msg_content += '性别为女'
    142 
    143         print(msg_content)
    144     elif msg['Type'] == 'Map':  # 如果消息为分享的位置信息
    145         x, y, location = re.search(
    146             "<location x="(.*?)" y="(.*?)".*label="(.*?)".*", msg['OriContent']).group(1, 2, 3)
    147         if location is None:
    148             msg_content = r"纬度->" + x.__str__() + " 经度->" + y.__str__()  # 内容为详细的地址
    149         else:
    150             msg_content = r"" + location
    151     elif msg['Type'] == 'Sharing':  # 如果消息为分享的音乐或者文章,详细的内容为文章的标题或者是分享的名字
    152         msg_content = msg['Text']
    153         msg_share_url = msg['Url']  # 记录分享的url
    154         print(msg_share_url)
    155     face_bug = msg_content
    156 
    157     ##将信息存储在字典中,每一个msg_id对应一条信息
    158     msg_information.update(
    159         {
    160             msg_id: {
    161                 "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec,
    162                 "msg_type": msg["Type"],
    163                 "msg_content": msg_content, "msg_share_url": msg_share_url
    164             }
    165         }
    166     )
    167 
    168 
    169 ##这个是用于监听是否有Group消息撤回
    170 @itchat.msg_register(NOTE, isGroupChat=True, isMpChat=True)
    171 def information(msg):
    172 
    173     # 这里如果这里的msg['Content']中包含消息撤回和id,就执行下面的语句
    174     if '撤回了一条消息' in msg['Content']:
    175         old_msg_id = re.search("<msgid>(.*?)</msgid>", msg['Content']).group(1)  # 在返回的content查找撤回的消息的id
    176         old_msg = msg_information.get(old_msg_id)  # 得到消息
    177         print(old_msg)
    178         if len(old_msg_id) < 11:  # 如果发送的是表情包
    179             itchat.send_file(face_bug, toUserName=msg['FromUserName'])
    180         else:  # 发送撤回的提示给文件助手
    181             msg_body = "" 
    182                        + old_msg.get('msg_from') + " 群消息撤回提醒】
    " 
    183                        + " 撤回了 " + old_msg.get("msg_type") + " 消息:" + "
    " 
    184                        + old_msg.get('msg_time_rec') + "
    " 
    185                        + r"" + old_msg.get('msg_content')
    186             # 如果是分享的文件被撤回了,那么就将分享的url加在msg_body中发送给文件助手
    187             if old_msg['msg_type'] == "Sharing":
    188                 msg_body += "
    就是这个链接➣ " + old_msg.get('msg_share_url')
    189 
    190             # 将撤回消息发送到文件助手
    191             itchat.send_msg(msg_body, toUserName=msg['FromUserName'])
    192             # 有文件的话也要将文件发送回去
    193             if old_msg["msg_type"] == "Picture" 
    194                     or old_msg["msg_type"] == "Recording" 
    195                     or old_msg["msg_type"] == "Video" 
    196                     or old_msg["msg_type"] == "Attachment":
    197                 file = '@fil@%s' % (old_msg['msg_content'])
    198                 itchat.send(msg=file, toUserName=msg['FromUserName'])
    199                 os.remove(old_msg['msg_content'])
    200             # 删除字典旧消息
    201             msg_information.pop(old_msg_id)
    202 
    203 
    204 # Main (enableCmdQr = True 时,将会生成二维码图片,如 =2 时二维码乱码的话 改为1 即可
    205 itchat.auto_login(enableCmdQR=2, hotReload=True) 
    206 itchat.run()
  • 相关阅读:
    题解 P3960 【列队】
    题解 P3825 【[NOI2017]游戏】
    题解 P3385 【【模板】负环】
    luoguP1555 尴尬的数字(暴力+map)
    luogu P1775 古代人的难题_NOI导刊2010提高(02)(斐波纳契+数学)
    luogu P1405 苦恼的小明(欧拉定理)
    luogu P2117 小Z的矩阵(结论题)
    BZOJ2870 最长道路tree(并查集+LCA)
    BZOJ 4668 冷战(按秩合并并查集+LCA)
    BZOJ 3376 [Usaco2004 Open]Cube Stacking 方块游戏(带权并查集)
  • 原文地址:https://www.cnblogs.com/wanglinjie/p/9291999.html
Copyright © 2011-2022 走看看