- # python 3.5.1
- # coding:utf-8
- # 智能对话机器人
- # by:ivor bathome.net
- import urllib.request, json
-
- class tuling:
- # 初始化用户ID
- def __init__(self, userid):
- self.userid = userid
- # 请求方法,参数为请求内容info
- def acce(self,info):
- try:
- self.info = urllib.request.quote(info)
- self.data = urllib.request.urlopen("http://www.tuling123.com/openapi/api?key=4ee063f83ed5639c97d109e6ae5ae34f&info=" + self.info + "&userid=" + self.userid )
- self.data = self.data.read().decode()
- self.data = json.loads(self.data)
- except:
- print("Check out your network!")
- input("Press Enter key to contine...")
- exit(1)
- if __name__ == '__main__':
- while True:
- # 初始化类tuling的实例
- robot = tuling("test")
- ask = input("[用户]%s:" % robot.userid)
- robot.acce(ask)
- for i in robot.data:
- if i == 'code':
- continue
- print("机器人:%s\n" % robot.data[i])
复制代码
|