import itchat from itchat.content import * import re msg_infomation = {} # 監聽發送消息 @itchat.msg_register([TEXT]) def handle_receive_msg(msg): print(msg) msg_from = itchat.search_friends(userName=msg["FromUserName"])["NickName"] print(msg_from) # 獲取元素中須要的信息 msg_time = msg["CreateTime"] msg_id = msg["MsgId"] msg_content = msg["Text"] # 把消息放入全局字典當中 msg_infomation.update( {msg_id:{ "msg_from":msg_from, "msg_time":msg_time, "msg_content":msg_content } } ) print(msg_infomation) # 監聽撤回消息 @itchat.msg_register([NOTE]) def infomation(msg): print(msg) content = msg.get("Content") print(content) if "撤回了一條消息" in content: print(1, content) old_msg_id = re.search("<msgid>(.*?)</msgid>", msg["Content"]).group(1) print(old_msg_id) print(msg_infomation.get(old_msg_id)) # 微信登陸操做 itchat.auto_login(hotReload=True) # 保證程序一直運行 itchat.run() # 課後做業:把未完成的完成