腾讯ai开放平台-智能闲聊开发日志

发布于 2020-05-23  375 次阅读


注册和七七八八的流程省略了= =,直接上码

因为大多数人用的是p3,但是服务器脚本有时候又是p2,于是先写了份P2的,复制粘贴就能用(大概吧)

Python2.x:

Md5运算-md5sign

import hashlib

import time

import random

import string
from urllib import quote
import sys

reload(sys)

sys.setdefaultencoding('utf8')


#from urllib.parse import quote(放弃的py3语法)



def curlmd5(src):

    m = hashlib.md5(src.encode('UTF-8'))

    # 转换成大写

    return m.hexdigest().upper()



def get_params(plus_item):

    # 请求时间戳(秒级),用于防止请求重放

    t = time.time()

    time_stamp=str(int(t))

    # 请求随机字符串,保证....额,忘了

    nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 10))

    

    app_id = 'yourid'

    app_key = 'yourkey'

    params = {'app_id':app_id,

              'question':plus_item,

              'time_stamp':time_stamp,

              'nonce_str':nonce_str,

              'session':'10000'

             }

    sign_before = ''

    # 对key排序再拼接

    for key in sorted(params):

      
        
        sign_before += '{}={}&'.format(key,quote(params[key] , safe=''))

    # 将应用密钥以app_key为键名,拼接到字符串sign_before末尾

    sign_before += 'app_key={}'.format(app_key)

    # 对字符串sign_before进行MD5运算,得到接口请求签名
    
    #(不想写注释了= =......)
    sign = curlmd5(sign_before)

    params['sign'] = sign

    return params

主程序:main:

# -*- coding: utf-8  -*-
import requests


import md5sign
   

print("Tip:按键入q即可退出~")
print("由于python的utf-8的编码问题会出现无法修复的bug,如果退出,请键入python main.py重启")


def get_content(plus_item):
    

    url = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat"

    

    plus_item = plus_item.encode('utf-8')

    payload = md5sign.get_params(plus_item)

   

    r = requests.post(url, data=payload)

    return r.json()["data"]["answer"]
    
    


if __name__ == '__main__':

    while True:
       
        chrash=comment = raw_input('-')
        
        if comment == 'q':
            print("robot:拜拜,别忘了下次找我聊天哦")
            
            break
        answer = get_content(comment)
        if answer == '':
            answer = get_content(comment)
            if answer == '':
                print("哎呀,出...出错了(ノ=Д=)ノ┻━┻ ")
                continue
            print ('robot:' + answer)
            #print("哎呀,出...出错了(ノ=Д=)ノ┻━┻ ")
            #continue
        else:
            print ('robot:' + answer)

Python3x:

MD5:

# -*- coding: utf-8 -*-
import hashlib

import time

import random

import string
from urllib.parse import quote
import sys
def curlmd5(src):

    m = hashlib.md5(src.encode('UTF-8'))

    # 转换成大写

    return m.hexdigest().upper()



def get_params(plus_item):

    # 请求时间戳(秒级),用于防止请求重放

    t = time.time()

    time_stamp=str(int(t))

    # 请求随机字符串,保证....额,忘了

    nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 10))

    

    app_id = 'yourid'

    app_key = 'yourkey'

    params = {'app_id':app_id,

              'question':plus_item,

              'time_stamp':time_stamp,

              'nonce_str':nonce_str,

              'session':'10000'

             }

    sign_before = ''

    # 对key排序再拼接

    for key in sorted(params):

      
        
        sign_before += '{}={}&'.format(key,quote(params[key] , safe=''))

    # 将应用密钥以app_key为键名,拼接到字符串sign_before末尾

    sign_before += 'app_key={}'.format(app_key)

    # 对字符串sign_before进行MD5运算,得到接口请求签名
    
    #(不想写注释了= =......)
    sign = curlmd5(sign_before)

    params['sign'] = sign

    return params
# -*- coding: utf-8 GB2312 -*-
import requests


import md5sign
   

print("Tip:按键入q即可退出~")
print("由于python的utf-8的编码问题会出现无法修复的bug,如果退出,请键入python main.py重启")


def get_content(plus_item):
    

    url = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat"

    

    plus_item = plus_item.encode('utf-8')

    payload = md5sign.get_params(plus_item)

   

    r = requests.post(url, data=payload)

    return r.json()["data"]["answer"]
    
    


if __name__ == '__main__':

    while True:
       
       comment = input('-')
        
        if comment == 'q':
            print("robot:拜拜,别忘了下次找我聊天哦")
            
            break
        answer = get_content(comment)
        if answer == '':
            answer = get_content(comment)
            if answer == '':
                print("哎呀,出...出错了(ノ=Д=)ノ┻━┻ ")
                continue
            print ('robot:' + answer)
            #print("哎呀,出...出错了(ノ=Д=)ノ┻━┻ ")
            #continue
        else:
            print ('robot:' + answer)


没有尽头!