3

I am trying to running IBM Watson conversation service in unity and following here, code snippet

private Conversation m_Conversation = new Conversation();
    private string m_WrokspaceID = "xyz";
    private string m_input = "help";


    // Use this for initialization
    void Start () {
        Debug.Log("user : " + m_input);
        m_Conversation.Message(OnMessage, m_WrokspaceID, m_input);
    }

    void OnMessage(MessageResponse resp, string customData) {
        foreach (Intent mi in resp.intents)
        {
            Debug.Log("intent : " + mi.intent + ", confidence :" + mi.confidence);
        }

        Debug.Log("response :" + resp.output.text);
    }

But i am getting this error

cannot convert from 'method group' to 'conversation.onMessage'

What i am doing wrong? The code snippet i get from watson official github repo.

Object returning as answer suggested: enter image description here

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186

2 Answers2

3

According to line 32 in the source code of Conversation, the delegate was changed to:

public delegate void OnMessage(object resp, string customData);

You'll have to change your OnMessage method to reflect that:

void OnMessage(object resp, string customData) {
    // ...
}
haim770
  • 48,394
  • 7
  • 105
  • 133
  • but if i make it object then i will be unable to use MessageResponse class properties. – Muhammad Faizan Khan Aug 30 '17 at 06:35
  • The common approach in such cases (as can be found virtually anywhere with event handlers) is to explicitly cast the `object` to your `MessageResponse`. See https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclick(v=vs.110).aspx for example. – haim770 Aug 30 '17 at 06:54
  • Yes i have cast reponse object but it failed InvalidCastException: Cannot cast from source type to destination type. – Muhammad Faizan Khan Aug 30 '17 at 06:56
  • What is the actual type of `resp` when you debug it? – haim770 Aug 30 '17 at 07:13
  • I didn't check it yet let me debug it. Thanks, i will let you know – Muhammad Faizan Khan Aug 30 '17 at 07:14
  • Judging by https://github.com/watson-developer-cloud/unity-sdk/blob/fb81974bb6e1905c42534ca0a1d70046c2b4c871/Scripts/Services/Conversation/Conversation.cs#L172, it's seems like actual type being sent is indeed some `object` that is a result of JSON decoding. – haim770 Aug 30 '17 at 07:17
  • it is list and dictionary, i have updated question with pic – Muhammad Faizan Khan Aug 30 '17 at 09:13
3

You can cast the response as a dictionary and try to get the value from there. Using a generic object instead of a static data model, you are able to pass more through the response.

private void OnMessage(object resp, string customData)
{
    Dictionary<string, object> respDict = resp as Dictionary<string, object>;
    object intents;
    respDict.TryGetValue("intents", out intents);

    foreach(var intentObj in (intents as List<object>))
    {
        Dictionary<string, object> intentDict = intentObj as Dictionary<string, object>;

        object intentString;
        intentDict.TryGetValue("intent", out intentString);

        object confidenceString;
        intentDict.TryGetValue("confidence", out confidenceString);

        Log.Debug("ExampleConversation", "intent: {0} | confidence {1}", intentString.ToString(), confidenceString.ToString());
    }
}
taj
  • 1,128
  • 1
  • 9
  • 23
  • Although i have solve this error buti will try this code as well. And please can you tell me that what is the purpose to log intent or confidence. I think that only response matter in conversation. I am unable to understand the purpose of intent and confidence log, we should use or log repsonse in order to show response by the chatbot – Muhammad Faizan Khan Sep 05 '17 at 04:39
  • The intent and confidence above is to show how to get values of different keys in the dictionary. Using the above as an example you can get different objects in the `MessageResponse` such as `output`, `context`, `alternate_intents`, `entities` and `input`. The `context` is especially important since you pass the context to the next `MessageRequest` to keep a conversation going. – taj Sep 05 '17 at 14:22
  • why github code repo has in correct code? Additionally i want to know that why my speech to text conversation is not working by default – Muhammad Faizan Khan Sep 07 '17 at 06:54
  • can you please check this question https://stackoverflow.com/questions/46095944/ibm-watson-speech-to-text-service-is-not-giving-response-in-unity3d – Muhammad Faizan Khan Sep 08 '17 at 13:31