# coding:utf-8
import itchat
from itchat.content import text
from itchat.content import *
import sys
import time
import re
import importlib
import os
importlib.reload(sys)
msg_information = {}
face_bug = none # 針對表情包的內容
@itchat.msg_register([text, picture, friends, card, map, sharing, recording, attachment, video], isfriendchat=true,
isgroupchat=true, ismpchat=true)
def handle_receive_msg(msg):
global face_bug
msg_time_rec = time.strftime("%y-%m-%d %h:%m:%s", time.localtime()) # 接受訊息的時間
msg_from = itchat.search_friends(username=msg['fromusername'])['nickname'] # 在好友列表中查詢傳送資訊的好友暱稱
msg_time = msg['createtime'] # 資訊傳送的時間
msg_id = msg['msgid'] # 每條資訊的id
msg_content = none # 儲存資訊的內容
print
msg['type']
print
msg['msgid']
if msg['type'] == 'text' or msg['type'] == 'friends': # 如果傳送的訊息是文字或者好友推薦
msg_content = msg['text']
print
msg_content
elif msg['type'] == "attachment" or msg['type'] == "video" \
or msg['type'] == 'picture' \
or msg['type'] == 'recording':
msg_content = msg['filename'] # 內容就是他們的檔名
# print msg_content
elif msg['type'] == 'card': # 如果訊息是推薦的名片
msg_content = msg['recommendinfo']['nickname'] + '的名片' # 內容就是推薦人的暱稱和性別
if msg['recommendinfo']['***'] == 1:
msg_content += '性別為男'
else:
msg_content += '性別為女'
print
msg_content
elif msg['type'] == 'map': # 如果訊息為分享的位置資訊
x, y, location = re.search(
"" + x.__str__() + " 經度->" + y.__str__() # 內容為詳細的位址
else:
msg_content = r"" + location
elif msg['type'] == 'sharing': # 如果訊息為分享的**或者文章,詳細的內容為文章的標題或者是分享的名字
msg_content = msg['text']
msg_share_url = msg['url'] # 記錄分享的url
print
msg_share_url
face_bug = msg_content
##將資訊儲存在字典中,每乙個msg_id對應一條資訊
msg_information.update(
})
##這個是用於監聽是否有訊息撤回
@itchat.msg_register(note, isfriendchat=true, isgroupchat=true, ismpchat=true)
def information(msg):
# 這裡如果這裡的msg['content']中包含訊息撤回和id,就執行下面的語句
if '撤回了一條訊息' in msg['content']:
old_msg_id = re.search("\(.*?)\<\/msgid\>", msg['content']).group(1) # 在返回的content查詢撤回的訊息的id
old_msg = msg_information.get(old_msg_id) # 得到訊息
print
old_msg
if len(old_msg_id) < 11: # 如果傳送的是表情包
itchat.send_file(face_bug, tousername='filehelper')
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')
# 如果是分享的檔案被撤回了,那麼就將分享的url加在msg_body中傳送給檔案助手
if old_msg['msg_type'] == "sharing":
msg_body += "\n就是這個鏈結➣ " + old_msg.get('msg_share_url')
# 將撤回訊息傳送到檔案助手
itchat.send_msg(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' % (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()
itchat監聽微信撤回訊息
import itchat from itchat.content import import remsg infomation 監聽傳送訊息 itchat.msg register text defhandle receive msg msg print msg msg from itchat.s...
基於itchat和pil獲取微信好友頭像並進行拼接
user friends username image itchat.get head img user import itcht itchat.auto login hotreload true friends itchat.get friends for friends in friends u...
微信,找回好友 群聊使用者撤回的訊息
coding utf 8 import itchat from itchat.content import text from itchat.content import import sys import time import re import os msg information face ...