之前想搞个聊天机器人,找了半天没找到腾讯的SDK(图灵有只不过每天都有限制),只有api,所以自己写了一个凑活着用
[Python] 纯文本查看 复制代码 import hashlibimport urllib
import urllib.request
import base64
import json
import time
#接口api
url_preffix='https://api.ai.qq.com/fcgi-bin/'
def setParams(array, key, value):
array[key] = value
def genSignString(parser):
uri_str = ''
for key in sorted(parser.keys()):
if key == 'app_key':
continue
uri_str += "%s=%s&" % (key,urllib.parse.quote(str(parser[key]), safe = ''))
sign_str = uri_str + 'app_key=' + parser['app_key']
hash_md5 = hashlib.md5(sign_str.encode("latin1"))
return hash_md5.hexdigest().upper()
class AiPlat(object):
def __init__(self, app_id, app_key):
self.app_id = app_id
self.app_key = app_key
self.data = {}
def invoke(self, params):
self.url_data = urllib.parse.urlencode(params).encode(encoding='utf-8')
req = urllib.request.Request(self.url, self.url_data)
try:
rsp = urllib.request.urlopen(req)
str_rsp = rsp.read()
dict_rsp = json.loads(str_rsp.decode('utf-8'))
return dict_rsp
except urllib.error.URLError as e:
dict_error = {}
if hasattr(e, "code"):
dict_error = {}
dict_error['ret'] = -1
dict_error['httpcode'] = e.code
dict_error['msg'] = "sdk http post err"
return dict_error
if hasattr(e,"reason"):
dict_error['msg'] = 'sdk http post err'
dict_error['httpcode'] = -1
dict_error['ret'] = -1
return dict_error
else:
dict_error = {}
dict_error['ret'] = -1
dict_error['httpcode'] = -1
dict_error['msg'] = "system error"
return dict_error
def getNlpTextChat(self, session, question):
self.url = url_preffix + 'nlp/nlp_textchat'
setParams(self.data, 'app_id', self.app_id)
setParams(self.data, 'app_key', self.app_key)
setParams(self.data, 'time_stamp', int(time.time()))
setParams(self.data, 'nonce_str', int(time.time()))
setParams(self.data, 'session', session)
setParams(self.data, 'question', question)
sign_str = genSignString(self.data)
setParams(self.data, 'sign', sign_str)
return self.invoke(self.data)
使用
[Python] 纯文本查看 复制代码 import optparse
import time
import apiutil#这里我上端代码独立生成一个文件“apiutil.py",所以要导入一下
import json
app_key = 'XXXXXXXXXX'
app_id = '110XXXX996'
questionS= '在吗?'
def anso(questionS):
str_question = questionS
session = 10000
ai_obj = apiutil.AiPlat(app_id, app_key)
rsp = ai_obj.getNlpTextChat(session,str_question)
if rsp['ret'] == 0:
print('............................................................')
ask = (rsp['data'])['answer']
print(ask)
else:
print(json.dumps(rsp, ensure_ascii=False, sort_keys=False, indent=4))
if __name__ == '__main__':
anso(questionS)
原文地址:https://blog.csdn.net/Rayn_Zhu/article/details/81093989
最后 证明下原创
|