Skip to content

Commit 1128439

Browse files
committed
Conversation returns a generic object
1 parent d04df12 commit 1128439

File tree

4 files changed

+212
-154
lines changed

4 files changed

+212
-154
lines changed

Examples/ServiceExamples/Scripts/ExampleConversation.cs

Lines changed: 125 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
using IBM.Watson.DeveloperCloud.Logging;
2222
using System;
2323
using System.Collections.Generic;
24+
using System.Collections;
25+
using IBM.Watson.DeveloperCloud.DataTypes;
26+
using System.Reflection;
2427

2528
public class ExampleConversation : MonoBehaviour
2629
{
@@ -34,125 +37,146 @@ void Start()
3437
LogSystem.InstallDefaultReactors();
3538
m_WorkspaceID = Config.Instance.GetVariableValue("ConversationV1_ID");
3639

37-
Debug.Log("**********User: Hello!");
38-
MessageWithOnlyInput("Hello!");
39-
40-
//GetRawOutput("Hello");
41-
}
40+
//Debug.Log("**********User: Hello!");
41+
// MessageWithOnlyInput("Hello!");
4242

43+
GetRawOutput("Hello");
44+
}
45+
4346
private void GetRawOutput(string input)
4447
{
4548
m_Conversation.Message(OnGetRawOutput, m_WorkspaceID, input);
4649
}
4750

48-
private void OnGetRawOutput(MessageResponse resp, string customData)
51+
private void OnGetRawOutput(object resp, string customData)
4952
{
5053
if (!string.IsNullOrEmpty(customData))
5154
Debug.Log(customData);
5255
else
5356
Debug.Log("No raw data was received.");
54-
}
55-
56-
private void MessageWithOnlyInput(string input)
57-
{
58-
if (string.IsNullOrEmpty(input))
59-
throw new ArgumentNullException("input");
60-
61-
m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, input);
62-
}
63-
64-
65-
private void OnMessageWithOnlyInput(MessageResponse resp, string customData)
66-
{
67-
if (resp != null)
68-
{
69-
foreach (Intent mi in resp.intents)
70-
Debug.Log("Message Only intent: " + mi.intent + ", confidence: " + mi.confidence);
71-
72-
if (resp.output != null && resp.output.text.Length > 0)
73-
foreach (string txt in resp.output.text)
74-
Debug.Log("Message Only output: " + txt);
75-
76-
if (resp.context != null)
77-
{
78-
if (!string.IsNullOrEmpty(resp.context.conversation_id))
79-
Log.Debug("ExampleConversation", "Conversation ID: {0}", resp.context.conversation_id);
80-
else
81-
Log.Debug("ExampleConversation", "Conversation ID is null.");
82-
83-
if (resp.context.system != null)
84-
{
85-
Log.Debug("ExampleConversation", "dialog_request_counter: {0}", resp.context.system.dialog_request_counter);
86-
Log.Debug("ExampleConversation", "dialog_turn_counter: {0}", resp.context.system.dialog_turn_counter);
87-
88-
if (resp.context.system.dialog_stack != null)
89-
{
90-
foreach (Dictionary<string, string> dialogNode in resp.context.system.dialog_stack)
91-
foreach(KeyValuePair<string, string> node in dialogNode)
92-
Log.Debug("ExampleConversation", "dialogNode: {0}", node.Value);
93-
}
94-
else
95-
{
96-
Log.Debug("ExampleConversation", "dialog stack is null");
97-
}
9857

99-
}
100-
else
58+
if (resp != null)
10159
{
102-
Log.Debug("ExampleConversation", "system is null.");
103-
}
60+
Dictionary<string, object> respDict = resp as Dictionary<string, object>;
61+
object intents;
62+
respDict.TryGetValue("intents", out intents);
10463

105-
}
106-
else
107-
{
108-
Log.Debug("ExampleConversation", "Context is null");
109-
}
64+
foreach(var intentObj in (intents as List<object>))
65+
{
66+
Dictionary<string, object> intentDict = intentObj as Dictionary<string, object>;
11067

111-
string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
112-
Debug.Log(string.Format("**********User: {0}", questionStr));
68+
object intentString;
69+
intentDict.TryGetValue("intent", out intentString);
11370

114-
MessageRequest messageRequest = new MessageRequest();
115-
messageRequest.InputText = questionStr;
116-
messageRequest.alternate_intents = m_UseAlternateIntents;
117-
messageRequest.ContextData = resp.context;
71+
object confidenceString;
72+
intentDict.TryGetValue("confidence", out confidenceString);
11873

119-
MessageWithFullMessageRequest(messageRequest);
120-
}
121-
else
122-
{
123-
Debug.Log("Message Only: Failed to invoke Message();");
74+
Log.Debug("ExampleConversation", "intent: {0} | confidence {1}", intentString.ToString(), confidenceString.ToString());
75+
}
76+
}
12477
}
125-
}
12678

