python微信撤回监测代码


Posted in Python onApril 29, 2019

 本文实例为大家分享了python微信撤回的监测代码,供大家参考,具体内容如下

注意:这里用了一个wechat库,当然,wechat库是基于微信提供的官方接口实现的。

这里的核心就是通过网页登陆微信的方式,然后获取各个通讯信息,然后存进内存,最后检测各种微信的操作,最后写入微信里面的文件传输助手即可。

直接看代码,然后运行,慢慢调试几次,就明白咋回事了。

#coding=utf8
import itchat
import requests
import time
import os
import re
import threading

#全局变量,对于每个用户的机器人开关
User_bot_control_flag = {}
#全局变量,我的昵称
myNickName = ''

def bot_chat_init():
  # 获取好友列表
  friends = itchat.get_friends(update=True)[0:]
  #将标志位置为0
  for i in friends[1:]:
    User_bot_control_flag[i["UserName"]] = 0


@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
  #  获取到发送消息者身份,如果身份匹配,就做对应的事
  # itchat.send_msg('已经收到了文本消息,消息内容为%s' % msg['Text'], toUserName=msg['FromUserName'])
  # 如果图灵Key出现问题,那么reply将会是None
  if msg['Text']=='service crond start':
    return u'你一看就是个程序员'
  if msg['Text'] == 'dididididi':
    return u'开车了'
  reply = get_response(msg['Text'])
  if not msg['FromUserName'] == myUserName:
    pass
    # 发送一条提示给文件助手
    # itchat.send_msg(u"[%s]收到好友@%s 的信息:%s\n" %
    #         (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime'])),
    #         msg['User']['NickName'],
    #         msg['Text']), 'filehelper')
  # a or b的意思是,如果a有内容,那么返回a,否则返回b
  # 有内容一般就是指非空或者非None,你可以用`if a: print('True')`来测试
  return reply or u'[自动回复]您好,我现在有事不在,一会再和您联系。\n已经收到您的的信息:%s\n' % (msg['Text'])


def friend():
  # 初始化计数器,有男有女,当然,有些人是不填的
  # 获取好友列表
  friends = itchat.get_friends(update=True)[0:]
  male = female = other = 0

  # 遍历这个列表,列表里第一位是自己,所以从"自己"之后开始计算
  # 1表示男性,2女性
  for i in friends[1:]:
    print (i)      #打印出签名
    sex = i["Sex"]
    if sex == 1:
      male += 1
    elif sex == 2:
      female += 1
    else:
      other += 1
  # 总数算上,好计算比例啊~
  total = len(friends[1:])
  # 好了,打印结果
  print(u"共有好友:%d" % total)
  print (u"男性好友:%.2f%%" % (float(male) / total * 100))
  print (u"女性好友:%.2f%%" % (float(female) / total * 100))
  print (u"其他:%.2f%%" % (float(other) / total * 100))

def get_response(msg):
  # 这里我们就像在“3. 实现最简单的与图灵机器人的交互”中做的一样
  # 构造了要发送给服务器的数据
  apiUrl = 'http://www.tuling123.com/openapi/api'
  data = {
    'key'  : key,
    'info'  : msg,
    'userid' : 'wechat-robot',
  }
  try:
    r = requests.post(apiUrl, data=data).json()
    # 字典的get方法在字典没有'text'值的时候会返回None而不会抛出异常
    return r.get('text')+'----来自机器人小Z的智能回复----'
  # 为了防止服务器没有正常响应导致程序异常退出,这里用try-except捕获了异常
  # 如果服务器没能正常交互(返回非json或无法连接),那么就会进入下面的return
  except:
    # 将会返回一个None
    return


@itchat.msg_register(itchat.content.TEXT, isGroupChat=True) #msg['ActualNickName'] 群里发消息的人名 #msg['User']['NickName'] 群名称
def text_reply(msg):
  # print (msg['User'])    #一个宏大的结构体
  # print ("群聊名字"+msg['User']['NickName']) #群聊名称
  # print (msg['FromUserName'])
  #监控所有群的消息,后来做统计用,后面可以做关键词分析什么的
  file_object = open(myNickName+"群"+msg['User']['NickName'], 'a')
  write_data = ''.join(time.strftime("%Y-%m-%d %H:%M:%S" , time.localtime(msg['CreateTime'])))+" "+msg['ActualNickName']+": "+msg['Text']+"\n"
  file_object.write(write_data)
  file_object.close()
  #指定群聊可以智能群聊
  if msg['User']['NickName'] == '184':
    print (" 184 ok")
    itchat.send(get_response(msg['Text']),msg['FromUserName'])
  #监控群聊内容发送到文件助手,已经被自己屏蔽掉了
  # itchat.send_msg(u"[%s]收到%s群 %s 的信息:%s\n" %
  #         (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime']))
  #         ,msg['User']['NickName'],msg['ActualNickName'],
  #         msg['Text']), 'filehelper')
  # 判断是否有人@自己
  if (msg.isAt):
   # 如果有人@自己,就发一个消息告诉对方我已经收到了信息
    itchat.send_msg("我已经收到了来自{0}的消息,实际内容为{1}".format(msg['ActualNickName'], msg['Text']),
      toUserName=msg['FromUserName'])

