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() # 课后作业:把未完成的完成