【Python3.6】将个人微信改造成机器人自动对话

版权声明:欢迎转载,注明出处。 https://blog.csdn.net/weixin_37637399/article/details/78015781

源码:http://download.csdn.net/download/weixin_37637399/9983454
python版本:Python3.6

前期准备:注册一个图灵机器人:http://www.tuling123.com,得到一个APIkey,如下图。
这里写图片描述

思路:使用itchat库调用微信接口,将图灵机器人API作为微信机器人的自动对话。

import requests
import itchat

#填写自己刚才注册的图灵机器人的APIkey
KEY = '自己的图灵机器人的APIkey'

#根据图灵机器人API的规定发送数据
def tuling_reponse(msg):
    url = 'http://www.tuling123.com/openapi/api'
    data = {
            'key' : KEY,
            'info' : msg,
            'userid' : 'xsl' #这个随便填,作为机器人识别的标志,因为一个机器人可以供多个用户使用
    }
    #rep返回的数据格式大概是这样{'code': 100000, 'text': '你好'}
    rep = requests.post(url,data).json()
    return rep.get('text')

#定义消息处理函数
#itchat.content.TEXT代表只处理文本信息,图片、语音都可以处理,自己百度一下就好
@itchat.msg_register(itchat.content.TEXT)
def get_reponse(msg):
    reponse=tuling_reponse(msg['Text'])
    return reponse

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

猜你喜欢

转载自blog.csdn.net/weixin_37637399/article/details/78015781