2019-04-29 14:57:23 2401瀏覽
今天扣丁學(xué)堂Python培訓(xùn)老師給大家分享一個(gè)基于itchat模塊實(shí)現(xiàn)微信防撤回的案例,比如有時(shí)候,女神發(fā)來一條消息,說約你看電影,她考慮了一下,又撤回了,不約你了…而你又想知道她究竟發(fā)了什么,該怎么辦?微信防撤回了解一下。
pip install itchat
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'jiangwenwen' import itchat from itchat.content import * import time import re import os print("該程序由里客云資源站開發(fā),網(wǎng)址:likeyunba.com") print("作者:TANKING") print("打開程序會(huì)彈出一個(gè)二維碼,微信掃碼") print("如果二維碼彈不出,那就在你這個(gè)程序的同一個(gè)目錄下找到QR.png雙擊打開掃碼") print("掃碼后,出現(xiàn)Start auto replying就可以實(shí)時(shí)監(jiān)控消息了...") msg_information = {} # 針對(duì)表情包的內(nèi)容 face_bug = None @itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO], isFriendChat=True, isMpChat=True) def handle_receive_msg(msg): global face_bug # 接收消息的時(shí)間 msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 在好友列表列表中查詢發(fā)送信息的好友昵稱 msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName'] # 信息發(fā)送的時(shí)間 msg_time = msg['CreateTime'] # 每條信息的ID msg_id = msg['MsgId'] # 儲(chǔ)存信息的內(nèi)容 msg_content = None # 儲(chǔ)存分享的連接,比如分享的文章和音樂 msg_share_url = None # 如果發(fā)送的消息是文本或者好友推薦 if msg['Type'] == 'Text' or msg['Type'] == 'Friends': msg_content = msg['Text'] print(msg_content) # 如果發(fā)送的消息是附件,視頻,圖片,語音 elif msg['Type'] == 'Attachment' or msg['Type'] == 'Video' \ or msg['Type'] == 'Picture'\ or msg['Type'] == 'Recording': # 內(nèi)容為下載文件名 msg_content = msg['FileName'] msg['Text'](str(msg_content)) # 如果消息是推薦的名片 elif msg['Type'] == 'Card': # 內(nèi)容是推薦人的昵稱和性別 msg_content = msg['RecommendInfo']['NickName'] + '的名片' if msg['RecommendInfo']['Sex'] == 1: msg_content += '性別為男' else: msg_content += '性別為女' print(msg_content) # 如果消息為分享的位置信息 elif msg['Type'] == 'Map': x, y, location = re.search( "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3) if location is None: # 內(nèi)容為詳細(xì)地址 msg_content = r'緯度->' + x.__str__() + "經(jīng)度->" + y.__str__() else: msg_content = r"" + location # 如果消息是分享的音樂或者文章,詳細(xì)的內(nèi)容為文章的標(biāo)題或者分享的名字 elif msg['Type'] == 'Sharing': msg_content = msg['Text'] msg_share_url = msg['Url'] print(msg_share_url) face_bug = msg_content # 將信息存儲(chǔ)在字典中,每一個(gè)msg_id對(duì)應(yīng)一條消息 msg_information.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 } } ) #這個(gè)是用于監(jiān)聽是否有friend消息撤回 @itchat.msg_register(NOTE, isFriendChat=True, isGroupChat=True, isMpChat=True) def information(msg): # 這里如果這里的msg['Content']中包含消息撤回和id,就執(zhí)行下面的語句 if '撤回了一條消息' in msg['Content']: old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) # 得到消息 old_msg = msg_information.get(old_msg_id) print(old_msg) # 如果發(fā)送的是表情 if len(old_msg_id)<11: itchat.send_file(face_bug, toUserName='filehelper') # 發(fā)送撤回的提示給文件助手 else: msg_body = "【"\ + old_msg.get('msg_from') + "撤回了】\n"\ + old_msg.get("msg_type") + "消息:" + "\n"\ + old_msg.get("msg_time_rec") + "\n"\ + r"" + old_msg.get("msg_content") # 如果分享的文件被撤回了,那么就將分享的url加在msg_body中發(fā)送給文件助手 if old_msg['msg_type'] == "Sharing": msg_body += "\n就是這個(gè)鏈接>" + old_msg.get('msg_share_url') # 將撤回消息發(fā)送到文件助手 itchat.send_msg(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" % (old_msg['msg_content']) itchat.send(msg=file, toUserName='filehelper') os.remove(old_msg['msg_content']) # 刪除字典舊信息 msg_information.pop(old_msg_id) itchat.auto_login(hotReload=True) itchat.run()
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入Python全棧開發(fā)免費(fèi)公開課】
查看更多關(guān)于"Python開發(fā)資訊"的相關(guān)文章>