【问题标题】:IBM Watson Unity 3D SDK Conservation Service (Almost working!)IBM Watson Unity 3D SDK 保护服务(几乎可以正常工作!)
【发布时间】: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


    【解决方案1】:

    响应字符串是 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);
    }
    

    【讨论】:

    • 您好 Taj,感谢您的回复,我正在试一试。
    • 嗨 Taj,你知道什么是“字符串 customData”吗?在 Message 和 OnMessage 的参数中?
    • 自定义数据是一个可选字符串,您可以随响应一起发送。除非您想在请求中发送某些内容,否则它实际上并没有做任何事情。例如,您可以说:m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input, "this is my string"); 并且在回调中 customData 将是 "this is my string"
    • 感谢 Taj 的解释!当我在对话工具网站上使用“Hi Alex”进行测试时,它会回复我设置的对话框中的答案。但是,当我在 UNity 3D 中向“Hi Alex”发送消息时,什么也没有回来,甚至没有触发回调 OnMessage 函数。任何提示!?
    • 当然,它可以是很多东西。您可以在问题中发布您的代码吗?
    猜你喜欢
    • 2013-03-31
    • 1970-01-01
    • 2018-02-14
    • 2022-06-28
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 2018-01-15
    相关资源
    最近更新 更多