from datetime import datetime import time import requests from hashlib import md5 from urllib import parse from random import randint appkey = "D7uzDYesASNmj0So" appid = 2155912333 def getreqsign(params,appkey): params = sorted(params.items()) str_s = "" for key,value in params: if value: urldata = parse.urlencode({key:value}) str_s += urldata str_s += "&" str_s +="app_key="+appkey str_s = str_s.encode(encoding="utf-8") m = md5() m.update(str_s) str_s = m.hexdigest() str_s = str_s.upper() return str_s params = { "app_id":2155912333, "session":randint(10000,99999), "question":"你的名字", "time_stamp":time.time(), "nonce_str":time.time(), "sign":"", } params["sign"] = getreqsign(params,appkey) url = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat" result = requests.post(url,params) print(result.text)
{ "ret": 0, "msg": "ok", "data": { "session": "89437", "answer": "设计员小白,叫我有惊吓,嘻嘻,敢试不?" } }