# def sendmsgToPsh():
#   while (True):
#     pass
#     # print ("123456")
#
# threads = []
# t1 = threading.Thread(target=sendmsgToPsh())


# 说明:可以撤回的有文本文字、语音、视频、图片、位置、名片、分享、附件

# {msg_id:(msg_from,msg_to,msg_time,msg_time_rec,msg_type,msg_content,msg_share_url)}
msg_dict = {}

# 文件存储临时目录
rev_tmp_dir = "/home/seen/PycharmProjects/Code"
if not os.path.exists(rev_tmp_dir): os.mkdir(rev_tmp_dir)

# 表情有一个问题 | 接受信息和接受note的msg_id不一致 巧合解决方案
face_bug = None


# # 将接收到的消息存放在字典中,当接收到新消息时对字典中超时的消息进行清理 | 不接受不具有撤回功能的信息
# # [TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS, NOTE]
# @itchat.msg_register([itchat.content.TEXT, itchat.content.PICTURE, itchat.content.MAP, itchat.content.CARD, itchat.content.SHARING,
#            itchat.content.RECORDING,itchat.content. ATTACHMENT, itchat.content.VIDEO],isGroupChat=True)
# def handler_receive_msg(msg):
#   #回复特定用户消息
#   # if msg['User']['NickName']=='YYYYY' or msg['User']['NickName']=='彭芊芊':
#   #   print ("yhj ok")
#   #   itchat.send_msg(get_response(msg['Text']), toUserName=msg['FromUserName'])
#   # 先获取对方说来的话
#   # 下面一行是获取发送消息者昵称
#   send_user_name = itchat.search_friends(userName=msg['FromUserName'])['NickName']
#   file_object = open(myNickName + "&" + msg['User']['NickName'], 'a')
#   write_data = ''.join(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime']))) + " " + \
#         send_user_name + ": " + msg['Text'] + "\n"
#   file_object.write(write_data)
#   file_object.close()
#
#   #控制指令检测模块
#   if msg['Text'] == 'service robot start':
#     User_bot_control_flag[msg['FromUserName']]=1   #检测到开启指令后开启机器人
#     itchat.send_msg("Robot small Z started...waiting for your service", toUserName=msg['FromUserName'])
#   if msg['Text'] == 'service robot stop':
#     User_bot_control_flag[msg['FromUserName']]=0   #检测到开启指令后关闭机器人
#     itchat.send_msg("Robot small Z stoped...get 'service robot start' restarted", toUserName=msg['FromUserName'])
#   #在开关开启的情况下回复对方对话
#   if not msg['FromUserName'] == myUserName:
#     if User_bot_control_flag[msg['FromUserName']]:
#       # 存储单人对话模块
#       # 下面一行是获取发送消息者昵称
#       reply = get_response(msg['Text'])
#       file_object = open(myNickName + "&" + msg['User']['NickName'], 'a')
#       write_data = ''.join(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime']))) + " " + \
#             myNickName + ": " + reply + "\n"
#       file_object.write(write_data)
#       file_object.close()
#       itchat.send_msg(reply, toUserName=msg['FromUserName'])
#
#   global face_bug
#   # 获取的是本地时间戳并格式化本地时间戳 e: 2017-04-21 21:30:08
#   msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
#   # 消息ID
#   msg_id = msg['MsgId']
#   # 消息时间
#   msg_time = msg['CreateTime']
#   # 消息发送人昵称 | 这里也可以使用RemarkName备注 但是自己或者没有备注的人为None
#   msg_from = (itchat.search_friends(userName=msg['FromUserName']))["NickName"]
#   # 消息内容
#   msg_content = None
#   # 分享的链接
#   msg_share_url = None
#   if msg['Type'] == 'Text' \
#       or msg['Type'] == 'Friends':
#     msg_content = msg['Text']
#   elif msg['Type'] == 'Recording' \
#       or msg['Type'] == 'Attachment' \
#       or msg['Type'] == 'Video' \
#       or msg['Type'] == 'Picture':
#     msg_content = r"" + msg['FileName']
#     # 保存文件
#     msg['Text'](rev_tmp_dir + msg['FileName'])
#   elif msg['Type'] == 'Card':
#     msg_content = msg['RecommendInfo']['NickName'] + r" 的名片"
#   elif msg['Type'] == 'Map':
#     x, y, location = re.search(
#       "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3)
#     if location is None:
#       msg_content = r"纬度->" + x.__str__() + " 经度->" + y.__str__()
#     else:
#       msg_content = r"" + location
#   elif msg['Type'] == 'Sharing':
#     msg_content = msg['Text']
#     msg_share_url = msg['Url']
#   face_bug = msg_content
#   # 更新字典
#   msg_dict.update(
#     {
#       msg_id: {
#         "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec,
#         "msg_type": msg["Type"],
#         "msg_content": msg_content, "msg_share_url": msg_share_url
#       }
#     }
#   )

