Skip to content

Commit d99f533

Browse files
authored
Merge pull request #480 from watson-developer-cloud/hotfix-assistant-serialization
Assistant v2 MessageRequest serialization
2 parents 95989e2 + 4396608 commit d99f533

File tree

5 files changed

+332
-29
lines changed

5 files changed

+332
-29
lines changed

Examples/ServiceExamples/Scripts/ExampleAssistantV2.cs

Lines changed: 97 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public class ExampleAssistantV2 : MonoBehaviour
5757
private Assistant _service;
5858

5959
private bool _createSessionTested = false;
60-
private bool _messageTested = false;
60+
private bool _messageTested0 = false;
61+
private bool _messageTested1 = false;
62+
private bool _messageTested2 = false;
63+
private bool _messageTested3 = false;
64+
private bool _messageTested4 = false;
6165
private bool _deleteSessionTested = false;
6266
private string _sessionId;
6367

@@ -104,23 +108,83 @@ private IEnumerator CreateService()
104108

105109
private IEnumerator Examples()
106110
{
107-
Log.Debug("ExampleAssistantV2.Examples()", "Attempting to CreateSession");
111+
Log.Debug("ExampleAssistantV2.RunTest()", "Attempting to CreateSession");
108112
_service.CreateSession(OnCreateSession, OnFail, _assistantId);
109113

110114
while (!_createSessionTested)
111115
{
112116
yield return null;
113117
}
114118

115-
Log.Debug("ExampleAssistantV2.Examples()", "Attempting to Message");
116-
_service.Message(OnMessage, OnFail, _assistantId, _sessionId);
119+
Log.Debug("ExampleAssistantV2.RunTest()", "Attempting to Message");
120+
_service.Message(OnMessage0, OnFail, _assistantId, _sessionId);
117121

118-
while (!_messageTested)
122+
while (!_messageTested0)
119123
{
120124
yield return null;
121125
}
122126

123-
Log.Debug("ExampleAssistantV2.Examples()", "Attempting to DeleteSession");
127+
Log.Debug("ExampleAssistantV2.RunTest()", "Are you open on Christmas?");
128+
MessageRequest messageRequest1 = new MessageRequest()
129+
{
130+
Input = new MessageInput()
131+
{
132+
Text = "Are you open on Christmas?"
133+
}
134+
};
135+
_service.Message(OnMessage1, OnFail, _assistantId, _sessionId, messageRequest1);
136+
137+
while (!_messageTested1)
138+
{
139+
yield return null;
140+
}
141+
142+
Log.Debug("ExampleAssistantV2.RunTest()", "What are your hours?");
143+
MessageRequest messageRequest2 = new MessageRequest()
144+
{
145+
Input = new MessageInput()
146+
{
147+
Text = "What are your hours?"
148+
}
149+
};
150+
_service.Message(OnMessage2, OnFail, _assistantId, _sessionId, messageRequest2);
151+
152+
while (!_messageTested2)
153+
{
154+
yield return null;
155+
}
156+
157+
Log.Debug("ExampleAssistantV2.RunTest()", "I'd like to make an appointment for 12pm.");
158+
MessageRequest messageRequest3 = new MessageRequest()
159+
{
160+
Input = new MessageInput()
161+
{
162+
Text = "I'd like to make an appointment for 12pm."
163+
}
164+
};
165+
_service.Message(OnMessage3, OnFail, _assistantId, _sessionId, messageRequest3);
166+
167+
while (!_messageTested3)
168+
{
169+
yield return null;
170+
}
171+
172+
Log.Debug("ExampleAssistantV2.RunTest()", "On Friday please.");
173+
MessageRequest messageRequest4 = new MessageRequest()
174+
{
175+
Input = new MessageInput()
176+
{
177+
Text = "On Friday please."
178+
}
179+
};
180+
_service.Message(OnMessage4, OnFail, _assistantId, _sessionId, messageRequest4);
181+
182+
while (!_messageTested4)
183+
{
184+
yield return null;
185+
}
186+
187+
Log.Debug("ExampleAssistantV2.RunTest()", "Attempting to delete session");
124188
_service.DeleteSession(OnDeleteSession, OnFail, _assistantId, _sessionId);
125189

126190
while (!_deleteSessionTested)
@@ -137,9 +201,34 @@ private void OnDeleteSession(object response, Dictionary<string, object> customD
137201
_createSessionTested = true;
138202
}
139203

140-
private void OnMessage(MessageResponse response, Dictionary<string, object> customData)
204+
private void OnMessage0(MessageResponse response, Dictionary<string, object> customData)
205+
{
206+
Log.Debug("ExampleAssistantV2.OnMessage0()", "response: {0}", response.Output.Generic[0].Text);
207+
_messageTested0 = true;
208+
}
209+
210+
private void OnMessage1(MessageResponse response, Dictionary<string, object> customData)
211+
{
212+
Log.Debug("ExampleAssistantV2.OnMessage1()", "response: {0}", response.Output.Generic[0].Text);
213+
214+
_messageTested1 = true;
215+
}
216+
217+
private void OnMessage2(MessageResponse response, Dictionary<string, object> customData)
218+
{
219+
Log.Debug("ExampleAssistantV2.OnMessage2()", "response: {0}", response.Output.Generic[0].Text);
220+
_messageTested2 = true;
221+
}
222+
223+
private void OnMessage3(MessageResponse response, Dictionary<string, object> customData)
224+
{
225+
Log.Debug("ExampleAssistantV2.OnMessage3()", "response: {0}", response.Output.Generic[0].Text);
226+
_messageTested3 = true;
227+
}
228+
private void OnMessage4(MessageResponse response, Dictionary<string, object> customData)
141229
{
142-
_messageTested = true;
230+
Log.Debug("ExampleAssistantV2.OnMessage4()", "response: {0}", response.Output.Generic[0].Text);
231+
_messageTested4 = true;
143232
}
144233

145234
private void OnCreateSession(SessionResponse response, Dictionary<string, object> customData)

Scripts/Services/Assistant/v2/Assistant.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -397,20 +397,14 @@ public bool Message(SuccessCallback<MessageResponse> successCallback, FailCallba
397397
}
398398
}
399399

