Skip to content

Fixing issue with error on sending message with conversation ID and e… #153

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
Sep 23, 2016
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
71 changes: 46 additions & 25 deletions Examples/ServiceExamples/Scripts/ExampleConversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,69 @@
*/

using UnityEngine;
using System.Collections;
using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
using IBM.Watson.DeveloperCloud.Utilities;
using IBM.Watson.DeveloperCloud.Logging;
using System;

public class ExampleConversation : MonoBehaviour
{
private Conversation m_Conversation = new Conversation();
private string m_WorkspaceID;
private string m_Input = "Can you unlock the door?";
private string m_ConversationID;
private bool m_UseAlternateIntents = true;
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"};

void Start () {
LogSystem.InstallDefaultReactors();
m_WorkspaceID = Config.Instance.GetVariableValue("ConversationV1_ID");
Debug.Log("User: " + m_Input);

// Message with input only
//m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input);
Debug.Log("**********User: Hello!");
MessageWithOnlyInput("Hello!");
}

// Message by creating message request
//MessageRequest messageRequest = new MessageRequest();
//messageRequest.inputText = m_Input;
//m_Conversation.Message(OnMessage, m_WorkspaceID, messageRequest);
private void MessageWithOnlyInput(string input)
{
if (string.IsNullOrEmpty(input))
throw new ArgumentNullException("input");

m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, input);
}


private void OnMessageWithOnlyInput(MessageResponse resp, string customData)
{
if (resp != null)
{
foreach (Intent mi in resp.intents)
Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);

if (resp.output != null && resp.output.text.Length > 0)
foreach (string txt in resp.output.text)
Debug.Log("output: " + txt);

m_ConversationID = resp.context.conversation_id;

string questionStr = questionArray[UnityEngine.Random.Range(0, questionArray.Length - 1)];
Debug.Log(string.Format("**********User: {0}", questionStr));

MessageRequest messageRequest = new MessageRequest();
messageRequest.InputText = questionStr;
messageRequest.alternate_intents = m_UseAlternateIntents;
messageRequest.ContextData = resp.context;

// Message by passing input, alternate intents and conversationID
m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input, false, null);
MessageWithFullMessageRequest(messageRequest);
}
else
{
Debug.Log("Failed to invoke Message();");
}
}

void OnMessage (MessageResponse resp, string customData)
private void MessageWithFullMessageRequest(MessageRequest messageRequest)
{
if(resp != null)
{
foreach(Intent mi in resp.intents)
Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);

if(resp.output != null && resp.output.text.Length > 0)
foreach(string txt in resp.output.text)
Debug.Log("output: " + txt);
}
else
{
Debug.Log("Failed to invoke Message();");
}
if (messageRequest == null)
throw new ArgumentNullException("messageRequest");
m_Conversation.Message(OnMessageWithOnlyInput, m_WorkspaceID, messageRequest);
}
}
4 changes: 2 additions & 2 deletions Examples/ServiceExamples/ServiceExamples.unity
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!114 &859102723
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -593,7 +593,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &1713392458
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
36 changes: 0 additions & 36 deletions Scripts/Services/Conversation/Conversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,42 +126,6 @@ public class Conversation : IWatsonService
return connector.Send(req);
}

public bool Message(OnMessage callback, string workspaceID, string input, bool useAlternateIntents, string conversationID = default(string), string customData = default(string))
{
if (string.IsNullOrEmpty(workspaceID))
throw new ArgumentNullException("workspaceId");
if (string.IsNullOrEmpty(input))
throw new ArgumentNullException("input");
if (callback == null)
throw new ArgumentNullException("callback");

MessageRequest messageRequest = new MessageRequest();
messageRequest.inputText = input;
messageRequest.alternate_intents = useAlternateIntents;
if (conversationID != default(string))
messageRequest.conversationID = conversationID;

RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_MESSAGE);
if (connector == null)
return false;

fsData data;
sm_Serializer.TrySerialize(messageRequest.GetType(), messageRequest, out data).AssertSuccessWithoutWarnings();
string reqString = fsJsonPrinter.CompressedJson(data);

MessageReq req = new MessageReq();
req.Callback = callback;
req.MessageRequest = messageRequest;
req.Headers["Content-Type"] = "application/json";
req.Headers["Accept"] = "application/json";
req.Parameters["version"] = Version.VERSION;
req.Function = "/" + workspaceID + "/message";
req.Data = customData;
req.Send = Encoding.UTF8.GetBytes(reqString);
req.OnResponse = MessageResp;

return connector.Send(req);
}