# 将接收到的消息存放在字典中,当接收到新消息时对字典中超时的消息进行清理 | 不接受不具有撤回功能的信息
# [TEXT, PICTURE, MAP, CARD, SHARING, RECORDING, ATTACHMENT, VIDEO, FRIENDS, NOTE]
@itchat.msg_register([itchat.content.TEXT, itchat.content.PICTURE, itchat.content.MAP, itchat.content.CARD, itchat.content.SHARING,
           itchat.content.RECORDING,itchat.content. ATTACHMENT, itchat.content.VIDEO])
def handler_receive_msg(msg):
  #回复特定用户消息
  # if msg['User']['NickName']=='YYYYY' or msg['User']['NickName']=='彭芊芊':
  #   print ("yhj ok")
  #   itchat.send_msg(get_response(msg['Text']), toUserName=msg['FromUserName'])
  # 先获取对方说来的话
  # 下面一行是获取发送消息者昵称
  send_user_name = itchat.search_friends(userName=msg['FromUserName'])['NickName']
  file_object = open(myNickName + "&" + msg['User']['NickName'], 'a')
  write_data = ''.join(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime']))) + " " + \
         send_user_name + ": " + msg['Text'] + "\n"
  file_object.write(write_data)
  file_object.close()

  #控制指令检测模块
  if msg['Text'] == 'service robot start':
    User_bot_control_flag[msg['FromUserName']]=1   #检测到开启指令后开启机器人
    itchat.send_msg("Robot small Z started...waiting for your service", toUserName=msg['FromUserName'])
  if msg['Text'] == 'service robot stop':
    User_bot_control_flag[msg['FromUserName']]=0   #检测到开启指令后关闭机器人
    itchat.send_msg("Robot small Z stoped...get 'service robot start' restarted", toUserName=msg['FromUserName'])
  #在开关开启的情况下回复对方对话
  if not msg['FromUserName'] == myUserName:
    if User_bot_control_flag[msg['FromUserName']]:
      # 存储单人对话模块
      # 下面一行是获取发送消息者昵称
      reply = get_response(msg['Text'])
      file_object = open(myNickName + "&" + msg['User']['NickName'], 'a')
      write_data = ''.join(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime']))) + " " + \
             myNickName + ": " + reply + "\n"
      file_object.write(write_data)
      file_object.close()
      itchat.send_msg(reply, toUserName=msg['FromUserName'])

  global face_bug
  # 获取的是本地时间戳并格式化本地时间戳 e: 2017-04-21 21:30:08
  msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  # 消息ID
  msg_id = msg['MsgId']
  # 消息时间
  msg_time = msg['CreateTime']
  # 消息发送人昵称 | 这里也可以使用RemarkName备注 但是自己或者没有备注的人为None
  msg_from = (itchat.search_friends(userName=msg['FromUserName']))["NickName"]
  # 消息内容
  msg_content = None
  # 分享的链接
  msg_share_url = None
  if msg['Type'] == 'Text' \
      or msg['Type'] == 'Friends':
    msg_content = msg['Text']
  elif msg['Type'] == 'Recording' \
      or msg['Type'] == 'Attachment' \
      or msg['Type'] == 'Video' \
      or msg['Type'] == 'Picture':
    msg_content = r"" + msg['FileName']
    # 保存文件
    msg['Text'](rev_tmp_dir + msg['FileName'])
  elif msg['Type'] == 'Card':
    msg_content = msg['RecommendInfo']['NickName'] + r" 的名片"
  elif msg['Type'] == 'Map':
    x, y, location = re.search(
      "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3)
    if location is None:
      msg_content = r"纬度->" + x.__str__() + " 经度->" + y.__str__()
    else:
      msg_content = r"" + location
  elif msg['Type'] == 'Sharing':
    msg_content = msg['Text']
    msg_share_url = msg['Url']
  face_bug = msg_content
  # 更新字典
  msg_dict.update(
    {
      msg_id: {
        "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec,
        "msg_type": msg["Type"],
        "msg_content": msg_content, "msg_share_url": msg_share_url
      }
    }
  )


# # 收到note通知类消息,判断是不是撤回并进行相应操作,针对于群
# @itchat.msg_register([itchat.content.NOTE],isGroupChat=True)
# def send_msg_helper(msg):
#   global face_bug
#   if re.search(r"\<\!\[CDATA\[.*撤回了一条消息\]\]\>", msg['Content']) is not None:
#     # 获取消息的id
#     old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1)
#     old_msg = msg_dict.get(old_msg_id, {})
#     if len(old_msg_id) < 11:
#       itchat.send_file(rev_tmp_dir + face_bug, toUserName='filehelper')
#       os.remove(rev_tmp_dir + face_bug)
#     else:
#       msg_body = "告诉你一个秘密~" + "\n" \
#            + old_msg.get('msg_from') + " 撤回了 " + old_msg.get("msg_type") + " 消息" + "\n" \
#            + old_msg.get('msg_time_rec') + "\n" \
#            + "撤回了什么 ⇣" + "\n" \
#            + r"" + old_msg.get('msg_content')
#       # 如果是分享存在链接
#       if old_msg['msg_type'] == "Sharing": msg_body += "\n就是这个链接➣ " + old_msg.get('msg_share_url')
#
#       # 将撤回消息发送到文件助手
#       itchat.send(msg_body, toUserName='filehelper')
#       # 有文件的话也要将文件发送回去
#       if old_msg["msg_type"] == "Picture" \
#           or old_msg["msg_type"] == "Recording" \
#           or old_msg["msg_type"] == "Video" \
#           or old_msg["msg_type"] == "Attachment":
#         file = '@fil@%s' % (rev_tmp_dir + old_msg['msg_content'])
#         itchat.send(msg=file, toUserName='filehelper')
#         os.remove(rev_tmp_dir + old_msg['msg_content'])
#       # 删除字典旧消息
#       msg_dict.pop(old_msg_id)

# 收到note通知类消息,判断是不是撤回并进行相应操作
@itchat.msg_register([itchat.content.NOTE])
def send_msg_helper(msg):
  global face_bug
  if re.search(r"\<\!\[CDATA\[.*撤回了一条消息\]\]\>", msg['Content']) is not None:
    # 获取消息的id
    old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1)
    old_msg = msg_dict.get(old_msg_id, {})
    if len(old_msg_id) < 11:
      itchat.send_file(rev_tmp_dir + face_bug, toUserName='filehelper')
      os.remove(rev_tmp_dir + face_bug)
    else:
      msg_body = "告诉你一个秘密~" + "\n" \
            + old_msg.get('msg_from') + " 撤回了 " + old_msg.get("msg_type") + " 消息" + "\n" \
            + old_msg.get('msg_time_rec') + "\n" \
            + "撤回了什么 ⇣" + "\n" \
            + r"" + old_msg.get('msg_content')
      # 如果是分享存在链接
      if old_msg['msg_type'] == "Sharing": msg_body += "\n就是这个链接➣ " + old_msg.get('msg_share_url')

      # 将撤回消息发送到文件助手
      itchat.send(msg_body, toUserName='filehelper')
      # 有文件的话也要将文件发送回去
      if old_msg["msg_type"] == "Picture" \
          or old_msg["msg_type"] == "Recording" \
          or old_msg["msg_type"] == "Video" \
          or old_msg["msg_type"] == "Attachment":
        file = '@fil@%s' % (rev_tmp_dir + old_msg['msg_content'])
        itchat.send(msg=file, toUserName='filehelper')
        os.remove(rev_tmp_dir + old_msg['msg_content'])
      # 删除字典旧消息
      msg_dict.pop(old_msg_id)




key = '02dd1dd1b5594e179aa4aca9a6a690a6'
if __name__ == '__main__':
  itchat.auto_login(hotReload=True)
  # 获取自己的UserName
  myNickName = itchat.get_friends(update=True)[0]["NickName"]
  myUserName = itchat.get_friends(update=True)[0]["UserName"]
  #做函数功能的实验
  # print (itchat.search_friends(name='彭芊芊')[0]['UserName'])    #我居然会用了这种办法我是真的猛
  # print(type(itchat.search_friends(name='彭芊芊')))
  #itchat.send("init messages to dindsong,A message from bangbangtang,distant areas...", toUserName='@509f2668d9380a6aeb1951585256827dc1d475c2de885b62fae401401d522f9b')
  friend()     #获取朋友信息

  bot_chat_init()  #初始化开关模块
  itchat.run()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python通过poll实现异步IO的方法
Jun 04 Python
结合Python的SimpleHTTPServer源码来解析socket通信
Jun 27 Python
Python中Proxypool库的安装与配置
Oct 19 Python
Matplotlib中文乱码的3种解决方案
Nov 15 Python
对python操作kafka写入json数据的简单demo分享
Dec 27 Python
python列表,字典,元组简单用法示例
Jul 11 Python
如何将 awk 脚本移植到 Python
Dec 09 Python
Python使用Tkinter实现转盘抽奖器的步骤详解
Jan 06 Python
python读取csv文件指定行的2种方法详解
Feb 13 Python
Django User 模块之 AbstractUser 扩展详解
Mar 11 Python
Python Numpy中数据的常用保存与读取方法
Apr 01 Python
Python还能这么玩之用Python修改了班花的开机密码
Jun 04 Python
Python3.5 Json与pickle实现数据序列化与反序列化操作示例
Apr 29 #Python
详解Python中的内建函数,可迭代对象,迭代器
Apr 29 #Python
python抓取需要扫微信登陆页面
Apr 29 #Python
python基于itchat模块实现微信防撤回
Apr 29 #Python
手把手教你使用Python创建微信机器人
Apr 29 #Python
python实现微信防撤回神器
Apr 29 #Python
python实现文件助手中查看微信撤回消息
Apr 29 #Python
You might like
php操作xml入门之xml标签的属性分析
2015/01/23 PHP
php源码分析之DZX1.5加密解密函数authcode用法
2015/06/17 PHP
PHP curl使用实例
2015/07/02 PHP
php实现分页功能的详细实例方法
2019/09/29 PHP
犀利的js 函数集合
2009/06/11 Javascript
浅析js中的浮点型运算问题
2014/01/06 Javascript
详解JavaScript中数组和字符串的lastIndexOf()方法使用
2016/03/13 Javascript
jQuery插件cxSelect多级联动下拉菜单实例解析
2016/06/24 Javascript
值得分享的bootstrap table实例
2016/09/22 Javascript
微信小程序 WXDropDownMenu组件详解及实例代码
2016/10/24 Javascript
JavaScript中splice与slice的区别
2017/05/09 Javascript
[js高手之路]图解javascript的原型(prototype)对象,原型链实例
2017/08/28 Javascript
vue中post请求以a=a&amp;b=b 的格式写遇到的问题
2018/04/27 Javascript
H5+C3+JS实现五子棋游戏(AI篇)
2020/05/28 Javascript
微信小程序实现文字从右向左无限滚动
2020/11/18 Javascript
详解js创建对象的几种方法及继承
2019/04/12 Javascript
vue项目中仿element-ui弹框效果的实例代码
2019/04/22 Javascript
详解Vue-Router源码分析路由实现原理
2019/05/15 Javascript
小程序如何构建骨架屏
2019/05/29 Javascript
[03:04]2018年度DOTA2玩家最喜爱的主播-完美盛典
2018/12/16 DOTA
[57:36]DOTA2-DPC中国联赛 正赛 SAG vs CDEC BO3 第三场 2月1日
2021/03/11 DOTA
使用matplotlib画散点图的方法
2018/05/25 Python
python 读取文本文件的行数据,文件.splitlines()的方法
2018/07/12 Python
在python中使用requests 模拟浏览器发送请求数据的方法
2018/12/26 Python
详解Windows下PyCharm安装Numpy包及无法安装问题解决方案
2020/06/18 Python
Python Selenium操作Cookie的实例方法
2021/02/28 Python
HTML5跳转小程序wx-open-launch-weapp的示例代码
2020/07/16 HTML / CSS
美国在线鲜花速递:ProFlowers
2017/01/05 全球购物
英国女性时尚精品店:THE DRESSING ROOM
2018/05/23 全球购物
土木工程专业个人求职信
2013/12/05 职场文书
2014年服装销售工作总结
2014/11/27 职场文书
2014小学年度工作总结
2014/12/20 职场文书
结婚保证书(卖身契)
2015/02/26 职场文书
2015年设计师个人工作总结
2015/04/25 职场文书
毕业班工作总结
2015/08/10 职场文书
go语言求任意类型切片的长度操作
2021/04/26 Golang