6

智能问答——小i机器人的pythonAPI调用

 3 years ago
source link: https://cjh0613.com/20200521xiaoiAPI.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

智能问答——小i机器人的pythonAPI调用

发表2020-05-21更新2020-08-01字数198预计阅读时长2分阅读次数89

首先,登陆http://open.xiaoi.com,左侧开发者中心,客服API,得到KeySecret

调用示例:
仅返回字符:

print(cjhGetResponse("你好","0"))

返回xml:

print(cjhGetResponse("你好","1"))
import urllib
import random
import hashlib

import requests

import re, json

#两个函数:
def getNonce():
strs = ''
for i in range(18):
strs += (str(random.randint(0, 15)))
return strs

def cjhGetResponse(question,t):
appKey = '你的Key'
appSecret = '你的Secret'
HA1 = hashlib.sha1(
':'.join([appKey, "xiaoi.com", appSecret]).encode("utf8")).hexdigest()
HA2 = hashlib.sha1(u"GET:/ask.do".encode("utf8")).hexdigest()
nonce = getNonce() # ':'.join([HA1, nonce, HA2]).encode("utf8")
sig = hashlib.sha1(
':'.join([HA1, nonce, HA2]).encode("utf8")).hexdigest()

headers = {
"X-Auth": "app_key=\"{0}\",nonce=\"{1}\",signature=\"{2}\"".format(
appKey, nonce, sig)
}
post_data = {
"question": question,
"userId": 'someone',
"type": t,
"platform": "web"
}
post_data = urllib.parse.urlencode(post_data)
url = "http://robot.open.xiaoi.com/ask.do?"+post_data
request = urllib.request.Request(url, None, headers)
request.add_header('Content-Type', 'text/html; charset=UTF-8')
response = urllib.request.urlopen(request)
return str(response.read(), 'utf8')

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK