UNIT2.0 Python3调用示例
林夕画晴 发布于2018-07 浏览:2911 回复:1
0
收藏

官网给出的调用示例Python2的代码,Py3直接使用会报错,经过自己的摸索,使用Py3改写成功了,分享给大家(*^▽^*)

官网Python2源代码链接:https://ai.baidu.com/docs#/UNIT-v2-API/top

# -*- coding: utf-8 -*-
import urllib.request as ul_re
import json
url = 'https://aip.baidubce.com/rpc/2.0/unit/bot/chat?access_token=' + 【你的access_token】
post_data  = {
	"bot_session": "",
	"log_id": "7758521",
	"request": {
		"bernard_level": 0,
		"client_session": "{\"client_results\":\"\", \"candidate_options\":[]}",
		"query": "【你的输入语句】",
		"query_info": {
			"asr_candidates": [],
			"source": "KEYBOARD",
			"type": "TEXT"
		},
		"updates": "",
		"user_id": "88888"
	},
	"bot_id": 【你的BOTID】,
	"version": "2.0"
}
encoded_data = json.dumps(post_data).encode('utf-8')
headers = {'Content-Type': 'application/json'}

request = ul_re.Request(url, data=encoded_data, headers=headers)
response = ul_re.urlopen(request)
content = response.read()
result = str(content,'utf-8')
print(result)
收藏
点赞
0
个赞
共1条回复 最后由用户已被禁言回复于2022-04
#2choleraa回复于2018-07

棒~~~~

感觉也可以尝试使用urllib3呀.

0
TOP
切换版块