心血来潮,后续有时间再修改修改,转载,原文找不到了
from importlib import reload
import itchat
from itchat.content import TEXT
from itchat.content import *
import sys
import time
import re
reload(sys)
# sys.setdefaultencoding('utf8')
import os
msg_information = {}
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
msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 接受消息的时间
print(msg)
msg_from = itchat.search_friends(userName=msg['FromUserName'])['NickName'] # 在好友列表中查询发送信息的好友昵称
msg_time = msg['CreateTime'] # 信息发送的时间
msg_id = msg['MsgId'] # 每条信息的id
msg_content = None # 储存信息的内容
msg_share_url = 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'] # 内容就是他们的文件名
msg['Text'](str(msg_content)) # 下载文件
# print msg_content
elif msg['Type'] == 'Card': # 如果消息是推荐的名片
msg_content = msg['RecommendInfo']['NickName'] + '的名片' # 内容就是推荐人的昵称和性别
if msg['RecommendInfo']['Sex'] == 1:
msg_content += '性别为男'
else:
msg_content += '性别为女'
print
msg_cont