微信聊天机器人

申请图灵机器人

  到官网注册下就可以免费申请了,我这里申请了两个。

 

安装itchat

  itchat是LittleCoder大神写的微信个人号接口。

  

pip install itchat

  

 

python调用图灵机器人代码:

 

  实现二维码登录。

#coding=utf8
#!/usr/bin/python3
import requests
import itchat

KEY = '你的机器人的APIkey'

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()
        return r.get('text')
    except:
        return

@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
    defaultReply = 'I received: ' + msg['Text']
    reply = get_response(msg['Text'])
    return reply or defaultReply

itchat.auto_login(hotReload=True)
itchat.run()

  

效果:

 

 

 

 

   

posted @ 2017-08-15 00:16  zuorn  阅读(345)  评论(0编辑  收藏  举报