您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“python如何實現(xiàn)微信撤回監(jiān)測”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“python如何實現(xiàn)微信撤回監(jiān)測”這篇文章吧。
具體內(nèi)容如下
注意:這里用了一個wechat庫,當(dāng)然,wechat庫是基于微信提供的官方接口實現(xiàn)的。
這里的核心就是通過網(wǎng)頁登陸微信的方式,然后獲取各個通訊信息,然后存進(jìn)內(nèi)存,最后檢測各種微信的操作,最后寫入微信里面的文件傳輸助手即可。
直接看代碼,然后運行,慢慢調(diào)試幾次,就明白咋回事了。
#coding=utf8 import itchat import requests import time import os import re import threading #全局變量,對于每個用戶的機(jī)器人開關(guān) User_bot_control_flag = {} #全局變量,我的昵稱 myNickName = '' def bot_chat_init(): # 獲取好友列表 friends = itchat.get_friends(update=True)[0:] #將標(biāo)志位置為0 for i in friends[1:]: User_bot_control_flag[i["UserName"]] = 0 @itchat.msg_register(itchat.content.TEXT) def tuling_reply(msg): # 獲取到發(fā)送消息者身份,如果身份匹配,就做對應(yīng)的事 # itchat.send_msg('已經(jīng)收到了文本消息,消息內(nèi)容為%s' % msg['Text'], toUserName=msg['FromUserName']) # 如果圖靈Key出現(xiàn)問題,那么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 # 發(fā)送一條提示給文件助手 # 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有內(nèi)容,那么返回a,否則返回b # 有內(nèi)容一般就是指非空或者非None,你可以用`if a: print('True')`來測試 return reply or u'[自動回復(fù)]您好,我現(xiàn)在有事不在,一會再和您聯(lián)系。\n已經(jīng)收到您的的信息:%s\n' % (msg['Text']) def friend(): # 初始化計數(shù)器,有男有女,當(dāng)然,有些人是不填的 # 獲取好友列表 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 # 總數(shù)算上,好計算比例啊~ total = len(friends[1:]) # 好了,打印結(jié)果 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. 實現(xiàn)最簡單的與圖靈機(jī)器人的交互”中做的一樣 # 構(gòu)造了要發(fā)送給服務(wù)器的數(shù)據(jù) 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')+'----來自機(jī)器人小Z的智能回復(fù)----' # 為了防止服務(wù)器沒有正常響應(yīng)導(dǎo)致程序異常退出,這里用try-except捕獲了異常 # 如果服務(wù)器沒能正常交互(返回非json或無法連接),那么就會進(jìn)入下面的return except: # 將會返回一個None return @itchat.msg_register(itchat.content.TEXT, isGroupChat=True) #msg['ActualNickName'] 群里發(fā)消息的人名 #msg['User']['NickName'] 群名稱 def text_reply(msg): # print (msg['User']) #一個宏大的結(jié)構(gòu)體 # print ("群聊名字"+msg['User']['NickName']) #群聊名稱 # print (msg['FromUserName']) #監(jiān)控所有群的消息,后來做統(tǒng)計用,后面可以做關(guān)鍵詞分析什么的 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']) #監(jiān)控群聊內(nèi)容發(fā)送到文件助手,已經(jīng)被自己屏蔽掉了 # 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): # 如果有人@自己,就發(fā)一個消息告訴對方我已經(jīng)收到了信息 itchat.send_msg("我已經(jīng)收到了來自{0}的消息,實際內(nèi)容為{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 # # 將接收到的消息存放在字典中,當(dāng)接收到新消息時對字典中超時的消息進(jìn)行清理 | 不接受不具有撤回功能的信息 # # [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): # #回復(fù)特定用戶消息 # # if msg['User']['NickName']=='YYYYY' or msg['User']['NickName']=='彭芊芊': # # print ("yhj ok") # # itchat.send_msg(get_response(msg['Text']), toUserName=msg['FromUserName']) # # 先獲取對方說來的話 # # 下面一行是獲取發(fā)送消息者昵稱 # 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 #檢測到開啟指令后開啟機(jī)器人 # 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 #檢測到開啟指令后關(guān)閉機(jī)器人 # itchat.send_msg("Robot small Z stoped...get 'service robot start' restarted", toUserName=msg['FromUserName']) # #在開關(guān)開啟的情況下回復(fù)對方對話 # if not msg['FromUserName'] == myUserName: # if User_bot_control_flag[msg['FromUserName']]: # # 存儲單人對話模塊 # # 下面一行是獲取發(fā)送消息者昵稱 # 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'] # # 消息發(fā)送人昵稱 | 這里也可以使用RemarkName備注 但是自己或者沒有備注的人為None # msg_from = (itchat.search_friends(userName=msg['FromUserName']))["NickName"] # # 消息內(nèi)容 # 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__() + " 經(jīng)度->" + 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 # } # } # ) # 將接收到的消息存放在字典中,當(dāng)接收到新消息時對字典中超時的消息進(jìn)行清理 | 不接受不具有撤回功能的信息 # [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): #回復(fù)特定用戶消息 # if msg['User']['NickName']=='YYYYY' or msg['User']['NickName']=='彭芊芊': # print ("yhj ok") # itchat.send_msg(get_response(msg['Text']), toUserName=msg['FromUserName']) # 先獲取對方說來的話 # 下面一行是獲取發(fā)送消息者昵稱 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 #檢測到開啟指令后開啟機(jī)器人 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 #檢測到開啟指令后關(guān)閉機(jī)器人 itchat.send_msg("Robot small Z stoped...get 'service robot start' restarted", toUserName=msg['FromUserName']) #在開關(guān)開啟的情況下回復(fù)對方對話 if not msg['FromUserName'] == myUserName: if User_bot_control_flag[msg['FromUserName']]: # 存儲單人對話模塊 # 下面一行是獲取發(fā)送消息者昵稱 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'] # 消息發(fā)送人昵稱 | 這里也可以使用RemarkName備注 但是自己或者沒有備注的人為None msg_from = (itchat.search_friends(userName=msg['FromUserName']))["NickName"] # 消息內(nèi)容 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__() + " 經(jīng)度->" + 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通知類消息,判斷是不是撤回并進(jìn)行相應(yīng)操作,針對于群 # @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') # # # 將撤回消息發(fā)送到文件助手 # itchat.send(msg_body, toUserName='filehelper') # # 有文件的話也要將文件發(fā)送回去 # 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通知類消息,判斷是不是撤回并進(jìn)行相應(yīng)操作 @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') # 將撤回消息發(fā)送到文件助手 itchat.send(msg_body, toUserName='filehelper') # 有文件的話也要將文件發(fā)送回去 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"] #做函數(shù)功能的實驗 # 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() #初始化開關(guān)模塊 itchat.run()
以上是“python如何實現(xiàn)微信撤回監(jiān)測”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。