调用图灵机器人api实例:
class RobotHandler(WebSocketHandler):def open(self):# print("WebSocket opened",self.request.remote_ip)robot_set.add(self)# 获取websocket的发过来的信息def on_message(self, message):url='http://openapi.tuling123.com/openapi/api/v2'response = requests.post(url,json={"perception": {"inputText": {"text": message}},"userInfo": {"apiKey": "176c3046933c4c3eac77777770","userId": "4777770"}})print(response)print(response.text)data = json.loads(response.text)print(type(response.text))print('data---------',data)print(type(data))result_list = data['results']print()print('result_list========',result_list)relay=result_list[0]['values']['text']print()print(relay)self.write_message('小丽说:{}'.format(relay))self.ws_connection.write_message(message=message)# # 向外发送消息# def write_message(self,message,binary=False):# return self.ws_connection.write_message(message=message)#当连接关闭时调用def on_close(self):print("WebSocket closed")robot_set.remove(self)
print(response)
<Response [200]>
print(response.text)
{"intent":{"code":4003},"results":[{"groupType":0,"resultType":"text","values":{"text":"请求次数超限制!"}}]}
data = json.loads(response.text)
print(type(response.text))
<class 'str'>
print('data---------',data)
data--------- {'intent': {'code': 4003}, 'results': [{'groupType': 0, 'resultType': 'text', 'values': {'text': '请求次数超限制!'}}]}
print(type(data))
<class 'dict'>