NadekoBot/NadekoBot.Modules.Games/Common/ChatterBot/ChatterBotSession.cs

38 lines
1.2 KiB
C#
Raw Normal View History

2017-07-17 19:42:36 +00:00
using System.Net.Http;
2017-05-27 08:19:27 +00:00
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common;
using NadekoBot.Extensions;
using Newtonsoft.Json;
2017-05-27 08:19:27 +00:00
namespace NadekoBot.Modules.Games.Common.ChatterBot
2017-05-27 08:19:27 +00:00
{
public class ChatterBotSession : IChatterBotSession
2017-05-27 08:19:27 +00:00
{
private static NadekoRandom Rng { get; } = new NadekoRandom();
private readonly string _chatterBotId;
2017-05-27 08:19:27 +00:00
private int _botId = 6;
public ChatterBotSession()
2017-05-27 08:19:27 +00:00
{
_chatterBotId = Rng.Next(0, 1000000).ToString().ToBase64();
2017-05-27 08:19:27 +00:00
}
private string apiEndpoint => "http://api.program-o.com/v2/chatbot/" +
$"?bot_id={_botId}&" +
"say={0}&" +
$"convo_id=nadekobot_{_chatterBotId}&" +
2017-05-27 08:19:27 +00:00
"format=json";
public async Task<string> Think(string message)
{
using (var http = new HttpClient())
{
var res = await http.GetStringAsync(string.Format(apiEndpoint, message)).ConfigureAwait(false);
var cbr = JsonConvert.DeserializeObject<ChatterBotResponse>(res);
return cbr.BotSay.Replace("<br/>", "\n");
}
}
}
}