图灵机器人在Unity中的接入

  本文讲的是在Unity中如何接入图灵机器人,后面我会再写一篇博客,在Unity中实现和智能机器人的对话功能。
  百度搜索图灵机器人,注册或者登陆一个,设置自己的机器人。然后点击帮助中心–>接入文档–>API接入文档。
在这里插入图片描述
  阅读文档中的内容,perception和userInfo是必须的,其他的可以删除掉,删除后如下,可以在BeJson中实时校验:

{
    "perception": {
        "inputText": {
            "text": "附近的酒店"
        },
        "inputImage": {
            "url": "imageUrl"
        }
    },
    "userInfo": {
        "apiKey": "",
        "userId": ""
    }
}

  然后在Unity中引入LitJson,新建一个脚本。新建的脚本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.Text;
public class TuLingTest : MonoBehaviour {
    public string url = "http://openapi.tuling123.com/openapi/api/v2";
    public string apiKey = "d2cf7b59357645d4a333517a197a8a9c";
    private void Start()
    {
        HttpPostFunc("你叫什么名字?");

    }
    public void HttpPostFunc(string message)
    {
        StartCoroutine(HttpPost(message));
    }

    IEnumerator HttpPost(string message)
    {
        //JsonData可以表示JsonObject{},也可以表示JsonArry[]
        JsonData request = new JsonData();
        //perception
        request["perception"] = new JsonData();
        request["perception"]["inputText"] = new JsonData();
        request["perception"]["inputText"]["text"] = message;
        //userInfo
        request["userInfo"] = new JsonData();
        request["userInfo"]["apiKey"] = apiKey;
        request["userInfo"]["userId"] = "1512267557";
                                //JsonMapper.ToJson(request)
                                //将Json对象转为Json字符串,直接ToString容易出错
                                //将Json字符串转为字节数组
                                //进行一个网络推送
        WWW post = new WWW(url,Encoding.UTF8.GetBytes(JsonMapper.ToJson(request)));
        yield return post;
        Debug.Log(post.text);
    }


}

  运行Unity,debug出来得到的是一个标准的Json字符串

{
	"emotion": {
		"robotEmotion": {
			"a": 0,
			"d": 0,
			"emotionId": 0,
			"p": 0
		},
		"userEmotion": {
			"a": 0,
			"d": 0,
			"emotionId": 0,
			"p": 0
		}
	},
	"intent": {
		"actionName": "",
		"code": 10004,
		"intentName": ""
	},
	"results": [{
		"groupType": 1,
		"resultType": "text",
		"values": {
			"text": "在下木小星,请多多指教。"
		}
	}]
}

  这里我们的表情和事件就不用了,留下results这个jsonArry数组,后面两个我复制的两个多余的是为了看得清楚一点,这个results数组的结构,然后把这后面两个多余的删除就行了:

{
	"results": [{
		"groupType": 1,
		"resultType": "text",
		"values": {
			"text": "在下木小星,请多多指教。"
		}
	}, {
		"groupType": 1,
		"resultType": "text",
		"values": {
			"text": "在下木小星,请多多指教。"
		}
	}, {
		"groupType": 1,
		"resultType": "text",
		"values": {
			"text": "在下木小星,请多多指教。"
		}
	}]
}

  如上Json字符串,我们需要的是它这个results数组里面的第0个元素的values里面的text这个字符串,然后进行下一步:
  在之前的代码后面加上下面这三行代码就可以得到这个text了,如下:
在这里插入图片描述
在这里插入图片描述
  更多内容请在我的博客中查看。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值