zoukankan      html  css  js  c++  java
  • 定制化自己的itchat

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

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

    
    
    #-*- 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()
    
    
    
    傻瓜式的照搬例子就可以了,代码几乎一样。
    高级功能有待后续实现
  • 相关阅读:
    <<一线架构师实践指南>>读书笔记之二PA阶段
    【读书笔记】简约至上交互设计四策略第4章 删除
    大数据量简单数据查询设计思考
    识别项目干系人
    【读书笔记】简约至上交互设计四策略第3章 简约四策略
    【读书笔记】简约至上交互设计四策略第2章 明确认识
    【读书笔记】简约至上交互设计四策略第1章 话说简单
    采购管理计划
    项目管理整体的一些基本概念1
    【读书笔记】简约至上交互设计四策略第5章 组织
  • 原文地址:https://www.cnblogs.com/AlwaysT-Mac/p/6527200.html
Copyright © 2011-2022 走看看