127-
private void MessageWithFullMessageRequest(MessageRequest messageRequest)
128-
{
129-
if (messageRequest == null)
130-
throw new ArgumentNullException("messageRequest");
131-
m_Conversation.Message(OnMessageWithFullRequest, m_WorkspaceID, messageRequest);
132-
}
79+
//private void MessageWithOnlyInput(string input)
80+
//{
81+
// if (string.IsNullOrEmpty(input))
82+
// throw new ArgumentNullException("input");
83+
84+
// m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, input);
85+
//}
86+
87+
88+
//private void OnMessageWithOnlyInput(object resp, string customData)
89+
//{
90+
// if (resp != null)
91+
// {
92+
// foreach (Intent mi in resp.intents)
93+
// Debug.Log("Message Only intent: " + mi.intent + ", confidence: " + mi.confidence);
94+
95+
// if (resp.output != null && resp.output.text.Length > 0)
96+
// foreach (string txt in resp.output.text)
97+
// Debug.Log("Message Only output: " + txt);
98+
99+
// if (resp.context != null)
100+
// {
101+
// if (!string.IsNullOrEmpty(resp.context.conversation_id))
102+
// Log.Debug("ExampleConversation", "Conversation ID: {0}", resp.context.conversation_id);
103+
// else
104+
// Log.Debug("ExampleConversation", "Conversation ID is null.");
105+
106+
// if (resp.context.system != null)
107+
// {
108+
// Log.Debug("ExampleConversation", "dialog_request_counter: {0}", resp.context.system.dialog_request_counter);
109+
// Log.Debug("ExampleConversation", "dialog_turn_counter: {0}", resp.context.system.dialog_turn_counter);
110+
111+
// if (resp.context.system.dialog_stack != null)
112+
// {
113+
// foreach (Dictionary<string, string> dialogNode in resp.context.system.dialog_stack)
114+
// foreach(KeyValuePair<string, string> node in dialogNode)
115+
// Log.Debug("ExampleConversation", "dialogNode: {0}", node.Value);
116+
// }
117+
// else
118+
// {
119+
// Log.Debug("ExampleConversation", "dialog stack is null");
120+
// }
121+
122+
// }
123+
// else
124+
// {
125+
// Log.Debug("ExampleConversation", "system is null.");
126+
// }
127+
128+
// }
129+
// else
130+
// {
131+
// Log.Debug("ExampleConversation", "Context is null");
132+
// }
133+
134+
// string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
135+
// Debug.Log(string.Format("**********User: {0}", questionStr));
136+
137+
// MessageRequest messageRequest = new MessageRequest();
138+
// messageRequest.InputText = questionStr;
139+
// messageRequest.alternate_intents = m_UseAlternateIntents;
140+
// messageRequest.ContextData = resp.context;
141+
142+
// MessageWithFullMessageRequest(messageRequest);
143+
// }
144+
// else
145+
// {
146+
// Debug.Log("Message Only: Failed to invoke Message();");
147+
// }
148+
//}
149+
150+
//private void MessageWithFullMessageRequest(MessageRequest messageRequest)
151+
//{
152+
// if (messageRequest == null)
153+
// throw new ArgumentNullException("messageRequest");
154+
// m_Conversation.Message(OnMessageWithFullRequest, m_WorkspaceID, messageRequest);
155+
//}
156+
157+
//private void OnMessageWithFullRequest(MessageResponse resp, string customData)
158+
//{
159+
// if (resp != null)
160+
// {
161+
// foreach (Intent mi in resp.intents)
162+
// Debug.Log("Full Request intent: " + mi.intent + ", confidence: " + mi.confidence);
163+
164+
// if (resp.output != null && resp.output.text.Length > 0)
165+
// foreach (string txt in resp.output.text)
166+
// Debug.Log("Full Request output: " + txt);
167+
168+
// string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
169+
// Debug.Log(string.Format("**********User: {0}", questionStr));
170+
171+
// MessageRequest messageRequest = new MessageRequest();
172+
// messageRequest.InputText = questionStr;
173+
// messageRequest.alternate_intents = m_UseAlternateIntents;
174+
// messageRequest.ContextData = resp.context;
175+
// }
176+
// else
177+
// {
178+
// Debug.Log("Full Request: Failed to invoke Message();");
179+
// }
180+
//}
133181

134-
private void OnMessageWithFullRequest(MessageResponse resp, string customData)
135-
{
136-
if (resp != null)
137-
{
138-
foreach (Intent mi in resp.intents)
139-
Debug.Log("Full Request intent: " + mi.intent + ", confidence: " + mi.confidence);
140-
141-
if (resp.output != null && resp.output.text.Length > 0)
142-
foreach (string txt in resp.output.text)
143-
Debug.Log("Full Request output: " + txt);
144-
145-
string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
146-
Debug.Log(string.Format("**********User: {0}", questionStr));
147-
148-
MessageRequest messageRequest = new MessageRequest();
149-
messageRequest.InputText = questionStr;
150-
messageRequest.alternate_intents = m_UseAlternateIntents;
151-
messageRequest.ContextData = resp.context;
152-
}
153-
else
154-
{
155-
Debug.Log("Full Request: Failed to invoke Message();");
156-
}
157-
}
158182
}

