Skip to content

Commit 9e1155a

Browse files
committed
removing overload message with only conversationID, helper methods in data model to create objects, rewirte ExampleConversation as loop to ensure conversation is iterating
1 parent d7356d8 commit 9e1155a

File tree

4 files changed

+120
-93
lines changed

4 files changed

+120
-93
lines changed

Examples/ServiceExamples/Scripts/ExampleConversation.cs

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
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;
@@ -26,53 +25,60 @@ public class ExampleConversation : MonoBehaviour
2625
{
2726
private Conversation m_Conversation = new Conversation();
2827
private string m_WorkspaceID;
29-
private string m_Input = "Can you unlock the door?";
30-
private string m_ConversationID;
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"};
3131

3232
void Start () {
3333
LogSystem.InstallDefaultReactors();
3434
m_WorkspaceID = Config.Instance.GetVariableValue("ConversationV1_ID");
35-
Debug.Log("User: " + m_Input);
3635

37-
// Message with input only
38-
//m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input);
39-
40-
// Message by creating message request
41-
//MessageRequest messageRequest = new MessageRequest();
42-
//messageRequest.inputText = m_Input;
43-
//m_Conversation.Message(OnMessage, m_WorkspaceID, messageRequest);
44-
45-
// Message by passing input, alternate intents and conversationID
46-
m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input, false, null);
47-
48-
Converse("is there traffic today?");
36+
Debug.Log("**********User: Hello!");
37+
MessageWithOnlyInput("Hello!");
4938
}
5039

51-
void OnMessage (MessageResponse resp, string customData)
40+
private void MessageWithOnlyInput(string input)
5241
{
53-
if(resp != null)
54-
{
55-
foreach(Intent mi in resp.intents)
56-
Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);
57-
58-
if(resp.output != null && resp.output.text.Length > 0)
59-
foreach(string txt in resp.output.text)
60-
Debug.Log("output: " + txt);
42+
if (string.IsNullOrEmpty(input))
43+
throw new ArgumentNullException("input");
6144

62-
m_ConversationID = resp.context.conversation_id;
63-
64-
}
65-
else
66-
{
67-
Debug.Log("Failed to invoke Message();");
68-
}
45+
m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, input);
6946
}
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);
7055

71-
private void Converse(string input)
72-
{
73-
if (string.IsNullOrEmpty(input))
74-
throw new ArgumentNullException("input");
56+
if (resp.output != null && resp.output.text.Length > 0)
57+
foreach (string txt in resp.output.text)
58+
Debug.Log("output: " + txt);
7559

76-
m_Conversation.Message(OnMessage, m_WorkspaceID, input, true, m_ConversationID);
77-
}
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;
69+
70+
MessageWithFullMessageRequest(messageRequest);
71+
}
72+
else
73+
{
74+
Debug.Log("Failed to invoke Message();");
75+
}
76+
}
77+
78+
private void MessageWithFullMessageRequest(MessageRequest messageRequest)
79+
{
80+
if (messageRequest == null)
81+
throw new ArgumentNullException("messageRequest");
82+
m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, messageRequest);
83+
}
7884
}

Scripts/Services/Conversation/Conversation.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -126,33 +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-
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_MESSAGE);
139-
if (connector == null)
140-
return false;
141-
142-
string reqJson = "{\"input\":{\"text\":\"" + input + "\"},\"alternate_intents\":" + useAlternateIntents.ToString().ToLower() + ",\"context\":{\"conversation_id\":\"" + conversationID + "\"}}";
143-
144-
MessageReq req = new MessageReq();
145-
req.Callback = callback;
146-
req.Headers["Content-Type"] = "application/json";
147-
req.Headers["Accept"] = "application/json";
148-
req.Parameters["version"] = Version.VERSION;
149-
req.Function = "/" + workspaceID + "/message";
150-
req.Data = customData;
151-
req.Send = Encoding.UTF8.GetBytes(reqJson);
152-
req.OnResponse = MessageResp;
153-
154-
return connector.Send(req);
155-
}
156129

157130
private class MessageReq : RESTConnector.Request
158131
{

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

0 commit comments

Comments
 (0)