Skip to content

Assistant v2 MessageRequest serialization #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 97 additions & 8 deletions Examples/ServiceExamples/Scripts/ExampleAssistantV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public class ExampleAssistantV2 : MonoBehaviour
private Assistant _service;

private bool _createSessionTested = false;
private bool _messageTested = false;
private bool _messageTested0 = false;
private bool _messageTested1 = false;
private bool _messageTested2 = false;
private bool _messageTested3 = false;
private bool _messageTested4 = false;
private bool _deleteSessionTested = false;
private string _sessionId;

Expand Down Expand Up @@ -104,23 +108,83 @@ private IEnumerator CreateService()

private IEnumerator Examples()
{
Log.Debug("ExampleAssistantV2.Examples()", "Attempting to CreateSession");
Log.Debug("ExampleAssistantV2.RunTest()", "Attempting to CreateSession");
_service.CreateSession(OnCreateSession, OnFail, _assistantId);

while (!_createSessionTested)
{
yield return null;
}

Log.Debug("ExampleAssistantV2.Examples()", "Attempting to Message");
_service.Message(OnMessage, OnFail, _assistantId, _sessionId);
Log.Debug("ExampleAssistantV2.RunTest()", "Attempting to Message");
_service.Message(OnMessage0, OnFail, _assistantId, _sessionId);

while (!_messageTested)
while (!_messageTested0)
{
yield return null;
}

Log.Debug("ExampleAssistantV2.Examples()", "Attempting to DeleteSession");
Log.Debug("ExampleAssistantV2.RunTest()", "Are you open on Christmas?");
MessageRequest messageRequest1 = new MessageRequest()
{
Input = new MessageInput()
{
Text = "Are you open on Christmas?"
}
};
_service.Message(OnMessage1, OnFail, _assistantId, _sessionId, messageRequest1);

while (!_messageTested1)
{
yield return null;
}

Log.Debug("ExampleAssistantV2.RunTest()", "What are your hours?");
MessageRequest messageRequest2 = new MessageRequest()
{
Input = new MessageInput()
{
Text = "What are your hours?"
}
};
_service.Message(OnMessage2, OnFail, _assistantId, _sessionId, messageRequest2);

while (!_messageTested2)
{
yield return null;
}

Log.Debug("ExampleAssistantV2.RunTest()", "I'd like to make an appointment for 12pm.");
MessageRequest messageRequest3 = new MessageRequest()
{
Input = new MessageInput()
{
Text = "I'd like to make an appointment for 12pm."
}
};
_service.Message(OnMessage3, OnFail, _assistantId, _sessionId, messageRequest3);

while (!_messageTested3)
{
yield return null;
}

Log.Debug("ExampleAssistantV2.RunTest()", "On Friday please.");
MessageRequest messageRequest4 = new MessageRequest()
{
Input = new MessageInput()
{
Text = "On Friday please."
}
};
_service.Message(OnMessage4, OnFail, _assistantId, _sessionId, messageRequest4);

while (!_messageTested4)
{
yield return null;
}

Log.Debug("ExampleAssistantV2.RunTest()", "Attempting to delete session");
_service.DeleteSession(OnDeleteSession, OnFail, _assistantId, _sessionId);

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

private void OnMessage(MessageResponse response, Dictionary<string, object> customData)
private void OnMessage0(MessageResponse response, Dictionary<string, object> customData)
{
Log.Debug("ExampleAssistantV2.OnMessage0()", "response: {0}", response.Output.Generic[0].Text);
_messageTested0 = true;
}

private void OnMessage1(MessageResponse response, Dictionary<string, object> customData)
{
Log.Debug("ExampleAssistantV2.OnMessage1()", "response: {0}", response.Output.Generic[0].Text);

_messageTested1 = true;
}

private void OnMessage2(MessageResponse response, Dictionary<string, object> customData)
{
Log.Debug("ExampleAssistantV2.OnMessage2()", "response: {0}", response.Output.Generic[0].Text);
_messageTested2 = true;
}

private void OnMessage3(MessageResponse response, Dictionary<string, object> customData)
{
Log.Debug("ExampleAssistantV2.OnMessage3()", "response: {0}", response.Output.Generic[0].Text);
_messageTested3 = true;
}
private void OnMessage4(MessageResponse response, Dictionary<string, object> customData)
{
_messageTested = true;
Log.Debug("ExampleAssistantV2.OnMessage4()", "response: {0}", response.Output.Generic[0].Text);
_messageTested4 = true;
}

private void OnCreateSession(SessionResponse response, Dictionary<string, object> customData)
Expand Down
16 changes: 5 additions & 11 deletions Scripts/Services/Assistant/v2/Assistant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,20 +397,14 @@ public bool Message(SuccessCallback<MessageResponse> successCallback, FailCallba
}
}

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

