Ghe*_*kon 4 c# chatbot unity-game-engine watson-conversation watson
在服务示例中找到了一个有效的对话脚本.再次感谢@Taj!
我觉得我非常接近它的工作.我在使用TJBot的Raspberry Pi上做了同样的事情,所以我拥有所有帐户,并且我正确地链接了所有凭据,包括来自Conversation工具的工作区ID.我正在使用Unity 3D 5.5.1f1和最新的SDK,这是13天前更新的.
我将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
}
}
Run Code Online (Sandbox Code Playgroud)
在弄清楚的过程中,我意识到onMessage函数缺少一个参数(字符串customData),我在朋友的帮助下补充说.
问题第二部分:
谢谢Taj单手回答了我的所有问题!这有助于我找到问题的核心,就在这里.我已经更新了上面的代码,以反映我在基于IBM的github页面上提供的示例代码块实现对话服务时所拥有的内容. https://github.com/watson-developer-cloud/unity-sdk#conversation
这就是Message函数在Watson/Scripts/Services/conversation.cs文件中的样子:
/// <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);
}
Run Code Online (Sandbox Code Playgroud)
当我打电话并且它返回true时,之后没有发生任何事情,没有回调= /.
非常感谢任何提示!请帮忙!
响应字符串是resp对象中的数组.
void OnMessage(MessageResponse resp, string customData)
{
foreach (Intent mi in resp.intents)
Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);
if (resp.output != null && resp.output.text.Length > 0)
foreach (string txt in resp.output.text)
Debug.Log("Output: " + txt);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
479 次 |
| 最近记录: |