From d535d2e8f44933836f492f4485eddc31f46550e7 Mon Sep 17 00:00:00 2001 From: jetsparrow Date: Wed, 19 Dec 2018 22:42:53 +0300 Subject: [PATCH] Finish moving to Perfusion --- JetKarmaBot/Config.cs | 17 ++++++---- JetKarmaBot/JetKarmaBot.cs | 65 ++++++++++++++++++++------------------ JetKarmaBot/Program.cs | 61 +++++++++++++++++++++++++++-------- perfusion | 2 +- 4 files changed, 94 insertions(+), 51 deletions(-) diff --git a/JetKarmaBot/Config.cs b/JetKarmaBot/Config.cs index 87898d8..68d2e0f 100644 --- a/JetKarmaBot/Config.cs +++ b/JetKarmaBot/Config.cs @@ -1,5 +1,4 @@ -using System; -using System.IO; +using System.IO; using Newtonsoft.Json; using JsonNet.PrivateSettersContractResolvers; using Newtonsoft.Json.Linq; @@ -12,10 +11,16 @@ namespace JetKarmaBot public string ApiKey { get; private set; } public string ConnectionString { get; private set; } - public string ProxyUrl { get; private set; } - public int ProxyPort { get; private set; } - public string ProxyLogin { get; private set; } - public string ProxyPassword { get; private set; } + + public class ProxySettings + { + public string Url { get; private set; } + public int Port { get; private set; } + public string Login { get; private set; } + public string Password { get; private set; } + } + + public ProxySettings Proxy { get; private set; } } public abstract class ConfigBase diff --git a/JetKarmaBot/JetKarmaBot.cs b/JetKarmaBot/JetKarmaBot.cs index d9c8c05..d350c4a 100644 --- a/JetKarmaBot/JetKarmaBot.cs +++ b/JetKarmaBot/JetKarmaBot.cs @@ -1,11 +1,7 @@ using JetKarmaBot.Commands; using Perfusion; using System; -using System.Collections.Generic; -using System.Linq; using System.Net; -using System.Reflection; -using System.Text; using System.Threading.Tasks; using Telegram.Bot; @@ -17,41 +13,38 @@ namespace JetKarmaBot { public class JetKarmaBot : IDisposable { - public void Broadcast(string message) - { - foreach (var u in db.Chats) - client.SendTextMessageAsync(u.Value.ChatId, message); - } + [Inject(true)] Config Config { get; set; } + [Inject(true)] Container Container { get; set; } + [Inject(true)] Db Db { get; set; } - public JetKarmaBot([Inject(true)]Config cfg, [Inject(true)] Container container) + TelegramBotClient Client { get; set; } + ChatCommandRouter Commands; + User Me { get; set; } + + public async Task Init() { - var httpProxy = new WebProxy($"{cfg.ProxyUrl}:{cfg.ProxyPort}") + var httpProxy = new WebProxy($"{Config.Proxy.Url}:{Config.Proxy.Port}") { - Credentials = new NetworkCredential(cfg.ProxyLogin, cfg.ProxyPassword) + Credentials = new NetworkCredential(Config.Proxy.Login, Config.Proxy.Password) }; - var botClient = new TelegramBotClient(cfg.ApiKey, httpProxy); - container.AddInstance(botClient); - var cred = new NetworkCredential(cfg.ProxyLogin, cfg.ProxyPassword); - client = new TelegramBotClient(cfg.ApiKey, httpProxy); - me = client.GetMeAsync().Result; - InitCommands(container); - client.OnMessage += BotOnMessageReceived; - client.StartReceiving(); + + Client = new TelegramBotClient(Config.ApiKey, httpProxy); + Container.AddInstance(Client); + Me = await Client.GetMeAsync(); + + InitCommands(Container); + + Client.OnMessage += BotOnMessageReceived; + Client.StartReceiving(); } - #region IDisposable - public void Dispose() + public async Task Stop() { - client.StopReceiving(); + Dispose(); } - #endregion #region service - [Inject(true)] Db db { get; set; } - TelegramBotClient client { get; } - User me { get; } - ChatCommandRouter commands; void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs) { var message = messageEventArgs.Message; @@ -61,15 +54,25 @@ namespace JetKarmaBot string s = message.Text; long id = message.Chat.Id; long from = message.From.Id; - Task.Run(() => commands.Execute(sender, messageEventArgs)); + Task.Run(() => Commands.Execute(sender, messageEventArgs)); } + void InitCommands(Container c) { commands = new ChatCommandRouter(); - commands.Add(c.ResolveObject(new StartCommand())); - commands.Add(c.ResolveObject(new AwardCommand(me))); + Commands.Add(c.ResolveObject(new StartCommand())); + Commands.Add(c.ResolveObject(new AwardCommand(Me))); } #endregion + + #region IDisposable + + public void Dispose() + { + Client.StopReceiving(); + } + + #endregion } } diff --git a/JetKarmaBot/Program.cs b/JetKarmaBot/Program.cs index 6fe46bc..6f0796d 100644 --- a/JetKarmaBot/Program.cs +++ b/JetKarmaBot/Program.cs @@ -1,26 +1,61 @@ using System; +using System.Threading; using Perfusion; namespace JetKarmaBot { - public class App + public static class Program { - public static void Main(string[] args) + public enum ExitCode : int + { + Ok = 0, + ErrorNotStarted = 0x80, + ErrorRunning = 0x81, + ErrorException = 0x82, + ErrorInvalidCommandLine = 0x100 + }; + +#if DEBUG + public const bool Debug = true; +#else + public const bool Debug = false; +#endif + public static int Main(string[] args) { Container c = new Container(); c.AddInstance(new Config("karma.cfg.json")); - Current = c.GetInstance(typeof(App)) as App; - Console.ReadKey(); - } + c.Add(); + c.Add(); - public static App Current { get; private set; } - [Inject(true)] - public void Run(Container c) - { - Watcher = c.GetInstance(); - Console.WriteLine("JetKarmaBot started!"); - } + var bot = c.GetInstance(); - JetKarmaBot Watcher { get; set; } + try + { + bot.Init().Wait(); + Console.WriteLine("JetKarmaBot started. Press Ctrl-C to exit..."); + Environment.ExitCode = (int)ExitCode.ErrorRunning; + } + catch (Exception ex) + { + Console.WriteLine($"Exception: {ex.Message}"); + Environment.ExitCode = (int)ExitCode.ErrorException; + } + ManualResetEvent quitEvent = new ManualResetEvent(false); + try + { + Console.CancelKeyPress += (sender, eArgs) => // ctrl-c + { + eArgs.Cancel = true; + quitEvent.Set(); + }; + } + catch { } + + quitEvent.WaitOne(Timeout.Infinite); + Console.WriteLine("Waiting for exit..."); + bot?.Stop()?.Wait(); + + return (int)ExitCode.Ok; + } } } diff --git a/perfusion b/perfusion index 470af23..9a30df6 160000 --- a/perfusion +++ b/perfusion @@ -1 +1 @@ -Subproject commit 470af231e3f7cf6942907d3b50473fa04dc04caf +Subproject commit 9a30df6d6e6573c901e4f792cbd08308ba1100a9