Skip to content

Commit

Permalink
codebase refactor
Browse files Browse the repository at this point in the history
- Removed wrapper classes
- Abstracted Logger, Utility, Configuration, Telegram Bot, Bot Functionalities
  • Loading branch information
sbadung committed Jul 31, 2023
1 parent 09c8e15 commit e339870
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dotnet.defaultSolution": "Moderation/Moderation.sln"
}
2 changes: 1 addition & 1 deletion Moderation/Main/Lib_CSharp
Submodule Lib_CSharp updated 29 files
+3 −0 PoliNetwork.Telegram/.vscode/settings.json
+31 −0 PoliNetwork.Telegram/PoliNetwork.Telegram.sln
+23 −5 PoliNetwork.Telegram/README.md
+55 −0 PoliNetwork.Telegram/src/Objects/Bot/AbstractTelegramBot.cs
+10 −0 PoliNetwork.Telegram/src/Objects/Bot/Functionality/IEcho.cs
+0 −16 PoliNetwork.Telegram/src/Objects/Bot/ITelegramBotWrapper.cs
+46 −0 PoliNetwork.Telegram/src/Objects/Bot/ModerationBot.cs
+0 −142 PoliNetwork.Telegram/src/Objects/Bot/TelegramBot.cs
+26 −34 PoliNetwork.Telegram/src/Objects/Bot/TestBot.cs
+15 −0 PoliNetwork.Telegram/src/Objects/Configuration/AbstractTelegramConfiguration.cs
+6 −8 PoliNetwork.Telegram/src/Objects/Configuration/TelegramConfiguration.cs
+3 −9 PoliNetwork.Telegram/src/Objects/Configuration/UpdateMethod.cs
+29 −0 PoliNetwork.Telegram/src/Objects/Updates/Chat/BotChat.cs
+0 −42 PoliNetwork.Telegram/src/Objects/Updates/Chat/ChatFromBot.cs
+1 −1 PoliNetwork.Telegram/src/Objects/Updates/Chat/IChat.cs
+0 −0 PoliNetwork.Telegram/src/Objects/Updates/Chat/Type/ChatType.cs
+24 −0 PoliNetwork.Telegram/src/Objects/Updates/Message/BotMessage.cs
+0 −22 PoliNetwork.Telegram/src/Objects/Updates/Message/MessageFromBot.cs
+19 −0 PoliNetwork.Telegram/src/Objects/Updates/Update/BotUpdate.cs
+0 −19 PoliNetwork.Telegram/src/Objects/Updates/Update/UpdateFromBot.cs
+10 −0 PoliNetwork.Telegram/src/Objects/Updates/User/BotUser.cs
+2 −1 PoliNetwork.Telegram/src/Objects/Updates/User/IUser.cs
+0 −24 PoliNetwork.Telegram/src/Objects/Updates/User/UserFromBot.cs
+70 −0 PoliNetwork.Telegram/src/Utility/Configuration/TelegramConfigurationUtility.cs
+102 −0 PoliNetwork.Telegram/src/Utility/Logger/AbstractLogger.cs
+22 −0 PoliNetwork.Telegram/src/Utility/Logger/DefaultLogger.cs
+0 −38 PoliNetwork.Telegram/src/Utils/ConfigUtils/TelegramConfiguration.cs
+0 −40 PoliNetwork.Telegram/src/Utils/Echo.cs
+1 −1 PoliNetwork.Telegram/src/Variables/Variables.cs
77 changes: 30 additions & 47 deletions Moderation/Main/src/Program.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Telegram bot
/// </summary>
private static TelegramBot? _telegramBot;

/// <summary>
/// Default log config
/// </summary>
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);
}
}

0 comments on commit e339870

Please sign in to comment.