-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Removed wrapper classes - Abstracted Logger, Utility, Configuration, Telegram Bot, Bot Functionalities
- Loading branch information
Showing
3 changed files
with
34 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"dotnet.defaultSolution": "Moderation/Moderation.sln" | ||
} |
Submodule Lib_CSharp
updated
29 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |