4

Found in service examples, a working conversation script. Thanks again to @Taj!

I feel like I am very very close to get it to work. I have done the samething on Raspberry Pi with TJBot, so I have all the accounts, and I linked all the credentials correctly including the workplace ID from Conversation tooling. I am using Unity 3D 5.5.1f1 and the latest SDK, the one that was updated 13 days ago.

I copied and pasted the sample code for conversation on SDK's github page into a brand new C# file:

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
    }
}

In the process of figuring out, I realized the onMessage function is missing a parameter (string customData), I added that with help from my friends.

Question Part II:

Thanks Taj for single handed answered all my questions! This helps me get to the core of my problem and here it is. I have updated the code above to reflect what I have in my implementation of the conversation service based on the sample code block provided on IBM's github page. https://github.com/watson-developer-cloud/unity-sdk#conversation

And this is what the Message function looks like in Watson/Scripts/Services/conversation.cs file:

/// <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);
}

When I called and it returned true, however nothing happened afterward, no callback =/.

Thanks so much for any tips! Please help!

Ghettokon
  • 63
  • 8

1 Answers1

9

The response strings are an array in the resp object.

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
  • 1,128
  • 1
  • 9
  • 23
  • Hi Taj, thanks for your response, I am giving it a shot right now. – Ghettokon Feb 09 '17 at 19:41
  • Hi Taj, do you know what is the "string customData"? both in the Message and OnMessage's parameter? – Ghettokon Feb 09 '17 at 20:28
  • The custom data is an optional string you can send with the response. It doesn't really do anything unless there is something you want to send in the request. For example you could say: `m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input, "this is my string");` and in the callback `customData` will be `"this is my string"`. – taj Feb 09 '17 at 20:52
  • Thanks Taj for explaining! When I test on the conversation tooling website with "Hi Alex", it will reply with answers from the Dialog I set up. However, when I Message "Hi Alex" in UNity 3D, nothing comes back, the callback OnMessage function wasn't even triggered. Any tips!? – Ghettokon Feb 10 '17 at 01:05
  • Sure, it can be a lot of things though. Can you post your code in the question? – taj Feb 10 '17 at 14:49
  • Hi Taj, I added "Question Part II" in the question above, again, thanks for your help! – Ghettokon Feb 10 '17 at 17:31
  • Ah, i meant can you post the code where you are invoking `Message`? Or is there a repo I can take a look at? – taj Feb 10 '17 at 18:53
  • I invoke `Message` in: `void Start () { Debug.Log("User: " + m_Input); m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input); }` I also tried moving it into a function and have an inspector button triggers it, still nothing. – Ghettokon Feb 10 '17 at 19:14