Skip to content

Commit dfb10f8

Browse files
committed
chore(Assistant v2): Added examples
1 parent 0895585 commit dfb10f8

File tree

4 files changed

+173
-2
lines changed

4 files changed

+173
-2
lines changed

IBM.WatsonDeveloperCloud.sln

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AssistantV2", "AssistantV2"
125125
EndProject
126126
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IBM.WatsonDeveloperCloud.Assistant.v2", "src\IBM.WatsonDeveloperCloud.Assistant.v2\IBM.WatsonDeveloperCloud.Assistant.v2.csproj", "{817CCCEB-FBAF-4BEB-A99B-3EE4F09F9200}"
127127
EndProject
128-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IBM.WatsonDeveloperCloud.Assistant.v2.UnitTests", "test\IBM.WatsonDeveloperCloud.Assistant.v2.UnitTests\IBM.WatsonDeveloperCloud.Assistant.v2.UnitTests.csproj", "{5FBF32E9-0352-4D8B-BED2-D6EE04363A47}"
128+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IBM.WatsonDeveloperCloud.Assistant.v2.UnitTests", "test\IBM.WatsonDeveloperCloud.Assistant.v2.UnitTests\IBM.WatsonDeveloperCloud.Assistant.v2.UnitTests.csproj", "{5FBF32E9-0352-4D8B-BED2-D6EE04363A47}"
129129
EndProject
130-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IBM.WatsonDeveloperCloud.Assistant.v2.IntTests", "test\IBM.WatsonDeveloperCloud.Assistant.v2.IntTests\IBM.WatsonDeveloperCloud.Assistant.v2.IntTests.csproj", "{200FC8C7-ED91-4F66-8674-D678588FC450}"
130+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IBM.WatsonDeveloperCloud.Assistant.v2.IntTests", "test\IBM.WatsonDeveloperCloud.Assistant.v2.IntTests\IBM.WatsonDeveloperCloud.Assistant.v2.IntTests.csproj", "{200FC8C7-ED91-4F66-8674-D678588FC450}"
131+
EndProject
132+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IBM.WatsonDeveloperCloud.Assistant.v2.Example", "examples\IBM.WatsonDeveloperCloud.Assistant.v2.Example\IBM.WatsonDeveloperCloud.Assistant.v2.Example.csproj", "{125A4EB4-D769-4EA7-A18D-8D7D568B5B33}"
131133
EndProject
132134
Global
133135
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -319,6 +321,10 @@ Global
319321
{200FC8C7-ED91-4F66-8674-D678588FC450}.Debug|Any CPU.Build.0 = Debug|Any CPU
320322
{200FC8C7-ED91-4F66-8674-D678588FC450}.Release|Any CPU.ActiveCfg = Release|Any CPU
321323
{200FC8C7-ED91-4F66-8674-D678588FC450}.Release|Any CPU.Build.0 = Release|Any CPU
324+
{125A4EB4-D769-4EA7-A18D-8D7D568B5B33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
325+
{125A4EB4-D769-4EA7-A18D-8D7D568B5B33}.Debug|Any CPU.Build.0 = Debug|Any CPU
326+
{125A4EB4-D769-4EA7-A18D-8D7D568B5B33}.Release|Any CPU.ActiveCfg = Release|Any CPU
327+
{125A4EB4-D769-4EA7-A18D-8D7D568B5B33}.Release|Any CPU.Build.0 = Release|Any CPU
322328
EndGlobalSection
323329
GlobalSection(SolutionProperties) = preSolution
324330
HideSolutionNode = FALSE
@@ -385,6 +391,7 @@ Global
385391
{817CCCEB-FBAF-4BEB-A99B-3EE4F09F9200} = {90E09BF8-A647-43B8-B721-05CAECFADD72}
386392
{5FBF32E9-0352-4D8B-BED2-D6EE04363A47} = {90E09BF8-A647-43B8-B721-05CAECFADD72}
387393
{200FC8C7-ED91-4F66-8674-D678588FC450} = {90E09BF8-A647-43B8-B721-05CAECFADD72}
394+
{125A4EB4-D769-4EA7-A18D-8D7D568B5B33} = {90E09BF8-A647-43B8-B721-05CAECFADD72}
388395
EndGlobalSection
389396
GlobalSection(ExtensibilityGlobals) = postSolution
390397
SolutionGuid = {B9D9D17B-1C17-402F-B701-DC671528690A}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+

2+
3+
using IBM.WatsonDeveloperCloud.Assistant.v2.Model;
4+
using Newtonsoft.Json;
5+
using System;
6+
/**
7+
* Copyright 2018 IBM Corp. All Rights Reserved.
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*
21+
*/
22+
namespace IBM.WatsonDeveloperCloud.Assistant.v2.Example
23+
{
24+
internal class AssistantServiceExample
25+
{
26+
private AssistantService _assistant;
27+
private string _assistantId;
28+
private string[] _questionArray = { "", "good", "i want a pizza", "large", "three" };
29+
private int _questionIndex = 0;
30+
private string _sessionId;
31+
32+
public AssistantServiceExample(string url, string username, string password, string assistantId)
33+
{
34+
_assistant = new AssistantService(username, password, "2018-09-20");
35+
_assistant.SetEndpoint(url);
36+
37+
_assistantId = assistantId;
38+
39+
var session = _assistant.CreateSession(_assistantId);
40+
_sessionId = session.SessionId;
41+
42+
CallAssistant(_questionIndex);
43+
}
44+
45+
public void CallAssistant(int questionIndex)
46+
{
47+
MessageRequest messageRequest = new MessageRequest()
48+
{
49+
Input = new MessageInput()
50+
{
51+
Text = _questionArray[questionIndex]
52+
}
53+
};
54+
55+
Console.WriteLine(_questionArray[questionIndex]);
56+
var result = _assistant.Message(_assistantId, _sessionId, messageRequest);
57+
Console.WriteLine(result.Output.Generic[0].Text);
58+
//Console.WriteLine(string.Format("result: {0}", JsonConvert.SerializeObject(result, Formatting.Indented)));
59+
_questionIndex++;
60+
61+
if (questionIndex < _questionArray.Length - 1)
62+
{
63+
CallAssistant(_questionIndex);
64+
}
65+
else
66+
{
67+
_assistant.DeleteSession(_assistantId, _sessionId);
68+
Console.WriteLine("Session deleted!");
69+
}
70+
}
71+
}
72+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Copyright 2018 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 IBM.WatsonDeveloperCloud.Util;
19+
using Newtonsoft.Json;
20+
using Newtonsoft.Json.Linq;
21+
using System;
22+
using System.IO;
23+
24+
namespace IBM.WatsonDeveloperCloud.Assistant.v2.Example
25+
{
26+
class Example
27+
{
28+
static void Main(string[] args)
29+
{
30+
string credentials = string.Empty;
31+
32+
#region Get Credentials
33+
string _endpoint = string.Empty;
34+
string _username = string.Empty;
35+
string _password = string.Empty;
36+
string _assistantId = string.Empty;
37+
38+
if (string.IsNullOrEmpty(credentials))
39+
{
40+
var parentDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.Parent.Parent.FullName;
41+
string credentialsFilepath = parentDirectory + Path.DirectorySeparatorChar + "sdk-credentials" + Path.DirectorySeparatorChar + "credentials.json";
42+
if (File.Exists(credentialsFilepath))
43+
{
44+
try
45+
{
46+
credentials = File.ReadAllText(credentialsFilepath);
47+
credentials = Utility.AddTopLevelObjectToJson(credentials, "VCAP_SERVICES");
48+
}
49+
catch (Exception e)
50+
{
51+
throw new Exception(string.Format("Failed to load credentials: {0}", e.Message));
52+
}
53+
54+
VcapCredentials vcapCredentials = JsonConvert.DeserializeObject<VcapCredentials>(credentials);
55+
var vcapServices = JObject.Parse(credentials);
56+
57+
Credential credential = vcapCredentials.GetCredentialByname("assistantV2-sdk-staging")[0].Credentials;
58+
_endpoint = credential.Url;
59+
_username = credential.Username;
60+
_password = credential.Password;
61+
_assistantId = credential.AssistantId;
62+
}
63+
else
64+
{
65+
Console.WriteLine("Credentials file does not exist. Please define credentials.");
66+
_username = "";
67+
_password = "";
68+
_endpoint = "";
69+
_assistantId = "";
70+
}
71+
}
72+
#endregion
73+
74+
AssistantServiceExample _AssistantServiceExample = new AssistantServiceExample(_endpoint, _username, _password, _assistantId);
75+
76+
Console.ReadKey();
77+
}
78+
}
79+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\IBM.WatsonDeveloperCloud.Assistant.v2\IBM.WatsonDeveloperCloud.Assistant.v2.csproj" />
10+
<ProjectReference Include="..\..\src\IBM.WatsonDeveloperCloud\IBM.WatsonDeveloperCloud.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)