图灵机器人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下方的密钥 就可正常运行

 

posted @ 2018-12-28 23:29  温而新  阅读(17338)  评论(0编辑  收藏  举报