400-
IDictionary<string, string> requestDict = new Dictionary<string, string>();
401-
int iterator = 0;
402-
StringBuilder stringBuilder = new StringBuilder("{");
403-
foreach (KeyValuePair<string, string> property in requestDict)
400+
if (request != null)
404401
{
405-
string delimeter = iterator < requestDict.Count - 1 ? "," : "";
406-
stringBuilder.Append(string.Format("\"{0}\": {1}{2}", property.Key, property.Value, delimeter));
407-
iterator++;
402+
fsData data = null;
403+
_serializer.TrySerialize(request, out data);
404+
string json = data.ToString().Replace('\"', '"');
405+
req.Send = Encoding.UTF8.GetBytes(json);
408406
}
409-
stringBuilder.Append("}");
410407

411-
string stringToSend = stringBuilder.ToString();
412-
413-
req.Send = Encoding.UTF8.GetBytes(stringToSend);
414408
req.Headers["Content-Type"] = "application/json";
415409
req.Parameters["version"] = VersionDate;
416410
req.OnResponse = OnMessageResponse;

Scripts/Services/Assistant/v2/Models/MessageInput.cs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*
1616
*/
1717
using FullSerializer;
18+
using System;
1819
using System.Collections.Generic;
1920
using System.Runtime.Serialization;
2021

@@ -23,7 +24,7 @@ namespace IBM.WatsonDeveloperCloud.Assistant.v2
2324
/// <summary>
2425
/// The user input.
2526
/// </summary>
26-
[fsObject]
27+
[fsObject(Converter = typeof(MessageInputConverter))]
2728
public class MessageInput
2829
{
2930
/// <summary>
@@ -73,4 +74,70 @@ public enum MessageTypeEnum
7374
public string SuggestionId { get; set; }
7475
}
7576

