【发布时间】:2017-06-26 16:07:23
【问题描述】:
在服务示例中找到,一个有效的对话脚本。再次感谢@Taj!
我觉得我非常接近让它发挥作用。我用 TJBot 在 Raspberry Pi 上做了同样的事情,所以我拥有所有帐户,并且我正确链接了所有凭据,包括来自对话工具的工作场所 ID。我正在使用 Unity 3D 5.5.1f1 和最新的 SDK,即 13 天前更新的 SDK。
我将 SDK 的 github 页面上的对话示例代码复制并粘贴到一个全新的 C# 文件中:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
public class test : MonoBehaviour {
private Conversation m_Conversation = new Conversation();
private string m_WorkspaceID = "my ID on the conversation tooling site";
private string m_Input = "Hi Alex";
// Use this for initialization
void Start () {
Debug.Log("User: " + m_Input);
m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input);
}
// Update is called once per frame
void Update () {
}
void OnMessage(MessageResponse resp, string customData)
{
//Parsing resp here
//foreach (Intent mi in resp.intents)
//Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);
//resp.output.text causes an error
}
}
在解决的过程中,我意识到onMessage函数缺少一个参数(字符串customData),我在朋友的帮助下添加了这个参数。
问题第二部分:
感谢 Taj 单枪匹马回答了我所有的问题!这有助于我找到问题的核心,就是这样。我已经根据 IBM 的 github 页面上提供的示例代码块更新了上面的代码,以反映我在实现对话服务中所拥有的内容。 https://github.com/watson-developer-cloud/unity-sdk#conversation
这就是 Watson/Scripts/Services/conversation.cs 文件中 Message 函数的样子:
/// <summary>
/// Message the specified workspaceId, input and callback.
/// </summary>
/// <param name="workspaceID">Workspace identifier.</param>
/// <param name="input">Input.</param>
/// <param name="callback">Callback.</param>
/// <param name="customData">Custom data.</param>
public bool Message(OnMessage callback, string workspaceID, string input, string customData = default(string))
{
if (string.IsNullOrEmpty(workspaceID))
throw new ArgumentNullException("workspaceId");
if (string.IsNullOrEmpty(input))
throw new ArgumentNullException("input");
if (callback == null)
throw new ArgumentNullException("callback");
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_MESSAGE);
if (connector == null)
return false;
string reqJson = "{{\"input\": {{\"text\": \"{0}\"}}}}";
string reqString = string.Format(reqJson, input);
MessageReq req = new MessageReq();
req.Callback = callback;
req.Headers["Content-Type"] = "application/json";
req.Headers["Accept"] = "application/json";
req.Parameters["version"] = Version.VERSION;
req.Function = "/" + workspaceID + "/message";
req.Data = customData;
req.Send = Encoding.UTF8.GetBytes(reqString);
req.OnResponse = MessageResp;
return connector.Send(req);
}
当我调用它并返回 true,但之后什么也没发生,没有回调 =/。
非常感谢您提供的任何提示!请帮忙!
【问题讨论】:
标签: c# unity3d chatbot watson-conversation watson