圖靈機器人API接口


調用圖靈API接口實現人機交互

流程一: 注冊

圖靈機器人官網: http://www.tuling123.com/

第一步: 先注冊, 然后創建機器人, 拿到一個32位的key

編碼方式

UTF-8(調用圖靈API的各個環節的編碼方式均為UTF-8)

接口地址

http://openapi.tuling123.com/openapi/api/v2

請求方式

HTTP POST

請求參數

請求參數格式為 json

{
    "reqType":0,
    "perception": {
        "inputText": {
            "text": "附近的酒店"
        },
        "inputImage": {
            "url": "imageUrl"
        },
        "selfInfo": {
            "location": {
                "city": "北京",
                "province": "北京",
                "street": "信息路"
            }
        }
    },
    "userInfo": {
        "apiKey": "",
        "userId": ""
    }
}
請求示例

輸出參數

{
    "intent": {
        "code": 10005,
        "intentName": "",
        "actionName": "",
        "parameters": {
            "nearby_place": "酒店"
        }
    },
    "results": [
        {
             "groupType": 1,
            "resultType": "url",
            "values": {
                "url": "http://m.elong.com/hotel/0101/nlist/#indate=2016-12-10&outdate=2016-12-11&keywords=%E4%BF%A1%E6%81%AF%E8%B7%AF"
            }
        },
        {
             "groupType": 1,
            "resultType": "text",
            "values": {
                "text": "親,已幫你找到相關酒店信息"
            }
        }
    ]
}
輸出示例

異常返回碼

{
    'intent':
        {
            'code':5000
        }
}
異常返回格式

 

代碼示例

import json
import urllib.request
while 1:
    try:
        api_url = "http://openapi.tuling123.com/openapi/api/v2"
        text_input = input('我:')
        if text_input == 'exit':
            break
        req = {
            "reqType": 0,  # 輸入類型 0-文本, 1-圖片, 2-音頻
            "perception":  # 信息參數
            {
                "inputText":  # 文本信息
                {
                    "text": text_input
                },

                "selfInfo":  # 用戶參數
                {
                    "location":
                    {
                        "city": "深圳",  # 所在城市
                        "province": "廣東",  # 省份
                        "street": "紅花嶺"  # 街道
                    }
                }
            },
            "userInfo":
            {
                "apiKey": "347b39ee228b4b109dae7270cc08d3c8",  # 改為自己申請的key
                "userId": "0001"  # 用戶唯一標識(隨便填, 非密鑰)
            }
        }
        # print(req)
        # 將字典格式的req編碼為utf8
        req = json.dumps(req).encode('utf8')
        # print(req)
        http_post = urllib.request.Request(api_url, data=req, headers={'content-type': 'application/json'})
        response = urllib.request.urlopen(http_post)
        response_str = response.read().decode('utf8')
        # print(response_str)
        response_dic = json.loads(response_str)
        # print(response_dic)
        intent_code = response_dic['intent']['code']
        results_text = response_dic['results'][0]['values']['text']
        print('機器人1號:', results_text)
        # print('code:' + str(intent_code))
    except KeyError:
        print('出錯啦~~, 下次別問這樣的問題了')

 

注意: 如果返回4001(加密方式錯誤), 請關閉你獲取apikey下方的密鑰 就可正常運行

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM