闲聊机器人API实现

基于图灵机器人实现API实现

def tulingChat(self,question):
    KEY = '8afba6fdc75544f0bebc465615da1e0b'  # change to your API KEY
    url = 'http://www.tuling123.com/openapi/api'
    req_info = question.encode('utf-8')

    query = {'key': KEY, 'info': req_info}
    headers = {'Content-type': 'text/html', 'charset': 'utf-8'}

    try:
        # 方法一、用requests模块已get方式获取内容
        r = requests.get(url, params=query, headers=headers)
        res = r.text
        result = json.loads(res).get('text').replace('
'
, '\n') except: result=choice(self.no_answer) return result

你可能感兴趣的:(learning,python技能)