zoukankan      html  css  js  c++  java
  • 百度聊天机器人UNIT http访问

    #-*- version: Python3.0 -*
    #-*- coding: UTF-8      -*
    import urllib
    import urllib.request
    import sys
    import ssl
    import json
    
    # client_id 为官网获取的AK, client_secret 为官网获取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=eiBsX7AmncXIANL7yCGojkTA&client_secret=OnqEu2UEfSd6aPA7evwHYGg5EFRE0Tia'
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    resp_taken = response.read()
    
    #if (resp_taken):
    #    print(resp_taken)
    
    text = json.loads(resp_taken)
    #print(text["access_token"])
    
    headers = {'Content-Type':'application/json'}
    access_token = text["access_token"]
    url = 'https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token=' + access_token
    post_data = "{"log_id":"UNITTEST_10000","version":"2.0","service_id":"S20837","session_id":"","request":{"query":"你几岁了","user_id":"88888"},"dialog_state":{"contexts":{"SYS_REMEMBERED_SKILLS":["1057"]}}}"
    request = urllib.request.Request(url,data=post_data.encode('utf-8'),headers=headers)
    response = urllib.request.urlopen(request)
    content = response.read().decode("utf-8")
    
    #if content:
    #    print(content)
    
    text1 = json.loads(content)
    print(text1['result']['response_list'][0]['action_list'][0]['say'])
    

      

  • 相关阅读:
    java实现拼出漂亮的表格
    java实现金额组合
    java实现金额组合
    java实现金额组合
    java实现金额组合
    java实现Playfair 密码
    Mblog 部署手册
    配置Tomcat apr运行模式
    Duboo入门示例(Idea开发环境)
    Font-Awesome最新版完整使用教程
  • 原文地址:https://www.cnblogs.com/xuyong437/p/11365895.html
Copyright © 2011-2022 走看看