Advertisement
mengyuxin

wechat.get_old_msg.py

Jan 13th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.37 KB | None | 0 0
  1. # coding:utf-8
  2.  
  3. """
  4. 作者:爱撒谎的男孩   Updated by Meng Yuxin on 2018-01-14
  5. 链接:https://www.jianshu.com/p/9a1cfc761b7e
  6. """
  7.  
  8. import itchat
  9. from itchat.content import TEXT
  10. from itchat.content import *
  11. import sys
  12. import time
  13. import re
  14.  
  15. """
  16. 注意:
  17. 1. Python 3 与 Python 2 有很大的区别,其中Python 3 系统默认使用的就是utf-8编码。
  18. 2. 所以,对于使用的是Python 3 的情况,就不需要sys.setdefaultencoding("utf-8")这段代码。
  19. 3. 最重要的是,Python 3 的 sys 库里面已经没有 setdefaultencoding() 函数了。
  20. """
  21. #import imp
  22. #imp.reload(sys)
  23. #sys.setdefaultencoding('utf8')
  24.  
  25. import os
  26.  
  27. msg_information = {}
  28. face_bug=None  #针对表情包的内容
  29.  
  30.  
  31. @itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO],isFriendChat=True, isGroupChat=True, isMpChat=True)
  32. def handle_receive_msg(msg):
  33.     global face_bug
  34.     msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())   #接受消息的时间
  35.     msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName']   #在好友列表中查询发送信息的好友昵称
  36.     msg_time = msg['CreateTime']    #信息发送的时间
  37.     msg_id = msg['MsgId']    #每条信息的id
  38.     msg_content = None      #储存信息的内容
  39.     msg_share_url = None    #储存分享的链接,比如分享的文章和音乐
  40.     print( msg['Type'] )
  41.     print( msg['MsgId'] )
  42.     if msg['Type'] == 'Text' or msg['Type'] == 'Friends':     #如果发送的消息是文本或者好友推荐
  43.         msg_content = msg['Text']
  44.         print( msg_content )
  45.  
  46.     #如果发送的消息是附件、视屏、图片、语音
  47.     elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \
  48.             or msg['Type'] == 'Picture' \
  49.             or msg['Type'] == 'Recording':
  50.         msg_content = msg['FileName']    #内容就是他们的文件名
  51.         msg['Text'](str(msg_content))    #下载文件
  52.         # print( msg_content )
  53.     elif msg['Type'] == 'Card':    #如果消息是推荐的名片
  54.         msg_content = msg['RecommendInfo']['NickName'] + '的名片'    #内容就是推荐人的昵称和性别
  55.         if msg['RecommendInfo']['Sex'] == 1:
  56.             msg_content += '性别为男'
  57.         else:
  58.             msg_content += '性别为女'
  59.  
  60.         print( msg_content )
  61.     elif msg['Type'] == 'Map':    #如果消息为分享的位置信息
  62.         x, y, location = re.search(
  63.             "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3)
  64.         if location is None:
  65.             msg_content = r"纬度->" + x.__str__() + " 经度->" + y.__str__()     #内容为详细的地址
  66.         else:
  67.             msg_content = r"" + location
  68.     elif msg['Type'] == 'Sharing':     #如果消息为分享的音乐或者文章,详细的内容为文章的标题或者是分享的名字
  69.         msg_content = msg['Text']
  70.         msg_share_url = msg['Url']       #记录分享的url
  71.         print( msg_share_url )
  72.     face_bug=msg_content
  73.  
  74. ##将信息存储在字典中,每一个msg_id对应一条信息
  75.     msg_information.update(
  76.         {
  77.             msg_id: {
  78.                 "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec,
  79.                 "msg_type": msg["Type"],
  80.                 "msg_content": msg_content, "msg_share_url": msg_share_url
  81.             }
  82.         }
  83.     )
  84.  
  85.  
  86. ##这个是用于监听是否有消息撤回
  87. @itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True)
  88. def information(msg):
  89.     #这里如果这里的msg['Content']中包含消息撤回和id,就执行下面的语句
  90.     if '撤回了一条消息' in msg['Content']:
  91.         old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1)   #在返回的content查找撤回的消息的id
  92.         old_msg = msg_information.get(old_msg_id)    #得到消息
  93.         print( old_msg )
  94.         if len(old_msg_id)<11:  #如果发送的是表情包
  95.             itchat.send_file(face_bug,toUserName='filehelper')
  96.         else:  #发送撤回的提示给文件助手
  97.             msg_body = "告诉你一个秘密~" + "\n" \
  98.                        + old_msg.get('msg_from') + " 撤回了 " + old_msg.get("msg_type") + " 消息" + "\n" \
  99.                        + old_msg.get('msg_time_rec') + "\n" \
  100.                        + "撤回了什么 ⇣" + "\n" \
  101.                        + r"" + old_msg.get('msg_content')
  102.             #如果是分享的文件被撤回了,那么就将分享的url加在msg_body中发送给文件助手
  103.             if old_msg['msg_type'] == "Sharing":
  104.                 msg_body += "\n就是这个链接➣ " + old_msg.get('msg_share_url')
  105.  
  106.             # 将撤回消息发送到文件助手
  107.             itchat.send_msg(msg_body, toUserName='filehelper')
  108.             # 有文件的话也要将文件发送回去
  109.             if old_msg["msg_type"] == "Picture" \
  110.                     or old_msg["msg_type"] == "Recording" \
  111.                     or old_msg["msg_type"] == "Video" \
  112.                     or old_msg["msg_type"] == "Attachment":
  113.                 file = '@fil@%s' % (old_msg['msg_content'])
  114.                 itchat.send(msg=file, toUserName='filehelper')
  115.                 os.remove(old_msg['msg_content'])
  116.             # 删除字典旧消息
  117.             msg_information.pop(old_msg_id)
  118.  
  119. itchat.auto_login(hotReload=True)
  120. itchat.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement