Skip to content

Commit 54b3496

Browse files
authored
Merge pull request #153 from watson-developer-cloud/feature-conversationIDFix
Fixing issue with error on sending message with conversation ID and e…
2 parents adce3b0 + 7187d1d commit 54b3496

File tree

7 files changed

+126
-93
lines changed

7 files changed

+126
-93
lines changed

Examples/ServiceExamples/Scripts/ExampleConversation.cs

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,69 @@
1616
*/
1717

1818
using UnityEngine;
19-
using System.Collections;
2019
using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
2120
using IBM.Watson.DeveloperCloud.Utilities;
2221
using IBM.Watson.DeveloperCloud.Logging;
22+
using System;
2323

2424
public class ExampleConversation : MonoBehaviour
2525
{
2626
private Conversation m_Conversation = new Conversation();
2727
private string m_WorkspaceID;
28-
private string m_Input = "Can you unlock the door?";
28+
private string m_ConversationID;
29+
private bool m_UseAlternateIntents = true;
30+
private string[] questionArray = { "can you turn up the AC", "can you turn on the wipers", "can you turn off the wipers", "can you turn down the ac", "can you unlock the door"};
2931

3032
void Start () {
3133
LogSystem.InstallDefaultReactors();
3234
m_WorkspaceID = Config.Instance.GetVariableValue("ConversationV1_ID");
33-
Debug.Log("User: " + m_Input);
3435

35-
// Message with input only
36-
//m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input);
36+
Debug.Log("**********User: Hello!");
37+
MessageWithOnlyInput("Hello!");
38+
}
3739

38-
// Message by creating message request
39-
//MessageRequest messageRequest = new MessageRequest();
40-
//messageRequest.inputText = m_Input;
41-
//m_Conversation.Message(OnMessage, m_WorkspaceID, messageRequest);
40+
private void MessageWithOnlyInput(string input)
41+
{
42+
if (string.IsNullOrEmpty(input))
43+
throw new ArgumentNullException("input");
44+
45+
m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, input);
46+
}
47+
48+
49+
private void OnMessageWithOnlyInput(MessageResponse resp, string customData)
50+
{
51+
if (resp != null)
52+
{
53+
foreach (Intent mi in resp.intents)
54+
Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);
55+
56+
if (resp.output != null && resp.output.text.Length > 0)
57+
foreach (string txt in resp.output.text)
58+
Debug.Log("output: " + txt);
59+
60+
m_ConversationID = resp.context.conversation_id;
61+
62+
string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
63+
Debug.Log(string.Format("**********User: {0}", questionStr));
64+
65+
MessageRequest messageRequest = new MessageRequest();
66+
messageRequest.InputText = questionStr;
67+
messageRequest.alternate_intents = m_UseAlternateIntents;
68+
messageRequest.ContextData = resp.context;
4269

43-
// Message by passing input, alternate intents and conversationID
44-
m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input, false, null);
70+
MessageWithFullMessageRequest(messageRequest);
71+
}
72+
else
73+
{
74+
Debug.Log("Failed to invoke Message();");
75+
}
4576
}
4677

47-
void OnMessage (MessageResponse resp, string customData)
78+
private void MessageWithFullMessageRequest(MessageRequest messageRequest)
4879
{
49-
if(resp != null)
50-
{
51-
foreach(Intent mi in resp.intents)
52-
Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);
53-
54-
if(resp.output != null && resp.output.text.Length > 0)
55-
foreach(string txt in resp.output.text)
56-
Debug.Log("output: " + txt);
57-
}
58-
else
59-
{
60-
Debug.Log("Failed to invoke Message();");
61-
}
80+
if (messageRequest == null)
81+
throw new ArgumentNullException("messageRequest");
82+
m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, messageRequest);
6283
}
6384
}

