|
| 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 | +} |
0 commit comments