关于response格式转换

调用图灵机器人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)           

 


print(response.text)

{"intent":{"code":4003},"results":[{"groupType":0,"resultType":"text","values":{"text":"请求次数超限制!"}}]}

 

data = json.loads(response.text) 
print(type(response.text))             

print('data---------',data)
data--------- {'intent': {'code': 4003}, 'results': [{'groupType': 0, 'resultType': 'text', 'values': {'text': '请求次数超限制!'}}]}

 


print(type(data))

 

result_list = data['results']
print('result_list========',result_list)

result_list======== [{'groupType': 0, 'resultType': 'text', 'values': {'text': '请求次数超限制!'}}]


relay=result_list[0]['values']['text']
print(relay)

请求次数超限制!


self.write_message('小丽说:{}'.format(relay))
self.ws_connection.write_message(message=message)

 

你可能感兴趣的:(Python之路)