Examples/ServiceExamples/ServiceExamples.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ GameObject:
106106
m_Icon: {fileID: 0}
107107
m_NavMeshLayer: 0
108108
m_StaticEditorFlags: 0
109-
m_IsActive: 1
109+
m_IsActive: 0
110110
--- !u!114 &85803341
111111
MonoBehaviour:
112112
m_ObjectHideFlags: 0
@@ -433,7 +433,7 @@ GameObject:
433433
m_Icon: {fileID: 0}
434434
m_NavMeshLayer: 0
435435
m_StaticEditorFlags: 0
436-
m_IsActive: 0
436+
m_IsActive: 1
437437
--- !u!114 &859102723
438438
MonoBehaviour:
439439
m_ObjectHideFlags: 0

Scripts/Services/Conversation/Conversation.cs

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using IBM.Watson.DeveloperCloud.Utilities;
2222
using IBM.Watson.DeveloperCloud.Connection;
2323
using IBM.Watson.DeveloperCloud.Logging;
24+
using MiniJSON;
2425

2526
namespace IBM.Watson.DeveloperCloud.Services.Conversation.v1
2627
{
@@ -47,16 +48,17 @@ public class Conversation : IWatsonService
4748
/// The callback delegate for the Message() function.
4849
/// </summary>
4950
/// <param name="resp">The response object to a call to Message().</param>
50-
public delegate void OnMessage(MessageResponse resp, string customData);
51-
52-
/// <summary>
53-
/// Message the specified workspaceId, input and callback.
54-
/// </summary>
55-
/// <param name="workspaceID">Workspace identifier.</param>
56-
/// <param name="input">Input.</param>
57-
/// <param name="callback">Callback.</param>
58-
/// <param name="customData">Custom data.</param>
59-
public bool Message(OnMessage callback, string workspaceID, string input, string customData = default(string))
51+
public delegate void OnMessage(object resp, string customData);
52+
//public delegate void OnMessage(MessageResponse resp, string customData);
53+
54+
/// <summary>
55+
/// Message the specified workspaceId, input and callback.
56+
/// </summary>
57+
/// <param name="workspaceID">Workspace identifier.</param>
58+
/// <param name="input">Input.</param>
59+
/// <param name="callback">Callback.</param>
60+
/// <param name="customData">Custom data.</param>
61+
public bool Message(OnMessage callback, string workspaceID, string input, string customData = default(string))
6062
{
6163
if (string.IsNullOrEmpty(workspaceID))
6264
throw new ArgumentNullException("workspaceId");
@@ -132,33 +134,42 @@ private class MessageReq : RESTConnector.Request
132134
public string Data { get; set; }
133135
}
134136

135-
private void MessageResp(RESTConnector.Request req, RESTConnector.Response resp)
136-
{
137-
MessageResponse response = new MessageResponse();
138-
fsData data = null;
139-
140-
if (resp.Success)
141-
{
142-
try
143-
{
144-
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
145-
if (!r.Succeeded)
146-
throw new WatsonException(r.FormattedMessages);
147-
148-
object obj = response;
149-
r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
150-
if (!r.Succeeded)
151-
throw new WatsonException(r.FormattedMessages);
152-
}
153-
catch (Exception e)
137+
private void MessageResp(RESTConnector.Request req, RESTConnector.Response resp)
154138
{
155-
Log.Error("Conversation", "MessageResp Exception: {0}", e.ToString());
156-
resp.Success = false;
157-
}
158-
}
159-
160-
if (((MessageReq)req).Callback != null)
161-
((MessageReq)req).Callback(resp.Success ? response : null, !string.IsNullOrEmpty(((MessageReq)req).Data) ? ((MessageReq)req).Data : data.ToString());
139+
object dataObject = null;
140+
string data = "";
141+
142+
//MessageResponse response = new MessageResponse();
143+
//fsData data = null;
144+
145+
if (resp.Success)
146+
{
147+
try
148+
{
149+
// For deserializing into a generic object
150+
data = Encoding.UTF8.GetString(resp.Data);
151+
dataObject = Json.Deserialize(data);
152+
153+
// For deserializing into fixed data model
154+
// fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
155+
// if (!r.Succeeded)
156+
// throw new WatsonException(r.FormattedMessages);
157+
158+
// object obj = response;
159+
// r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
160+
// if (!r.Succeeded)
161+
// throw new WatsonException(r.FormattedMessages);
162+
}
163+
catch (Exception e)
164+
{
165+
Log.Error("Conversation", "MessageResp Exception: {0}", e.ToString());
166+
data = e.Message;
167+
resp.Success = false;
168+
}
169+
}
170+
171+
if (((MessageReq)req).Callback != null)
172+
((MessageReq)req).Callback(resp.Success ? dataObject : null, !string.IsNullOrEmpty(((MessageReq)req).Data) ? ((MessageReq)req).Data : data.ToString());
162173
}
163174
#endregion
164175

@@ -220,7 +231,7 @@ public CheckServiceStatus(Conversation service, ServiceStatus callback)
220231
}
221232
}
222233

223-
private void OnMessage(MessageResponse resp, string customData)
234+
private void OnMessage(object resp, string customData)
224235
{
225236
if (m_ConversationCount > 0)
226237
{

0 commit comments

Comments
 (0)