Skip to content
This repository was archived by the owner on Mar 7, 2019. It is now read-only.

Commit f6711c6

Browse files
committed
Initial commit 👌
1 parent 299a904 commit f6711c6

File tree

9 files changed

+266
-18
lines changed

9 files changed

+266
-18
lines changed

CleverbotCOM.NET Test/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
5+
</startup>
6+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{6C98EF0B-104F-430E-ADB9-D3899EC79353}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>CleverbotCOM.NET_Test</RootNamespace>
11+
<AssemblyName>CleverbotCOM.NET Test</AssemblyName>
12+
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="App.config" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<ProjectReference Include="..\CleverbotCOM.NET\CleverbotCOM.NET.csproj">
54+
<Project>{eb77a170-e64b-4a7a-951f-a80b47bd8026}</Project>
55+
<Name>CleverbotCOM.NET</Name>
56+
</ProjectReference>
57+
</ItemGroup>
58+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
59+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
60+
Other similar extension points exist, see Microsoft.Common.targets.
61+
<Target Name="BeforeBuild">
62+
</Target>
63+
<Target Name="AfterBuild">
64+
</Target>
65+
-->
66+
</Project>

CleverbotCOM.NET Test/Program.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using CleverbotCOM.NET;
2+
using System;
3+
4+
namespace CleverbotCOM.NET_Test
5+
{
6+
class Program
7+
{
8+
private const string ApiKey = "YOUR_APIKEY_HERE";
9+
10+
static void Main(string[] args)
11+
{
12+
var cleverbot1 = new Cleverbot(ApiKey);
13+
var cleverbot2 = new Cleverbot(ApiKey);
14+
15+
var message = "Hello. Im sorry."; // Our start phrase.
16+
var requests = 0;
17+
while (requests < 100)
18+
{
19+
message = cleverbot1.Ask(message);
20+
Console.WriteLine("Cleverbot #1: " + message);
21+
22+
message = cleverbot2.Ask(message);
23+
Console.WriteLine("Cleverbot #2: " + message);
24+
25+
requests = requests + 2;
26+
}
27+
28+
Console.Write("\n\nPress any key to continue...");
29+
Console.ReadKey();
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("CleverbotCOM.NET Test")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("CleverbotCOM.NET Test")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("6c98ef0b-104f-430e-adb9-d3899ec79353")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

CleverbotCOM.NET.sln

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 14.0.25420.1
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CleverbotCOM.NET", "CleverbotCOM.NET\CleverbotCOM.NET.csproj", "{EB77A170-E64B-4A7A-951F-A80B47BD8026}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CleverbotCOM.NET Test", "CleverbotCOM.NET Test\CleverbotCOM.NET Test.csproj", "{6C98EF0B-104F-430E-ADB9-D3899EC79353}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{EB77A170-E64B-4A7A-951F-A80B47BD8026}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{EB77A170-E64B-4A7A-951F-A80B47BD8026}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{EB77A170-E64B-4A7A-951F-A80B47BD8026}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{6C98EF0B-104F-430E-ADB9-D3899EC79353}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{6C98EF0B-104F-430E-ADB9-D3899EC79353}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{6C98EF0B-104F-430E-ADB9-D3899EC79353}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{6C98EF0B-104F-430E-ADB9-D3899EC79353}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

CleverbotCOM.NET/Class1.cs

-11
This file was deleted.

CleverbotCOM.NET/Cleverbot.cs

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using Newtonsoft.Json;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Net;
5+
using System.Threading.Tasks;
6+
7+
namespace CleverbotCOM.NET
8+
{
9+
public class Cleverbot
10+
{
11+
/// <summary>
12+
/// URL used for the Cleverbot connection to the API.
13+
/// </summary>
14+
public const string ApiUrl = "http://www.Cleverbot.com/getreply";
15+
16+
/// <summary>
17+
/// Our API-key.
18+
/// </summary>
19+
private string _key { get; set; }
20+
21+
/// <summary>
22+
/// Sets the unique conversation identifier.
23+
///
24+
/// Set empty to start a new converation with Cleverbot.
25+
/// </summary>
26+
private string _conversationId { get; set; }
27+
28+
29+
/// <summary>
30+
/// Creates a connection to the api with the specified API-key.
31+
/// </summary>
32+
/// <param name="apiKey">The API-key to Cleverbot</param>
33+
public Cleverbot(string apiKey)
34+
{
35+
_key = apiKey;
36+
}
37+
38+
/// <summary>
39+
/// Sends a request to Cleverbot and returns the output as a string
40+
/// </summary>
41+
/// <param name="phrase">The text to ask Cleverbot</param>
42+
/// <returns>The returned message from Cleverbot</returns>
43+
public string Ask(string phrase)
44+
{
45+
// Create the URL to the API.
46+
string requestUrl = RequestBuilder(phrase);
47+
48+
// Send the request and get the response
49+
Dictionary<string, string> response = SendWebRequest(requestUrl).GetAwaiter().GetResult();
50+
51+
// Check if we got an error and return this.
52+
if (response.ContainsKey("error"))
53+
return "Error " + response["status"] + ": " + response["error"];
54+
55+
// Set local conversation variable.
56+
_conversationId = response["cs"];
57+
58+
return response["output"];
59+
}
60+
61+
/// <summary>
62+
/// Internal method to build the request URL.
63+
/// </summary>
64+
/// <param name="phrase">The message to ask Cleverbot</param>
65+
/// <returns>The URL-call that should be made</returns>
66+
private string RequestBuilder(string phrase)
67+
{
68+
// Cleverbot needs the phrase to be separated by plus-signs instead of spaces... I know, it's weird...
69+
phrase = phrase.Replace(" ", "+");
70+
71+
var request = ApiUrl + "?key=" + _key + "&input=" + phrase + "&wrapper=" + "MatsACleverbotComNET";
72+
73+
// If we have an ongoing conversation add the conversation ID.
74+
if (!string.IsNullOrEmpty(_conversationId))
75+
request += "&cs=" + _conversationId;
76+
77+
return request;
78+
}
79+
80+
/// <summary>
81+
/// Internal method to send request to the Cleverbot web api.
82+
/// </summary>
83+
/// <param name="url">The URL to request. Use the RequestBuider to generate.</param>
84+
private async Task<Dictionary<string, string>> SendWebRequest(string url)
85+
{
86+
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
87+
request.Method = "GET";
88+
request.ContinueTimeout = 20000;
89+
90+
// Get the response
91+
WebResponse webResponse = await request.GetResponseAsync();
92+
93+
var reader = new StreamReader(webResponse.GetResponseStream());
94+
var json = await reader.ReadToEndAsync();
95+
96+
// Destroy the response and the reader.
97+
webResponse.Dispose();
98+
reader.Dispose();
99+
100+
// Create JSON dictionary and return.
101+
return JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
102+
}
103+
}
104+
}

CleverbotCOM.NET/CleverbotCOM.NET.csproj

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
55
<MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion>
66
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
77
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
8-
<ProjectGuid>{eb77a170-e64b-4a7a-951f-a80b47bd8026}</ProjectGuid>
8+
<ProjectGuid>{EB77A170-E64B-4A7A-951F-A80B47BD8026}</ProjectGuid>
99
<OutputType>Library</OutputType>
1010
<AppDesignerFolder>Properties</AppDesignerFolder>
1111
<RootNamespace>CleverbotCOM.NET</RootNamespace>
@@ -34,11 +34,17 @@
3434
<WarningLevel>4</WarningLevel>
3535
</PropertyGroup>
3636
<ItemGroup>
37-
<!-- A reference to the entire .NET Framework is automatically included -->
37+
<Compile Include="Cleverbot.cs" />
38+
<Compile Include="Properties\AssemblyInfo.cs" />
3839
</ItemGroup>
3940
<ItemGroup>
40-
<Compile Include="Class1.cs" />
41-
<Compile Include="Properties\AssemblyInfo.cs" />
41+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
42+
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\portable-net45+win8+wpa81+wp8\Newtonsoft.Json.dll</HintPath>
43+
<Private>True</Private>
44+
</Reference>
45+
</ItemGroup>
46+
<ItemGroup>
47+
<None Include="packages.config" />
4248
</ItemGroup>
4349
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
4450
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -48,5 +54,4 @@
4854
<Target Name="AfterBuild">
4955
</Target>
5056
-->
51-
52-
</Project>
57+
</Project>

CleverbotCOM.NET/packages.config

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="portable45-net45+win8+wpa81" />
4+
</packages>

0 commit comments

Comments
 (0)