From 05d6152936bc7c1b9bc4178beb4027cb6b5e0375 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Mon, 6 Mar 2017 20:13:06 +0100 Subject: [PATCH] Cleverbot is beyond stupid, but works. Not using cleverbot, but program-o.com --- .../Games/Commands/CleverBotCommands.cs | 50 ++++++++++++++++--- src/NadekoBot/Services/Impl/StatsService.cs | 2 +- src/NadekoBot/_Extensions/Extensions.cs | 6 +++ 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs b/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs index fa04a2c6..3d7fb832 100644 --- a/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs +++ b/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs @@ -5,12 +5,15 @@ using NadekoBot.Attributes; using NadekoBot.Extensions; using NadekoBot.Services; using NLog; -using Services.CleverBotApi; +//using Services.CleverBotApi; using System; using System.Collections.Concurrent; using System.Diagnostics; using System.Linq; +using System.Net.Http; using System.Threading.Tasks; +using Newtonsoft.Json; +using Services.CleverBotApi; namespace NadekoBot.Modules.Games { @@ -27,13 +30,11 @@ namespace NadekoBot.Modules.Games { _log = LogManager.GetCurrentClassLogger(); var sw = Stopwatch.StartNew(); - - var bot = ChatterBotFactory.Create(ChatterBotType.CLEVERBOT); CleverbotGuilds = new ConcurrentDictionary>( NadekoBot.AllGuildConfigs .Where(gc => gc.CleverbotEnabled) - .ToDictionary(gc => gc.GuildId, gc => new Lazy(() => bot.CreateSession(), true))); + .ToDictionary(gc => gc.GuildId, gc => new Lazy(() => new ChatterBotSession(gc.GuildId), true))); sw.Stop(); _log.Debug($"Loaded in {sw.Elapsed.TotalSeconds:F2}s"); @@ -100,9 +101,7 @@ namespace NadekoBot.Modules.Games return; } - var cleverbot = ChatterBotFactory.Create(ChatterBotType.CLEVERBOT); - - CleverbotGuilds.TryAdd(channel.Guild.Id, new Lazy(() => cleverbot.CreateSession(), true)); + CleverbotGuilds.TryAdd(channel.Guild.Id, new Lazy(() => new ChatterBotSession(Context.Guild.Id), true)); using (var uow = DbHandler.UnitOfWork()) { @@ -113,5 +112,42 @@ namespace NadekoBot.Modules.Games await ReplyConfirmLocalized("cleverbot_enabled").ConfigureAwait(false); } } + + public class ChatterBotSession + { + private static NadekoRandom rng { get; } = new NadekoRandom(); + public string ChatterbotId { get; } + public string ChannelId { get; } + private int _botId = 15; + + public ChatterBotSession(ulong channelId) + { + ChannelId = channelId.ToString().ToBase64(); + ChatterbotId = rng.Next(0, 1000000).ToString().ToBase64(); + } + + private string apiEndpoint => "http://api.program-o.com/v2/chatbot/" + + $"?bot_id={_botId}&" + + "say={0}&" + + $"convo_id=nadekobot_{ChatterbotId}_{ChannelId}&" + + "format=json"; + + public async Task Think(string message) + { + using (var http = new HttpClient()) + { + var res = await http.GetStringAsync(string.Format(apiEndpoint, message)).ConfigureAwait(false); + var cbr = JsonConvert.DeserializeObject(res); + //Console.WriteLine(cbr.Convo_id); + return cbr.BotSay.Replace("
", "\n"); + } + } + } + + public class ChatterBotResponse + { + public string Convo_id { get; set; } + public string BotSay { get; set; } + } } } \ No newline at end of file diff --git a/src/NadekoBot/Services/Impl/StatsService.cs b/src/NadekoBot/Services/Impl/StatsService.cs index 0390cbda..946bf11f 100644 --- a/src/NadekoBot/Services/Impl/StatsService.cs +++ b/src/NadekoBot/Services/Impl/StatsService.cs @@ -16,7 +16,7 @@ namespace NadekoBot.Services.Impl private readonly DiscordShardedClient _client; private readonly DateTime _started; - public const string BotVersion = "1.2"; + public const string BotVersion = "1.21"; public string Author => "Kwoth#2560"; public string Library => "Discord.Net"; diff --git a/src/NadekoBot/_Extensions/Extensions.cs b/src/NadekoBot/_Extensions/Extensions.cs index f95e6fbb..429e9c80 100644 --- a/src/NadekoBot/_Extensions/Extensions.cs +++ b/src/NadekoBot/_Extensions/Extensions.cs @@ -21,6 +21,12 @@ namespace NadekoBot.Extensions private const string arrow_left = "⬅"; private const string arrow_right = "➡"; + public static string ToBase64(this string plainText) + { + var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); + return Convert.ToBase64String(plainTextBytes); + } + public static Stream ToStream(this IEnumerable bytes, bool canWrite = false) { var ms = new MemoryStream(bytes as byte[] ?? bytes.ToArray(), canWrite);