定制化本身的itchat

上篇文章很详实,能够实现一个低级的微信自动回复机器人,据说还有用图灵的,那就变成高级机器人了。git

初级目标: 自动回复好友的信息。微信

 
 
#-*- coding:utf-8 -*-
#微信的库
import itchat
#导入想要处理的content内容
from itchat.content import *

import re
import time
#这个装饰器是个类装饰器吧,貌似功能很强大,括号里的内容定义了你这个函数想处理的信息类型,msg即是你收到的微信信息,看样子是个字典。
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, NOTE, SHARING, RECORDING, ATTACHMENT, VIDEO])
def text_reply(msg):
    #调试用的,看看不一样的信息都长啥样
    print msg
    #对于不一样类型的信息,咱们要记录不一样的内容来回复,
    #普通文本
    if msg['Type'] == 'Text':
        reply_content = msg['Text']
    
    #图片,记录图片的名字,FileName这个键值能够表示图片,音频视频的名字
    elif msg['Type'] == 'Picture':
        reply_content = r"Picture: " + msg['FileName']
    #若是接收到的是个名片的话,记下推荐信息和昵称
    elif msg['Type'] == 'Card':
        reply_content = r" " + msg['RecommendInfo']['NickName'] + r" 's card"
    #若是收到的是一个共享的地址的话,用正则分别尝试去匹配经纬度和位置名称
    elif msg['Type'] == 'Map':
        x, y, location = re.search("<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1,
                                                                                                                    2,
                                                                                                                    3)
        if location is None:
            reply_content = r"Location: longititude->" + x.__str__() + u" jingdu->" + y.__str__()
        else:
            reply_content = r"location: " + location
    #后面这些还没用过,直接处理了,之后有错再说
    elif msg['Type'] == 'Note':
        reply_content = r"Note"
    elif msg['Type'] == 'Sharing':
        reply_content = r"Sharing"
    elif msg['Type'] == 'Recording':
        reply_content = r"Voice"
    elif msg['Type'] == 'Attachment':
        reply_content = r"File: " + msg['FileName']
    elif msg['Type'] == 'Video':
        reply_content = r"Video: " + msg['FileName']
    else:
        reply_content = r"Message"
    #获取信息来源
    friend = itchat.search_friends(userName=msg['FromUserName'])
    #在itchat助手里进行记录
    itchat.send(r"Friend:%s -- %s    "
                r"Time:%s    "
                r" Message:%s" % (friend['NickName'], friend['RemarkName'], time.ctime(), reply_content),
                toUserName='filehelper')
    #回复给信息来源,表示朕已经收到你的消息了,你能够退下了
    itchat.send(r"I received your news (%s) %s.Reply later.--From itchat(Python)" % (time.ctime(), reply_content),
                toUserName=msg['FromUserName'])
#懒得自定义登陆函数了,用自带的函数
itchat.auto_login(enableCmdQR=-2,hotReload=True)
itchat.send(r'Hello my friend!',toUserName='filehelper')
#运行起来,等待接受信息
itchat.run()
 
 

 

傻瓜式的照搬例子就能够了,代码几乎同样。高级功能有待后续实现
相关文章
相关标签/搜索