智能问答——小i机器人的pythonAPI调用
生活随笔
收集整理的這篇文章主要介紹了
智能问答——小i机器人的pythonAPI调用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文作者:?峽州仙士
原文鏈接:?https://cjh0613.github.io/blog/20200521xiaoiAPI.html
聲明:?如文章有更新,將首先在本網站(峽州仙士之頁)發布!(可百度搜到)| 本網站(峽州仙士之頁)所有文章除特別聲明外,均采用?BY-NC-SA?許可協議。轉載請注明出處!
首先,登陸http://open.xiaoi.com,左側開發者中心,客服API,得到Key和Secret
調用示例:
僅返回字符:
返回xml:
print(cjhGetResponse("你好","1"))函數:
import urllib import random import hashlibimport requestsimport re, json#兩個函數: def getNonce():strs = ''for i in range(18):strs += (str(random.randint(0, 15)))return strsdef 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_datarequest = 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')總結
以上是生活随笔為你收集整理的智能问答——小i机器人的pythonAPI调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pointofix 1.7 Portab
- 下一篇: 飞凌嵌入式丨NXP MFGTool软件烧