77+
#region Message Input Converter
78+
public class MessageInputConverter : fsConverter
79+
{
80+
private fsSerializer _serializer = new fsSerializer();
81+
82+
public override bool CanProcess(Type type)
83+
{
84+
return type == typeof(MessageInput);
85+
}
86+
87+
public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
88+
{
89+
throw new NotImplementedException();
90+
}
91+
92+
public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
93+
{
94+
MessageInput messageInput = (MessageInput)instance;
95+
serialized = null;
96+
97+
Dictionary<string, fsData> serialization = new Dictionary<string, fsData>();
98+
99+
fsData tempData = null;
100+
101+
if (messageInput.MessageType != null)
102+
{
103+
_serializer.TrySerialize(messageInput.MessageType, out tempData);
104+
serialization.Add("message_type", tempData);
105+
}
106+
107+
if (messageInput.Text != null)
108+
{
109+
_serializer.TrySerialize(messageInput.Text, out tempData);
110+
serialization.Add("text", tempData);
111+
}
112+
113+
if (messageInput.Options != null)
114+
{
115+
_serializer.TrySerialize(messageInput.Options, out tempData);
116+
serialization.Add("options", tempData);
117+
}
118+
119+
if (messageInput.Intents != null)
120+
{
121+
_serializer.TrySerialize(messageInput.Intents, out tempData);
122+
serialization.Add("intents", tempData);
123+
}
124+
125+
if (messageInput.Entities != null)
126+
{
127+
_serializer.TrySerialize(messageInput.Entities, out tempData);
128+
serialization.Add("entities", tempData);
129+
}
130+
131+
if (messageInput.SuggestionId != null)
132+
{
133+
_serializer.TrySerialize(messageInput.SuggestionId, out tempData);
134+
serialization.Add("suggestion_id", tempData);
135+
}
136+
137+
serialized = new fsData(serialization);
138+
139+
return fsResult.Success;
140+
}
141+
#endregion
142+
}
76143
}

Scripts/Services/Assistant/v2/Models/MessageInputOptions.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
*/
1717

1818
using FullSerializer;
19+
using System;
20+
using System.Collections.Generic;
1921

2022
namespace IBM.WatsonDeveloperCloud.Assistant.v2
2123
{
2224
/// <summary>
2325
/// Optional properties that control how the assistant responds.
2426
/// </summary>
25-
[fsObject]
27+
[fsObject(Converter = typeof(MessageInputOptionsConverter))]
2628
public class MessageInputOptions
2729
{
2830
/// <summary>
@@ -50,4 +52,58 @@ public class MessageInputOptions
5052
public bool? ReturnContext { get; set; }
5153
}
5254

55+
#region Create Value Converter
56+
public class MessageInputOptionsConverter : fsConverter
57+
{
58+
private fsSerializer _serializer = new fsSerializer();
59+
60+
public override bool CanProcess(Type type)
61+
{
62+
return type == typeof(MessageInputOptions);
63+
}
64+
65+
public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
66+
{
67+
throw new NotImplementedException();
68+
}
69+
70+
public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
71+
{
72+
MessageInputOptions messageInputOptions = (MessageInputOptions)instance;
73+
serialized = null;
74+
75+
Dictionary<string, fsData> serialization = new Dictionary<string, fsData>();
76+
77+
fsData tempData = null;
78+
79+
if (messageInputOptions.Debug != null)
80+
{
81+
_serializer.TrySerialize(messageInputOptions.Debug, out tempData);
82+
serialization.Add("debug", tempData);
83+
}
84+
85+
if (messageInputOptions.Restart != null)
86+
{
87+
_serializer.TrySerialize(messageInputOptions.Restart, out tempData);
88+
serialization.Add("restart", tempData);
89+
}
90+
91+
if (messageInputOptions.AlternateIntents != null)
92+
{
93+
_serializer.TrySerialize(messageInputOptions.AlternateIntents, out tempData);
94+
serialization.Add("alternate_intents", tempData);
95+
}
96+
97+
if (messageInputOptions.ReturnContext != null)
98+
{
99+
_serializer.TrySerialize(messageInputOptions.ReturnContext, out tempData);
100+
serialization.Add("return_context", tempData);
101+
}
102+
103+
serialized = new fsData(serialization);
104+
105+
return fsResult.Success;
106+
}
107+
#endregion
108+
}
53109
}

0 commit comments

Comments
 (0)