Examples/ServiceExamples/ServiceExamples.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ GameObject:
352352
m_Icon: {fileID: 0}
353353
m_NavMeshLayer: 0
354354
m_StaticEditorFlags: 0
355-
m_IsActive: 0
355+
m_IsActive: 1
356356
--- !u!114 &859102723
357357
MonoBehaviour:
358358
m_ObjectHideFlags: 0
@@ -593,7 +593,7 @@ GameObject:
593593
m_Icon: {fileID: 0}
594594
m_NavMeshLayer: 0
595595
m_StaticEditorFlags: 0
596-
m_IsActive: 1
596+
m_IsActive: 0
597597
--- !u!114 &1713392458
598598
MonoBehaviour:
599599
m_ObjectHideFlags: 0

Scripts/Services/Conversation/Conversation.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -126,42 +126,6 @@ public class Conversation : IWatsonService
126126
return connector.Send(req);
127127
}
128128

129-
public bool Message(OnMessage callback, string workspaceID, string input, bool useAlternateIntents, string conversationID = default(string), string customData = default(string))
130-
{
131-
if (string.IsNullOrEmpty(workspaceID))
132-
throw new ArgumentNullException("workspaceId");
133-
if (string.IsNullOrEmpty(input))
134-
throw new ArgumentNullException("input");
135-
if (callback == null)
136-
throw new ArgumentNullException("callback");
137-
138-
MessageRequest messageRequest = new MessageRequest();
139-
messageRequest.inputText = input;
140-
messageRequest.alternate_intents = useAlternateIntents;
141-
if (conversationID != default(string))
142-
messageRequest.conversationID = conversationID;
143-
144-
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_MESSAGE);
145-
if (connector == null)
146-
return false;
147-
148-
fsData data;
149-
sm_Serializer.TrySerialize(messageRequest.GetType(), messageRequest, out data).AssertSuccessWithoutWarnings();
150-
string reqString = fsJsonPrinter.CompressedJson(data);
151-
152-
MessageReq req = new MessageReq();
153-
req.Callback = callback;
154-
req.MessageRequest = messageRequest;
155-
req.Headers["Content-Type"] = "application/json";
156-
req.Headers["Accept"] = "application/json";
157-
req.Parameters["version"] = Version.VERSION;
158-
req.Function = "/" + workspaceID + "/message";
159-
req.Data = customData;
160-
req.Send = Encoding.UTF8.GetBytes(reqString);
161-
req.OnResponse = MessageResp;
162-
163-
return connector.Send(req);
164-
}
165129

166130
private class MessageReq : RESTConnector.Request
167131
{

Scripts/Services/Conversation/DataModels.cs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ public class LogMessageResponse
127127
[fsObject]
128128
public class MessageRequest
129129
{
130+
///// <summary>
131+
///// Default constructor.
132+
///// </summary>
133+
//public MessageRequest()
134+
//{
135+
// input = new InputData();
136+
// context = new Context();
137+
//}
138+
130139
/// <summary>
131140
/// The input text.
132141
/// </summary>
@@ -143,9 +152,19 @@ public class MessageRequest
143152
/// <summary>
144153
/// Creates the input object and sets the InputText.
145154
/// </summary>
146-
public string inputText
155+
public string InputText
147156
{
148-
get { return input != null ? input.text : null; }
157+
get {
158+
if (input == null)
159+
{
160+
input = new InputData();
161+
return "";
162+
}
163+
else
164+
{
165+
return input.text;
166+
}
167+
}
149168
set
150169
{
151170
if (input == null)
@@ -155,6 +174,21 @@ public string inputText
155174
}
156175
}
157176

177+
/// <summary>
178+
/// Gets and sets the input value and creates the InputData object if it hasn't been created.
179+
/// </summary>
180+
public InputData InputData
181+
{
182+
get { return input != null ? input : input = new InputData(); }
183+
set
184+
{
185+
if (input == null)
186+
input = new InputData();
187+
188+
input = value;
189+
}
190+
}
191+
158192
/// <summary>
159193
/// Creates the Context object and sets the conversation_id.
160194
/// </summary>
@@ -169,6 +203,21 @@ public string conversationID
169203
context.conversation_id = value;
170204
}
171205
}
206+
207+
/// <summary>
208+
/// Gets and sets the context value and creates the Context object if it hasn't been created.
209+
/// </summary>
210+
public Context ContextData
211+
{
212+
get { return context != null ? context : context = new Context(); }
213+
set
214+
{
215+
if (context == null)
216+
context = new Context();
217+
218+
context = value;
219+
}
220+
}
172221
}
173222
#endregion
174223