string stringToSend = stringBuilder.ToString();

req.Send = Encoding.UTF8.GetBytes(stringToSend);
req.Headers["Content-Type"] = "application/json";
req.Parameters["version"] = VersionDate;
req.OnResponse = OnMessageResponse;
Expand Down
69 changes: 68 additions & 1 deletion Scripts/Services/Assistant/v2/Models/MessageInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
*/
using FullSerializer;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

Expand All @@ -23,7 +24,7 @@ namespace IBM.WatsonDeveloperCloud.Assistant.v2
/// <summary>
/// The user input.
/// </summary>
[fsObject]
[fsObject(Converter = typeof(MessageInputConverter))]
public class MessageInput
{
/// <summary>
Expand Down Expand Up @@ -73,4 +74,70 @@ public enum MessageTypeEnum
public string SuggestionId { get; set; }
}

#region Message Input Converter
public class MessageInputConverter : fsConverter
{
private fsSerializer _serializer = new fsSerializer();

public override bool CanProcess(Type type)
{
return type == typeof(MessageInput);
}

public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
{
throw new NotImplementedException();
}

public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
{
MessageInput messageInput = (MessageInput)instance;
serialized = null;

Dictionary<string, fsData> serialization = new Dictionary<string, fsData>();

fsData tempData = null;

if (messageInput.MessageType != null)
{
_serializer.TrySerialize(messageInput.MessageType, out tempData);
serialization.Add("message_type", tempData);
}

if (messageInput.Text != null)
{
_serializer.TrySerialize(messageInput.Text, out tempData);
serialization.Add("text", tempData);
}

if (messageInput.Options != null)
{
_serializer.TrySerialize(messageInput.Options, out tempData);
serialization.Add("options", tempData);
}

if (messageInput.Intents != null)
{
_serializer.TrySerialize(messageInput.Intents, out tempData);
serialization.Add("intents", tempData);
}

if (messageInput.Entities != null)
{
_serializer.TrySerialize(messageInput.Entities, out tempData);
serialization.Add("entities", tempData);
}

if (messageInput.SuggestionId != null)
{
_serializer.TrySerialize(messageInput.SuggestionId, out tempData);
serialization.Add("suggestion_id", tempData);
}

serialized = new fsData(serialization);

return fsResult.Success;
}
#endregion
}
}
58 changes: 57 additions & 1 deletion Scripts/Services/Assistant/v2/Models/MessageInputOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*/

using FullSerializer;
using System;
using System.Collections.Generic;

namespace IBM.WatsonDeveloperCloud.Assistant.v2
{
/// <summary>
/// Optional properties that control how the assistant responds.
/// </summary>
[fsObject]
[fsObject(Converter = typeof(MessageInputOptionsConverter))]
public class MessageInputOptions
{
/// <summary>
Expand Down Expand Up @@ -50,4 +52,58 @@ public class MessageInputOptions
public bool? ReturnContext { get; set; }
}

#region Create Value Converter
public class MessageInputOptionsConverter : fsConverter
{
private fsSerializer _serializer = new fsSerializer();

public override bool CanProcess(Type type)
{
return type == typeof(MessageInputOptions);
}

public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
{
throw new NotImplementedException();
}

public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
{
MessageInputOptions messageInputOptions = (MessageInputOptions)instance;
serialized = null;

Dictionary<string, fsData> serialization = new Dictionary<string, fsData>();

fsData tempData = null;

if (messageInputOptions.Debug != null)
{
_serializer.TrySerialize(messageInputOptions.Debug, out tempData);
serialization.Add("debug", tempData);
}

if (messageInputOptions.Restart != null)
{
_serializer.TrySerialize(messageInputOptions.Restart, out tempData);
serialization.Add("restart", tempData);
}

if (messageInputOptions.AlternateIntents != null)
{
_serializer.TrySerialize(messageInputOptions.AlternateIntents, out tempData);
serialization.Add("alternate_intents", tempData);
}

if (messageInputOptions.ReturnContext != null)
{
_serializer.TrySerialize(messageInputOptions.ReturnContext, out tempData);
serialization.Add("return_context", tempData);
}

serialized = new fsData(serialization);

return fsResult.Success;
}
#endregion
}
}
Loading