private class MessageReq : RESTConnector.Request
{
Expand Down
77 changes: 74 additions & 3 deletions Scripts/Services/Conversation/DataModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ public class LogMessageResponse
[fsObject]
public class MessageRequest
{
///// <summary>
///// Default constructor.
///// </summary>
//public MessageRequest()
//{
// input = new InputData();
// context = new Context();
//}

/// <summary>
/// The input text.
/// </summary>
Expand All @@ -143,9 +152,19 @@ public class MessageRequest
/// <summary>
/// Creates the input object and sets the InputText.
/// </summary>
public string inputText
public string InputText
{
get { return input != null ? input.text : null; }
get {
if (input == null)
{
input = new InputData();
return "";
}
else
{
return input.text;
}
}
set
{
if (input == null)
Expand All @@ -155,6 +174,21 @@ public string inputText
}
}

/// <summary>
/// Gets and sets the input value and creates the InputData object if it hasn't been created.
/// </summary>
public InputData InputData
{
get { return input != null ? input : input = new InputData(); }
set
{
if (input == null)
input = new InputData();

input = value;
}
}

/// <summary>
/// Creates the Context object and sets the conversation_id.
/// </summary>
Expand All @@ -169,6 +203,21 @@ public string conversationID
context.conversation_id = value;
}
}

/// <summary>
/// Gets and sets the context value and creates the Context object if it hasn't been created.
/// </summary>
public Context ContextData
{
get { return context != null ? context : context = new Context(); }
set
{
if (context == null)
context = new Context();

context = value;
}
}
}
#endregion

Expand All @@ -191,6 +240,13 @@ public class InputData
[fsObject]
public class Context
{
///// <summary>
///// Default constructor.
///// </summary>
//public Context()
//{
// system = new SystemResponse();
//}
/// <summary>
/// The unique identifier of the conversation.
/// </summary>
Expand All @@ -199,7 +255,22 @@ public class Context
/// Information about the dialog
/// </summary>
public SystemResponse system { get; set; }
}

/// <summary>
/// Creates the SystemResponse object and sets it.
/// </summary>
public SystemResponse SystemResponse
{
get { return system != null ? system : system = new SystemResponse(); }
set
{
if (system == null)
system = new SystemResponse();

system = value;
}
}
}

/// <summary>
/// Dialog information.
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Services/TextToSpeech/TextToSpeech.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ private void OnDeleteCustomizationResp(RESTConnector.Request req, RESTConnector.
if (callback == null)
throw new ArgumentNullException("callback");
if (string.IsNullOrEmpty(customizationID))
throw new ArgumentNullException("A name is customizationID to get a custom voice model.");
throw new ArgumentNullException("A customizationID to get a custom voice model.");

GetCustomizationRequest req = new GetCustomizationRequest();
req.Callback = callback;
Expand Down
27 changes: 2 additions & 25 deletions Scripts/UnitTests/TestConversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class TestConversation : UnitTest
private string m_Input = "Can you unlock the door?";
private bool m_MessageInputTested = false;
private bool m_MessageObjectTested = false;
private bool m_MessageTested = false;

public override IEnumerator RunTest()
{
Expand All @@ -48,19 +47,12 @@ public override IEnumerator RunTest()
if (!m_MessageObjectTested)
{
MessageRequest messageRequest = new MessageRequest();
messageRequest.inputText = m_Input;
messageRequest.InputText = m_Input;
m_Conversation.Message(OnMessageObject, m_WorkspaceID, messageRequest);
while (!m_MessageObjectTested)
yield return null;
}

if (!m_MessageTested)
{
m_Conversation.Message(OnMessage, m_WorkspaceID, m_Input, false);
while (!m_MessageTested)
yield return null;
}


yield break;
}

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

m_MessageObjectTested = true;
}

private void OnMessage(MessageResponse resp, string customData)
{
Test(resp != null);
if (resp != null)
{
foreach (Intent mi in resp.intents)
Log.Debug("TestConversation", "intent: " + mi.intent + ", confidence: " + mi.confidence);
if (resp.output != null && resp.output.text.Length > 0)
foreach (string txt in resp.output.text)
Debug.Log("output: " + txt);
}

m_MessageTested = true;
}
}
2 changes: 1 addition & 1 deletion Scripts/Utilities/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static class Resources
public static class String
{
/// <exclude />
public const string VERSION = "watson-developer-cloud-unity-sdk-0.9.0";
public const string VERSION = "watson-developer-cloud-unity-sdk-0.10.0";
/// <exclude />
public const string DEBUG_DISPLAY_QUALITY = "Quality: {0}";
}
Expand Down