@@ -191,6 +240,13 @@ public class InputData
191240
[fsObject]
192241
public class Context
193242
{
243+
///// <summary>
244+
///// Default constructor.
245+
///// </summary>
246+
//public Context()
247+
//{
248+
// system = new SystemResponse();
249+
//}
194250
/// <summary>
195251
/// The unique identifier of the conversation.
196252
/// </summary>
@@ -199,7 +255,22 @@ public class Context
199255
/// Information about the dialog
200256
/// </summary>
201257
public SystemResponse system { get; set; }
202-
}
258+
259+
/// <summary>
260+
/// Creates the SystemResponse object and sets it.
261+
/// </summary>
262+
public SystemResponse SystemResponse
263+
{
264+
get { return system != null ? system : system = new SystemResponse(); }
265+
set
266+
{
267+
if (system == null)
268+
system = new SystemResponse();
269+
270+
system = value;
271+
}
272+
}
273+
}
203274

204275
/// <summary>
205276
/// Dialog information.

Scripts/Services/TextToSpeech/TextToSpeech.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ private void OnDeleteCustomizationResp(RESTConnector.Request req, RESTConnector.
660660
if (callback == null)
661661
throw new ArgumentNullException("callback");
662662
if (string.IsNullOrEmpty(customizationID))
663-
throw new ArgumentNullException("A name is customizationID to get a custom voice model.");
663+
throw new ArgumentNullException("A customizationID to get a custom voice model.");
664664

665665
GetCustomizationRequest req = new GetCustomizationRequest();
666666
req.Callback = callback;

Scripts/UnitTests/TestConversation.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public class TestConversation : UnitTest
2929
private string m_Input = "Can you unlock the door?";
3030
private bool m_MessageInputTested = false;
3131
private bool m_MessageObjectTested = false;
32-
private bool m_MessageTested = false;
3332

3433
public override IEnumerator RunTest()
3534
{
@@ -48,19 +47,12 @@ public override IEnumerator RunTest()
4847
if (!m_MessageObjectTested)
4948
{
5049
MessageRequest messageRequest = new MessageRequest();
51-
messageRequest.inputText = m_Input;
50+
messageRequest.InputText = m_Input;
5251
m_Conversation.Message(OnMessageObject, m_WorkspaceID, messageRequest);
5352
while (!m_MessageObjectTested)
5453
yield return null;
5554
}
56-
57-
if (!m_MessageTested)
58-
{
59-
m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input, false);
60-
while (!m_MessageTested)
61-
yield return null;
62-
}
63-
55+
6456
yield break;
6557
}
6658

@@ -93,19 +85,4 @@ private void OnMessageObject(MessageResponse resp, string customData)
9385

9486
m_MessageObjectTested = true;
9587
}
96-
97-
private void OnMessage(MessageResponse resp, string customData)
98-
{
99-
Test(resp != null);
100-
if (resp != null)
101-
{
102-
foreach (Intent mi in resp.intents)
103-
Log.Debug("TestConversation", "intent: " + mi.intent + ", confidence: " + mi.confidence);
104-
if (resp.output != null && resp.output.text.Length > 0)
105-
foreach (string txt in resp.output.text)
106-
Debug.Log("output: " + txt);
107-
}
108-
109-
m_MessageTested = true;
110-
}
11188
}

Scripts/Utilities/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static class Resources
6666
public static class String
6767
{
6868
/// <exclude />
69-
public const string VERSION = "watson-developer-cloud-unity-sdk-0.9.0";
69+
public const string VERSION = "watson-developer-cloud-unity-sdk-0.10.0";
7070
/// <exclude />
7171
public const string DEBUG_DISPLAY_QUALITY = "Quality: {0}";
7272
}

0 commit comments

Comments
 (0)