From e339870e7691960f0e31625f556336278931657c Mon Sep 17 00:00:00 2001 From: herfost <133514023+sbadung@users.noreply.github.com> Date: Mon, 31 Jul 2023 16:42:18 +0200 Subject: [PATCH] codebase refactor - Removed wrapper classes - Abstracted Logger, Utility, Configuration, Telegram Bot, Bot Functionalities --- .vscode/settings.json | 3 ++ Moderation/Main/Lib_CSharp | 2 +- Moderation/Main/src/Program.cs | 77 +++++++++++++--------------------- 3 files changed, 34 insertions(+), 48 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..33b6914 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "dotnet.defaultSolution": "Moderation/Moderation.sln" +} \ No newline at end of file diff --git a/Moderation/Main/Lib_CSharp b/Moderation/Main/Lib_CSharp index f512d8b..5166e43 160000 --- a/Moderation/Main/Lib_CSharp +++ b/Moderation/Main/Lib_CSharp @@ -1 +1 @@ -Subproject commit f512d8ba2d933b9491dc55f39299413675a6cf53 +Subproject commit 5166e4308a6af71765480b25935807e6c811a97f diff --git a/Moderation/Main/src/Program.cs b/Moderation/Main/src/Program.cs index ed52e22..158b7b0 100644 --- a/Moderation/Main/src/Program.cs +++ b/Moderation/Main/src/Program.cs @@ -1,72 +1,55 @@ -#region - -using PoliNetwork.Core.Data; using PoliNetwork.Core.Utils; -using PoliNetwork.Core.Utils.LoggerNS; +using PoliNetwork.Telegram.Logger; using PoliNetwork.Db.Utils; using PoliNetwork.Telegram.Objects.Bot; -using PoliNetwork.Telegram.Objects.Configuration; -using PoliNetwork.Telegram.Objects.Updates.Update; -using PoliNetwork.Telegram.Utils; -using PoliNetwork.Telegram.Utils.ConfigUtils; using PoliNetwork.Telegram.Variables; - -#endregion +using Telegram.Bot.Types; +using PoliNetwork.Telegram.Utils.ConfigUtils; +using PoliNetwork.Telegram.Objects.Configuration; +using PoliNetwork.Db.Objects; namespace Moderation; internal static class Program { - /// - /// Telegram bot - /// - private static TelegramBot? _telegramBot; - - /// - /// Default log config - /// - private static readonly LogConfig LogConfig = new(); - + private static ModerationBot? bot = null; + private static readonly string botConfigurationPath = Variables.DefaultTelegramConfigurationPath; + private static readonly string databaseConfigurationPath = ""; - public static void Main(string[] args) + public static void Main() { - GlobalVariables.DefaultLogger.SetLogConfing(LogConfig); - GlobalVariables.DefaultLogger.Info("Hello, starting Moderation bot!"); - var telegramConfig = TelegramConfigUtils.LoadOrInitializeConfig(Variables.DefaultConfigPath); - if (telegramConfig == null) + /* The PoliNetwork.Core.Utils.LoggerNS.Logger must extends the AbstractLogger so a sub class will be needed + The only dependency of the PoliNetwork.Core.Utils.LoggerNS.Logger resides here in the Program.cs */ + AbstractLogger? logger = new DefaultLogger(); + TelegramConfigurationUtility.FileConfigurationLoader fileConfigurationLoader = new TelegramConfigurationUtility.JSONFileConfigurationLoader(); + AbstractTelegramConfiguration? botConfiguration = fileConfigurationLoader.LoadOrInitializeConfig(botConfigurationPath); + + DbConfig? databaseConfiguration = DbConfigUtils.LoadOrInitializeConfig(databaseConfigurationPath); + if (botConfiguration == null) { - GlobalVariables.DefaultLogger.Emergency("Telegram Config is undefined when starting the bot."); + logger?.Emergency("Telegram Config is undefined when starting the bot."); return; } - telegramConfig.UpdateMethod = new UpdateMethod(UpdateAction); - - var dbConfig = DbConfigUtils.LoadOrInitializeConfig(PoliNetwork.Db.Variables.Variables.DefaultConfigPath); - if (dbConfig == null) + if (databaseConfiguration == null) { - GlobalVariables.DefaultLogger.Emergency("Database Config is undefined when starting the bot."); + logger?.Emergency("Database Config is undefined when starting the bot."); return; } - _telegramBot = new TelegramBot(telegramConfig, LogConfig); - _telegramBot.Start(new CancellationToken()); + logger?.Info("RUNNING MODERATION BOT"); + botConfiguration.UpdateMethod = new UpdateMethod(UpdateAction); + + /* TelegramConfiguration should extends TelegramBotClientOptions */ + bot = new ModerationBot(options: botConfiguration, logger: logger); + bot.Start(new CancellationToken()); Wait.WaitForever(); } - private static void UpdateAction(CancellationToken arg1, IUpdate arg2) + private static void UpdateAction(CancellationToken cancellationToken, Update update) { - if (_telegramBot == null) - return; - - - // Only process Message updates: https://core.telegram.org/bots/api#message - if (arg2.Message is not { } message) - return; - - //Simply handle every message update with the "echo" method - Echo.EchoMethod( - message, - _telegramBot, //we actually pass our bot object, not the one received from the caller - arg1); + /* Process Message updates only, see: https://core.telegram.org/bots/api#message */ + if (bot == null || update.Message is not { } message) return; + bot.Echo(message, cancellationToken); } } \ No newline at end of file