You can now add CleverBotApiKey from cleverbot.com/api in order to use official cleverbot, instead of stupid program-o
This commit is contained in:
@ -4,19 +4,19 @@ using NadekoBot.Services;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using NadekoBot.Common.Attributes;
|
||||
using NadekoBot.Modules.Games.Common;
|
||||
using NadekoBot.Modules.Games.Services;
|
||||
using NadekoBot.Modules.Games.Common.ChatterBot;
|
||||
|
||||
namespace NadekoBot.Modules.Games
|
||||
{
|
||||
public partial class Games
|
||||
{
|
||||
[Group]
|
||||
public class CleverBotCommands : NadekoSubmodule<ChatterBotService>
|
||||
public class ChatterBotCommands : NadekoSubmodule<ChatterBotService>
|
||||
{
|
||||
private readonly DbService _db;
|
||||
|
||||
public CleverBotCommands(DbService db)
|
||||
public ChatterBotCommands(DbService db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
@ -28,7 +28,7 @@ namespace NadekoBot.Modules.Games
|
||||
{
|
||||
var channel = (ITextChannel)Context.Channel;
|
||||
|
||||
if (_service.ChatterBotGuilds.TryRemove(channel.Guild.Id, out Lazy<ChatterBotSession> throwaway))
|
||||
if (_service.ChatterBotGuilds.TryRemove(channel.Guild.Id, out Lazy<IChatterBotSession> throwaway))
|
||||
{
|
||||
using (var uow = _db.UnitOfWork)
|
||||
{
|
||||
@ -39,7 +39,7 @@ namespace NadekoBot.Modules.Games
|
||||
return;
|
||||
}
|
||||
|
||||
_service.ChatterBotGuilds.TryAdd(channel.Guild.Id, new Lazy<ChatterBotSession>(() => new ChatterBotSession(Context.Guild.Id), true));
|
||||
_service.ChatterBotGuilds.TryAdd(channel.Guild.Id, new Lazy<IChatterBotSession>(() => _service.CreateSession(), true));
|
||||
|
||||
using (var uow = _db.UnitOfWork)
|
||||
{
|
||||
|
@ -1,4 +1,4 @@
|
||||
namespace NadekoBot.Modules.Games.Common
|
||||
namespace NadekoBot.Modules.Games.Common.ChatterBot
|
||||
{
|
||||
public class ChatterBotResponse
|
||||
{
|
@ -4,25 +4,24 @@ using NadekoBot.Common;
|
||||
using NadekoBot.Extensions;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Modules.Games.Common
|
||||
namespace NadekoBot.Modules.Games.Common.ChatterBot
|
||||
{
|
||||
public class ChatterBotSession
|
||||
public class ChatterBotSession : IChatterBotSession
|
||||
{
|
||||
private static NadekoRandom rng { get; } = new NadekoRandom();
|
||||
public string ChatterbotId { get; }
|
||||
public string ChannelId { get; }
|
||||
private static NadekoRandom Rng { get; } = new NadekoRandom();
|
||||
|
||||
private readonly string _chatterBotId;
|
||||
private int _botId = 6;
|
||||
|
||||
public ChatterBotSession(ulong channelId)
|
||||
public ChatterBotSession()
|
||||
{
|
||||
ChannelId = channelId.ToString().ToBase64();
|
||||
ChatterbotId = rng.Next(0, 1000000).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}&" +
|
||||
$"convo_id=nadekobot_{_chatterBotId}&" +
|
||||
"format=json";
|
||||
|
||||
public async Task<string> Think(string message)
|
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NadekoBot.Modules.Games.Common.ChatterBot
|
||||
{
|
||||
public class CleverbotResponse
|
||||
{
|
||||
public string Cs { get; set; }
|
||||
public string Output { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NadekoBot.Modules.Games.Common.ChatterBot
|
||||
{
|
||||
public interface IChatterBotSession
|
||||
{
|
||||
Task<string> Think(string input);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NadekoBot.Modules.Games.Common.ChatterBot
|
||||
{
|
||||
public class OfficialCleverbotSession : IChatterBotSession
|
||||
{
|
||||
private readonly string _apiKey;
|
||||
private string _cs = null;
|
||||
|
||||
private string queryString => $"https://www.cleverbot.com/getreply?key={_apiKey}" +
|
||||
"&wrapper=nadekobot" +
|
||||
"&input={0}" +
|
||||
"&cs={1}";
|
||||
|
||||
public OfficialCleverbotSession(string apiKey)
|
||||
{
|
||||
this._apiKey = apiKey;
|
||||
}
|
||||
|
||||
public async Task<string> Think(string input)
|
||||
{
|
||||
using (var http = new HttpClient())
|
||||
{
|
||||
var dataString = await http.GetStringAsync(string.Format(queryString, input, _cs ?? "")).ConfigureAwait(false);
|
||||
var data = JsonConvert.DeserializeObject<CleverbotResponse>(dataString);
|
||||
_cs = data?.Cs;
|
||||
return data?.Output;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -7,13 +7,13 @@ using Discord;
|
||||
using Discord.WebSocket;
|
||||
using NadekoBot.Common.ModuleBehaviors;
|
||||
using NadekoBot.Extensions;
|
||||
using NadekoBot.Modules.Games.Common;
|
||||
using NadekoBot.Modules.Permissions.Common;
|
||||
using NadekoBot.Modules.Permissions.Services;
|
||||
using NadekoBot.Services;
|
||||
using NadekoBot.Services.Database.Models;
|
||||
using NadekoBot.Services.Impl;
|
||||
using NLog;
|
||||
using NadekoBot.Modules.Games.Common.ChatterBot;
|
||||
|
||||
namespace NadekoBot.Modules.Games.Services
|
||||
{
|
||||
@ -24,24 +24,34 @@ namespace NadekoBot.Modules.Games.Services
|
||||
private readonly PermissionService _perms;
|
||||
private readonly CommandHandler _cmd;
|
||||
private readonly NadekoStrings _strings;
|
||||
private readonly IBotCredentials _creds;
|
||||
|
||||
public ConcurrentDictionary<ulong, Lazy<ChatterBotSession>> ChatterBotGuilds { get; }
|
||||
public ConcurrentDictionary<ulong, Lazy<IChatterBotSession>> ChatterBotGuilds { get; }
|
||||
|
||||
public ChatterBotService(DiscordSocketClient client, PermissionService perms, IEnumerable<GuildConfig> gcs,
|
||||
CommandHandler cmd, NadekoStrings strings)
|
||||
CommandHandler cmd, NadekoStrings strings, IBotCredentials creds)
|
||||
{
|
||||
_client = client;
|
||||
_log = LogManager.GetCurrentClassLogger();
|
||||
_perms = perms;
|
||||
_cmd = cmd;
|
||||
_strings = strings;
|
||||
_creds = creds;
|
||||
|
||||
ChatterBotGuilds = new ConcurrentDictionary<ulong, Lazy<ChatterBotSession>>(
|
||||
ChatterBotGuilds = new ConcurrentDictionary<ulong, Lazy<IChatterBotSession>>(
|
||||
gcs.Where(gc => gc.CleverbotEnabled)
|
||||
.ToDictionary(gc => gc.GuildId, gc => new Lazy<ChatterBotSession>(() => new ChatterBotSession(gc.GuildId), true)));
|
||||
.ToDictionary(gc => gc.GuildId, gc => new Lazy<IChatterBotSession>(() => CreateSession(), true)));
|
||||
}
|
||||
|
||||
public string PrepareMessage(IUserMessage msg, out ChatterBotSession cleverbot)
|
||||
public IChatterBotSession CreateSession()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_creds.CleverbotApiKey))
|
||||
return new ChatterBotSession();
|
||||
else
|
||||
return new OfficialCleverbotSession(_creds.CleverbotApiKey);
|
||||
}
|
||||
|
||||
public string PrepareMessage(IUserMessage msg, out IChatterBotSession cleverbot)
|
||||
{
|
||||
var channel = msg.Channel as ITextChannel;
|
||||
cleverbot = null;
|
||||
@ -49,7 +59,7 @@ namespace NadekoBot.Modules.Games.Services
|
||||
if (channel == null)
|
||||
return null;
|
||||
|
||||
if (!ChatterBotGuilds.TryGetValue(channel.Guild.Id, out Lazy<ChatterBotSession> lazyCleverbot))
|
||||
if (!ChatterBotGuilds.TryGetValue(channel.Guild.Id, out Lazy<IChatterBotSession> lazyCleverbot))
|
||||
return null;
|
||||
|
||||
cleverbot = lazyCleverbot.Value;
|
||||
@ -74,7 +84,7 @@ namespace NadekoBot.Modules.Games.Services
|
||||
return message;
|
||||
}
|
||||
|
||||
public async Task<bool> TryAsk(ChatterBotSession cleverbot, ITextChannel channel, string message)
|
||||
public async Task<bool> TryAsk(IChatterBotSession cleverbot, ITextChannel channel, string message)
|
||||
{
|
||||
await channel.TriggerTypingAsync().ConfigureAwait(false);
|
||||
|
||||
@ -96,7 +106,7 @@ namespace NadekoBot.Modules.Games.Services
|
||||
return false;
|
||||
try
|
||||
{
|
||||
var message = PrepareMessage(usrMsg, out ChatterBotSession cbs);
|
||||
var message = PrepareMessage(usrMsg, out IChatterBotSession cbs);
|
||||
if (message == null || cbs == null)
|
||||
return false;
|
||||
|
||||
|
@ -23,6 +23,7 @@ namespace NadekoBot.Services
|
||||
string ShardRunCommand { get; }
|
||||
string ShardRunArguments { get; }
|
||||
string PatreonCampaignId { get; }
|
||||
string CleverbotApiKey { get; }
|
||||
}
|
||||
|
||||
public class DBConfig
|
||||
|
@ -26,6 +26,7 @@ namespace NadekoBot.Services.Impl
|
||||
|
||||
public string LoLApiKey { get; }
|
||||
public string OsuApiKey { get; }
|
||||
public string CleverbotApiKey { get; }
|
||||
|
||||
public DBConfig Db { get; }
|
||||
public int TotalShards { get; }
|
||||
@ -121,6 +122,7 @@ namespace NadekoBot.Services.Impl
|
||||
public string MashapeKey { get; set; } = "";
|
||||
public string OsuApiKey { get; set; } = "";
|
||||
public string SoundCloudClientId { get; set; } = "";
|
||||
public string CleverbotApiKey { get; } = "";
|
||||
public string CarbonKey { get; set; } = "";
|
||||
public DBConfig Db { get; set; } = new DBConfig("sqlite", "Filename=./data/NadekoBot.db");
|
||||
public int TotalShards { get; set; } = 1;
|
||||
|
@ -9,6 +9,7 @@
|
||||
"MashapeKey": "",
|
||||
"OsuApiKey": "",
|
||||
"SoundCloudClientId": "",
|
||||
"CleverbotApiKey": "",
|
||||
"CarbonKey": "",
|
||||
"Db": {
|
||||
"Type": "sqlite",
|
||||
|
Reference in New Issue
Block a user