zoukankan      html  css  js  c++  java
  • python 微信机器人,微信自动回复

    使用python现成的模块 itchat,可以实现,微信机器人的自动回复

    其内部原理,是模拟了web版微信的登录,然后进行消息的接收发送,并不是只能用python实现,其他后端语言都可以做到

    下面是使用例子

    #coding=utf8
    import itchat, time
    from itchat.content import *
    
    import requests
    
    KEY = '46653ff2f959407da57e9ada472ce4f0' 
    ME = '@2df4bb846987e4e401e7da93e4fa09131e4ffc38cdedbb951ae18b74721f769e'  #自己
    def get_response(msg):
        # 构造了要发送给服务器的数据
        apiUrl = 'http://www.tuling123.com/openapi/api'
        data = {
            'key'    : KEY,
            'info'   : msg,
            'userid' : 'wechat-robot',
        }
        try:
            r = requests.post(apiUrl, data=data).json()
            # 字典的get方法在字典没有'text'值的时候会返回None而不会抛出异常
            return  "机器人回复:" + r.get('text')
        # 为了防止服务器没有正常响应导致程序异常退出,这里用try-except捕获了异常
        # 如果服务器没能正常交互(返回非json或无法连接),那么就会进入下面的return
        except:
            # 将会返回一个None
            return
    
    
    @itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
    def text_reply(msg):
        #打印一下你发出的消息
        print(msg['Text'])
        # 为了保证在图灵Key出现问题的时候仍旧可以回复,这里设置一个默认回复
        defaultReply = 'I received: ' + msg['Text']
        # 如果图灵Key出现问题,那么reply将会是None
        reply = get_response(msg['Text'])
        # a or b的意思是,如果a有内容,那么返回a,否则返回b
        # 有内容一般就是指非空或者非None,你可以用`if a: print('True')`来测试
        replymsg = reply or defaultReply
        print(replymsg)
        return replymsg
    
    #普通好有回复
    @itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
    def download_files(msg):
        msg.download(msg.fileName)
        typeSymbol = {
            PICTURE: 'img',
            VIDEO: 'vid', }.get(msg.type, 'fil')
        return '@%s@%s' % (typeSymbol, msg.fileName)
    
    @itchat.msg_register(FRIENDS)
    def add_friend(msg):
    # 该操作会自动将新好友的消息录入,不需要重载通讯录
        itchat.add_friend(**msg['Text']) 
        # 加完好友后,给好友打个招呼
        itchat.send('Nice to meet you!', msg['RecommendInfo']['UserName'])
    
    #群消息回复
    @itchat.msg_register(TEXT, isGroupChat=True)
    def text_reply(msg):
        print(msg['Text'])
        print("to  ",msg['ToUserName'])
        print("from",msg['FromUserName'])
        # 为了保证在图灵Key出现问题的时候仍旧可以回复,这里设置一个默认回复
        defaultReply = 'I received: ' + msg['Text']
        # 如果图灵Key出现问题,那么reply将会是None
        reply = get_response(msg['Text'])
        # a or b的意思是,如果a有内容,那么返回a,否则返回b
        # 有内容一般就是指非空或者非None,你可以用`if a: print('True')`来测试
        replymsg = reply or defaultReply
        print(replymsg)
        return replymsg
    
    itchat.auto_login(hotReload=True)
    itchat.run(True)

     
  • 相关阅读:
    Python 以指定列宽格式化字符串
    Windows环境下QWT安装及配置
    iOS用户体验之-modal上下文
    android-调用系统的ContentPrivder获取单张图片实现剪切做头像及源代码下载
    Codeforces Round #253 (Div. 1) A Borya and Hanabi
    剑指offer 面试题9
    Memcache应用场景介绍
    [Unity-22] Coroutine协程浅析
    ZOJ 2706 Thermal Death of the Universe (线段树)
    为什么不建议用Table布局
  • 原文地址:https://www.cnblogs.com/muamaker/p/9869482.html
Copyright © 2011-2022 走看看