diff --git a/Examples/ServiceExamples/Scripts/ExampleConversation.cs b/Examples/ServiceExamples/Scripts/ExampleConversation.cs index e5f34b8d7..d6c0543dc 100755 --- a/Examples/ServiceExamples/Scripts/ExampleConversation.cs +++ b/Examples/ServiceExamples/Scripts/ExampleConversation.cs @@ -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); } } diff --git a/Examples/ServiceExamples/ServiceExamples.unity b/Examples/ServiceExamples/ServiceExamples.unity index ac74c0a89..ad85f0afb 100755 --- a/Examples/ServiceExamples/ServiceExamples.unity +++ b/Examples/ServiceExamples/ServiceExamples.unity @@ -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 @@ -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 diff --git a/Scripts/Services/Conversation/Conversation.cs b/Scripts/Services/Conversation/Conversation.cs index 7e1950875..29dc833c0 100755 --- a/Scripts/Services/Conversation/Conversation.cs +++ b/Scripts/Services/Conversation/Conversation.cs @@ -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 { diff --git a/Scripts/Services/Conversation/DataModels.cs b/Scripts/Services/Conversation/DataModels.cs index 81152e08e..3124de7be 100755 --- a/Scripts/Services/Conversation/DataModels.cs +++ b/Scripts/Services/Conversation/DataModels.cs @@ -127,6 +127,15 @@ public class LogMessageResponse [fsObject] public class MessageRequest { + ///// + ///// Default constructor. + ///// + //public MessageRequest() + //{ + // input = new InputData(); + // context = new Context(); + //} + /// /// The input text. /// @@ -143,9 +152,19 @@ public class MessageRequest /// /// Creates the input object and sets the InputText. /// - 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) @@ -155,6 +174,21 @@ public string inputText } } + /// + /// Gets and sets the input value and creates the InputData object if it hasn't been created. + /// + public InputData InputData + { + get { return input != null ? input : input = new InputData(); } + set + { + if (input == null) + input = new InputData(); + + input = value; + } + } + /// /// Creates the Context object and sets the conversation_id. /// @@ -169,6 +203,21 @@ public string conversationID context.conversation_id = value; } } + + /// + /// Gets and sets the context value and creates the Context object if it hasn't been created. + /// + public Context ContextData + { + get { return context != null ? context : context = new Context(); } + set + { + if (context == null) + context = new Context(); + + context = value; + } + } } #endregion @@ -191,6 +240,13 @@ public class InputData [fsObject] public class Context { + ///// + ///// Default constructor. + ///// + //public Context() + //{ + // system = new SystemResponse(); + //} /// /// The unique identifier of the conversation. /// @@ -199,7 +255,22 @@ public class Context /// Information about the dialog /// public SystemResponse system { get; set; } - } + + /// + /// Creates the SystemResponse object and sets it. + /// + public SystemResponse SystemResponse + { + get { return system != null ? system : system = new SystemResponse(); } + set + { + if (system == null) + system = new SystemResponse(); + + system = value; + } + } + } /// /// Dialog information. diff --git a/Scripts/Services/TextToSpeech/TextToSpeech.cs b/Scripts/Services/TextToSpeech/TextToSpeech.cs index ba6a51380..d86f6c392 100755 --- a/Scripts/Services/TextToSpeech/TextToSpeech.cs +++ b/Scripts/Services/TextToSpeech/TextToSpeech.cs @@ -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; diff --git a/Scripts/UnitTests/TestConversation.cs b/Scripts/UnitTests/TestConversation.cs index ca5b09d79..ff07a51ab 100755 --- a/Scripts/UnitTests/TestConversation.cs +++ b/Scripts/UnitTests/TestConversation.cs @@ -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() { @@ -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; } @@ -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; - } } diff --git a/Scripts/Utilities/Constants.cs b/Scripts/Utilities/Constants.cs index 56149ea1d..8b0d22ac8 100755 --- a/Scripts/Utilities/Constants.cs +++ b/Scripts/Utilities/Constants.cs @@ -66,7 +66,7 @@ public static class Resources public static class String { /// - public const string VERSION = "watson-developer-cloud-unity-sdk-0.9.0"; + public const string VERSION = "watson-developer-cloud-unity-sdk-0.10.0"; /// public const string DEBUG_DISPLAY_QUALITY = "Quality: {0}"; }