Skip to content

Commit 2f6a007

Browse files
committed
abstraction of message method of conversation service, unit test and low level example
1 parent 31fd453 commit 2f6a007

File tree

5 files changed

+85
-11
lines changed

5 files changed

+85
-11
lines changed

Examples/ServiceExamples/Scripts/ExampleConversation.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@
1919
using System.Collections;
2020
using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
2121

22-
public class ExampleConversation : MonoBehaviour {
22+
public class ExampleConversation : MonoBehaviour
23+
{
2324
private Conversation m_Conversation = new Conversation();
2425
private string m_WorkspaceID = "car_demo_1";
2526
private string m_Input = "Can you unlock the door?";
2627

2728
void Start () {
2829
Debug.Log("User: " + m_Input);
29-
m_Conversation.Message(m_WorkspaceID, m_Input, HandleOnMessage);
30+
m_Conversation.Message(m_WorkspaceID, m_Input, OnMessage);
3031
}
3132

32-
void HandleOnMessage (DataModels.MessageResponse resp)
33+
void OnMessage (DataModels.MessageResponse resp)
3334
{
34-
Debug.Log("Watson: " + resp);
35+
foreach(DataModels.MessageIntent mi in resp.intents)
36+
Debug.Log("intent: " + mi.intent + ", confidence: " + mi.confidence);
37+
38+
Debug.Log("response: " + resp.output.text);
3539
}
3640
}

Examples/ServiceExamples/ServiceExamples.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Transform:
209209
m_LocalScale: {x: 1, y: 1, z: 1}
210210
m_Children: []
211211
m_Father: {fileID: 0}
212-
m_RootOrder: 5
212+
m_RootOrder: 6
213213
--- !u!1 &859102722
214214
GameObject:
215215
m_ObjectHideFlags: 0
@@ -248,7 +248,7 @@ Transform:
248248
m_LocalScale: {x: 1, y: 1, z: 1}
249249
m_Children: []
250250
m_Father: {fileID: 0}
251-
m_RootOrder: 6
251+
m_RootOrder: 5
252252
--- !u!1 &1160237478
253253
GameObject:
254254
m_ObjectHideFlags: 0

Scripts/Services/Conversation/Conversation.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ namespace IBM.Watson.DeveloperCloud.Services.Conversation.v1
3030
/// This class wraps the Watson Conversation service.
3131
/// <a href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/conversation.html">Conversation Service</a>
3232
/// </summary>
33-
public class Conversation : IWatsonService {
33+
public class Conversation : IWatsonService
34+
{
3435
#region Public Types
3536
/// <summary>
3637
/// The callback for Message().
@@ -68,19 +69,19 @@ public bool Message(string workspaceId, string input, OnMessage callback)
6869
if(callback == null)
6970
throw new ArgumentNullException("callback");
7071

71-
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, "v2/rest/workspaces");
72+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, "/v2/rest/workspaces");
7273
if(connector == null)
7374
return false;
7475

75-
string reqJson = "{\"input\": {\"text\": \"{0}\"}}";
76+
string reqJson = "{{\"input\": {{\"text\": \"{0}\"}}}}";
77+
string reqString = string.Format(reqJson, input);
7678

7779
MessageReq req = new MessageReq();
7880
req.Callback = callback;
7981
req.Headers["Content-Type"] = "application/json";
8082
req.Function = "/" + workspaceId + "/message";
83+
req.Send = Encoding.UTF8.GetBytes(reqString);
8184
req.OnResponse = MessageResp;
82-
req.Forms = new Dictionary<string, RESTConnector.Form>();
83-
req.Forms["input"] = new RESTConnector.Form(input);
8485

8586
return connector.Send(req);
8687
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using UnityEngine;
19+
using System.Collections;
20+
using IBM.Watson.DeveloperCloud.UnitTests;
21+
using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
22+
using IBM.Watson.DeveloperCloud.Utilities;
23+
using IBM.Watson.DeveloperCloud.Logging;
24+
25+
public class TestConversation : UnitTest
26+
{
27+
private Conversation m_Conversation = new Conversation();
28+
private string m_WorkspaceID = "car_demo_1";
29+
private string m_Input = "Can you unlock the door?";
30+
private bool m_MessageTested = false;
31+
32+
public override IEnumerator RunTest()
33+
{
34+
if (Config.Instance.FindCredentials(m_Conversation.GetServiceID()) == null)
35+
yield break;
36+
37+
if(!m_MessageTested)
38+
{
39+
m_Conversation.Message(m_WorkspaceID, m_Input, OnMessage);
40+
while(!m_MessageTested)
41+
yield return null;
42+
}
43+
}
44+
45+
private void OnMessage(DataModels.MessageResponse resp)
46+
{
47+
Test(resp != null);
48+
if(resp != null)
49+
{
50+
foreach(DataModels.MessageIntent mi in resp.intents)
51+
Log.Debug("TestConversation", "intent: " + mi.intent + ", confidence: " + mi.confidence);
52+
Log.Debug("TestConversation", "response: " + resp.output.text);
53+
}
54+
55+
m_MessageTested = true;
56+
}
57+
}

Scripts/UnitTests/TestConversation.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)