Skip to content

Commit

Permalink
Fixing build and project
Browse files Browse the repository at this point in the history
  • Loading branch information
sbadung committed Sep 9, 2023
1 parent afb6f59 commit 0701679
Show file tree
Hide file tree
Showing 18 changed files with 395 additions and 60 deletions.
28 changes: 16 additions & 12 deletions Moderation/Moderation/Moderation.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -15,24 +14,29 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="Lib_CSharp\PoliNetwork.Core\src\PoliNetwork.Core.csproj"/>
<ProjectReference Include="Lib_CSharp\PoliNetwork.Db\src\PoliNetwork.Db.csproj"/>
<ProjectReference Include="Lib_CSharp\PoliNetwork.Telegram\src\PoliNetwork.Telegram.csproj"/>
<ProjectReference Include="Lib_CSharp\PoliNetwork.Core\src\PoliNetwork.Core.csproj" />
<ProjectReference Include="Lib_CSharp\PoliNetwork.Db\src\PoliNetwork.Db.csproj" />
<ProjectReference Include="Lib_CSharp\PoliNetwork.Telegram\src\PoliNetwork.Telegram.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Lib_CSharp\**" />
<Compile Remove="static\**" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Lib_CSharp\**"/>
<Compile Remove="static\**"/>
<EmbeddedResource Remove="Lib_CSharp\**" />
<EmbeddedResource Remove="static\**" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Remove="Lib_CSharp\**"/>
<EmbeddedResource Remove="static\**"/>
<None Remove="Lib_CSharp\**" />
<None Remove="static\**" />
<None Remove="Dockerfile" />
</ItemGroup>

<ItemGroup>
<None Remove="Lib_CSharp\**"/>
<None Remove="static\**"/>
<None Remove="Dockerfile"/>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Telegram.Bot;
using Telegram.Bot.Types;

namespace PoliNetwork.Telegram.Bot.Functionality
{
/// <summary>
/// Represents an abstract base class for defining the functionality of a Telegram bot.
/// </summary>
public abstract class AbstractTelegramBotFunctionality : ITelegramBotFunctionality
{
/// <summary>
/// Defines the behavior of the bot's functionality. This method must be implemented by derived classes.
/// </summary>
/// <param name="bot">The <see cref="TelegramBot"/> instance representing the bot with which the functionality is associated.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public abstract Task RunAsync(ITelegramBotClient bot, Update update, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using PoliNetwork.Utility.Configuration;
using Telegram.Bot;
using Telegram.Bot.Types;
using PoliNetwork.Utility.MessageContentControl;

namespace PoliNetwork.Telegram.Bot.Functionality.Example
{
public class ResponseOnMessageFunctionality : AbstractTelegramBotFunctionality
{
private static string[] CreateForbiddenWordsArray()
{
var path = Config.FORBIDDEN_WORDS;

Check warning on line 12 in Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs#L11-L12

Added lines #L11 - L12 were not covered by tests
return JSONConverter.GetArrayFromFile(path) ?? Array.Empty<string>();
}

Check warning on line 14 in Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs#L14

Added line #L14 was not covered by tests

public override async Task RunAsync(ITelegramBotClient bot, Update update, CancellationToken cancellationToken)
{

Check warning on line 17 in Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs#L17

Added line #L17 was not covered by tests
/* This way we only process the message text part */
if (update.Message is not { } message) return;
if (message.Text is not { } messageText) return;

var chatId = message.Chat.Id;
var username = message.Chat.Username;

Check warning on line 23 in Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs#L22-L23

Added lines #L22 - L23 were not covered by tests

var forbiddenWords = CreateForbiddenWordsArray();
var HasForbiddenWord = new ForbiddenWordsController(forbiddenWords).ContainsForbiddenWord(messageText);

Check warning on line 26 in Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs#L25-L26

Added lines #L25 - L26 were not covered by tests

var serverPrompt = $"Received a '{messageText}' message in chat {chatId} ({username}).";

Check warning on line 28 in Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs#L28

Added line #L28 was not covered by tests
var responseText = HasForbiddenWord ? "Your massage contains a forbidden word" : "Your message is a valid one";
Console.WriteLine(serverPrompt);

Check warning on line 30 in Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs#L30

Added line #L30 was not covered by tests

var sentMessage = await bot.SendTextMessageAsync(
chatId: chatId,
text: responseText,
cancellationToken: cancellationToken
);

Check warning on line 36 in Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs#L32-L36

Added lines #L32 - L36 were not covered by tests

serverPrompt = $"Sent message: {sentMessage.Text}";
Console.WriteLine(serverPrompt);
}

Check warning on line 40 in Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Functionality/Example/ResponseOnMessageFunctionality.cs#L38-L40

Added lines #L38 - L40 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Telegram.Bot;
using Telegram.Bot.Types;

namespace PoliNetwork.Telegram.Bot.Functionality
{
/// <summary>
/// Represents an interface for defining the functionality of a Telegram bot.
/// </summary>
public interface ITelegramBotFunctionality
{
/// <summary>
/// Runs the defined functionality using the provided <see cref="TelegramBot"/>.
/// </summary>
/// <param name="bot">The <see cref="TelegramBot"/> instance representing the bot with which the functionality is associated.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task RunAsync(ITelegramBotClient bot, Update update, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Telegram.Bot;
using Telegram.Bot.Exceptions;

namespace PoliNetwork.Telegram.Bot.Handler
{
public class DefaultPollingErrorHandler : IPollingErrorHandler
{
public Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{

Check warning on line 9 in Moderation/Moderation/src/Bot/Handler/Default/DefaultPollingErrorHandler.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Handler/Default/DefaultPollingErrorHandler.cs#L9

Added line #L9 was not covered by tests
var ErrorMessage = exception switch
{
ApiRequestException apiRequestException
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};

Check warning on line 15 in Moderation/Moderation/src/Bot/Handler/Default/DefaultPollingErrorHandler.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Handler/Default/DefaultPollingErrorHandler.cs#L11-L15

Added lines #L11 - L15 were not covered by tests

Console.WriteLine(ErrorMessage);
return Task.CompletedTask;
}

Check warning on line 19 in Moderation/Moderation/src/Bot/Handler/Default/DefaultPollingErrorHandler.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Handler/Default/DefaultPollingErrorHandler.cs#L17-L19

Added lines #L17 - L19 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using PoliNetwork.Telegram.Bot.Functionality;
using Telegram.Bot;
using Telegram.Bot.Types;

namespace PoliNetwork.Telegram.Bot.Handler
{
public class DefaultUpdateHandler : IUpdateHandler
{
private readonly List<ITelegramBotFunctionality> Functionalities;

public DefaultUpdateHandler(List<ITelegramBotFunctionality> functionalities)
{
Functionalities = functionalities;
}

Check warning on line 14 in Moderation/Moderation/src/Bot/Handler/Default/DefaultUpdateHandler.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Handler/Default/DefaultUpdateHandler.cs#L11-L14

Added lines #L11 - L14 were not covered by tests

public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{

Check warning on line 17 in Moderation/Moderation/src/Bot/Handler/Default/DefaultUpdateHandler.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Handler/Default/DefaultUpdateHandler.cs#L17

Added line #L17 was not covered by tests
/* Parallel.ForEach(_functionalities, functionalities => functionalities.Run(botClient, update, cancellationToken)); */
foreach (var functionality in Functionalities)
{
await functionality.RunAsync(botClient, update, cancellationToken);
}
}

Check warning on line 23 in Moderation/Moderation/src/Bot/Handler/Default/DefaultUpdateHandler.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/Handler/Default/DefaultUpdateHandler.cs#L20-L23

Added lines #L20 - L23 were not covered by tests
}
}
22 changes: 22 additions & 0 deletions Moderation/Moderation/src/Bot/Handler/IPollingErrorHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot;

namespace PoliNetwork.Telegram.Bot.Handler
{
/// <summary>
/// Represents an interface for handling errors that occur during the polling process of a Telegram bot.
/// </summary>
public interface IPollingErrorHandler
{
/// <summary>
/// Handles a polling error asynchronously.
/// </summary>
/// <param name="botClient">The <see cref="ITelegramBotClient"/> instance used for interaction with the Telegram Bot API.</param>
/// <param name="exception">The <see cref="Exception"/> that occurred during the polling process.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the asynchronous operation.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken);
}
}
21 changes: 21 additions & 0 deletions Moderation/Moderation/src/Bot/Handler/IUpdateHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

using Telegram.Bot;
using Telegram.Bot.Types;

namespace PoliNetwork.Telegram.Bot.Handler
{
/// <summary>
/// Represents an interface for handling incoming Telegram updates.
/// </summary>
public interface IUpdateHandler
{
/// <summary>
/// Handles an incoming Telegram update asynchronously.
/// </summary>
/// <param name="botClient">The <see cref="ITelegramBotClient"/> instance used for interaction with the Telegram Bot API.</param>
/// <param name="update">The <see cref="Update"/> representing the incoming update to be handled.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the asynchronous operation.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken);
}
}
53 changes: 53 additions & 0 deletions Moderation/Moderation/src/Bot/TelegramBot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using PoliNetwork.Telegram.Bot.Handler;
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;

namespace PoliNetwork.Telegram.Bot
{
/// <summary>
/// Represents a Telegram bot that can interact with the Telegram Bot API.
/// </summary>
public class TelegramBot : TelegramBotClient
{
/// <summary>
/// Initializes a new instance of the <see cref="TelegramBot"/> class with the provided options and an optional HttpClient.
/// </summary>
/// <param name="options">The options specifying bot settings.</param>
/// <param name="httpClient">Optional HttpClient to be used for API requests.</param>
public TelegramBot(TelegramBotOptions options, HttpClient? httpClient = null)
: base(options, httpClient) { }

Check warning on line 20 in Moderation/Moderation/src/Bot/TelegramBot.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/TelegramBot.cs#L20

Added line #L20 was not covered by tests

/// <summary>
/// Initializes a new instance of the <see cref="TelegramBot"/> class with the provided bot token and an optional HttpClient.
/// </summary>
/// <param name="token">The authentication token of the bot.</param>
/// <param name="httpClient">Optional HttpClient to be used for API requests.</param>
public TelegramBot(string token, HttpClient? httpClient = null)
: base(token, httpClient) { }

Check warning on line 28 in Moderation/Moderation/src/Bot/TelegramBot.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/TelegramBot.cs#L28

Added line #L28 was not covered by tests

public async Task RunAsync(Handler.IUpdateHandler updateHandler, IPollingErrorHandler pollingErrorHandler)
{
using CancellationTokenSource cts = new();

Check warning on line 32 in Moderation/Moderation/src/Bot/TelegramBot.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/TelegramBot.cs#L31-L32

Added lines #L31 - L32 were not covered by tests

ReceiverOptions receiverOptions = new()
{
AllowedUpdates = Array.Empty<UpdateType>()
};

Check warning on line 37 in Moderation/Moderation/src/Bot/TelegramBot.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/TelegramBot.cs#L34-L37

Added lines #L34 - L37 were not covered by tests

this.StartReceiving(
updateHandler: updateHandler.HandleUpdateAsync,
pollingErrorHandler: pollingErrorHandler.HandlePollingErrorAsync,
receiverOptions: receiverOptions,
cancellationToken: cts.Token
);

Check warning on line 44 in Moderation/Moderation/src/Bot/TelegramBot.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/TelegramBot.cs#L39-L44

Added lines #L39 - L44 were not covered by tests

var me = await this.GetMeAsync();
Console.WriteLine($"Start listening for @{me.Username}\nExit via typing");
Console.ReadLine();
cts.Cancel();
}

Check warning on line 50 in Moderation/Moderation/src/Bot/TelegramBot.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/TelegramBot.cs#L46-L50

Added lines #L46 - L50 were not covered by tests
}
}

19 changes: 19 additions & 0 deletions Moderation/Moderation/src/Bot/TelegramBotOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Telegram.Bot;

namespace PoliNetwork.Telegram.Bot
{
/// <summary>
/// Represents options for configuring a <see cref="TelegramBot"/>.
/// </summary>
public class TelegramBotOptions : TelegramBotClientOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="TelegramBotOptions"/> class with the provided token, base URL, and test environment settings.
/// </summary>
/// <param name="token">The authentication token of the bot.</param>
/// <param name="baseUrl">Optional base URL for API requests.</param>
/// <param name="useTestEnvironment">Flag indicating whether to use the test environment.</param>
protected TelegramBotOptions(string token, string? baseUrl = null, bool useTestEnvironment = false)
: base(token, baseUrl, useTestEnvironment) { }

Check warning on line 17 in Moderation/Moderation/src/Bot/TelegramBotOptions.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Bot/TelegramBotOptions.cs#L17

Added line #L17 was not covered by tests
}
}
9 changes: 9 additions & 0 deletions Moderation/Moderation/src/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace PoliNetwork.Utility.Configuration
{
public static class Config
{
private static readonly string? BASE_PATH = Directory.GetParent(Environment.CurrentDirectory)?.FullName;
public static readonly string FORBIDDEN_WORDS = BASE_PATH + @"\Moderation\src\persistence\forbidden_words.json";
public static readonly string APP_SETTINGS = BASE_PATH + @"\Moderation\src\persistence\appsettings.json";

Check warning on line 7 in Moderation/Moderation/src/Configuration.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Configuration.cs#L6-L7

Added lines #L6 - L7 were not covered by tests
}
}
69 changes: 21 additions & 48 deletions Moderation/Moderation/src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,29 @@
#region
using PoliNetwork.Core.Utils;
using PoliNetwork.Telegram.Logger;
using PoliNetwork.Db.Utils;
using PoliNetwork.Telegram.Configuration;
using PoliNetwork.Telegram.ConfigurationLoader;
using PoliNetwork.Db.Objects;
using PoliNetwork.Telegram.Bot.Bots;
using PoliNetwork.Telegram.Options;
using PoliNetwork.Telegram.Properties;
#endregion

namespace Moderation;

internal static class Program
using PoliNetwork.Telegram.Bot;
using PoliNetwork.Telegram.Bot.Functionality;
using PoliNetwork.Telegram.Bot.Functionality.Example;
using PoliNetwork.Telegram.Bot.Handler;
using PoliNetwork.Utility.ConfigurationLoader;
using PoliNetwork.Utility.Configuration;

namespace PoliNetwork.Moderation
{
private static ModerationBot? _bot = null;
private const string DatabaseConfigurationPath = "";

public static void Main()
internal abstract class Program
{
/* 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();
FileConfigurationLoader fileConfigurationLoader = new JSONFileConfigurationLoader();
AbstractTelegramBotOptions? botConfiguration = fileConfigurationLoader.LoadOrInitializeConfig(Configuration.TELEGRAM_CONFIGURATION_PATH, logger);

DbConfig? databaseConfiguration = DbConfigUtils.LoadOrInitializeConfig(DatabaseConfigurationPath);
if (botConfiguration == null)
private static async Task Main()
{
logger?.Emergency("Telegram Config is undefined when starting the bot.");
return;
}
List<ITelegramBotFunctionality> telegramBotFunctionalities = new()
{
new ResponseOnMessageFunctionality()
};

Check warning on line 17 in Moderation/Moderation/src/Program.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Program.cs#L14-L17

Added lines #L14 - L17 were not covered by tests

if (databaseConfiguration == null)
{
logger?.Emergency("Database Config is undefined when starting the bot.");
return;
}

logger?.Info("RUNNING MODERATION BOT");
botConfiguration.UpdateMethod = new UpdateMethod(UpdateAction);
IUpdateHandler updateHandler = new DefaultUpdateHandler(telegramBotFunctionalities);
IPollingErrorHandler pollingErrorHandler = new DefaultPollingErrorHandler();

Check warning on line 20 in Moderation/Moderation/src/Program.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Program.cs#L19-L20

Added lines #L19 - L20 were not covered by tests

/* TelegramConfiguration should extends TelegramBotClientOptions */
_bot = new ModerationBot(options: botConfiguration, logger: logger);
_bot.Start(new CancellationToken());
Wait.WaitForever();
}
var botToken = new JSONConfigurationLoader().LoadConfiguration(Config.APP_SETTINGS)
.GetSection("Secrets:BotToken").Value!;
TelegramBot bot = new(botToken);

Check warning on line 24 in Moderation/Moderation/src/Program.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Program.cs#L22-L24

Added lines #L22 - L24 were not covered by tests

private static void UpdateAction(CancellationToken cancellationToken, IUpdate update)
{
/* 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);
await bot.RunAsync(updateHandler, pollingErrorHandler);
}

Check warning on line 27 in Moderation/Moderation/src/Program.cs

View check run for this annotation

Codecov / codecov/patch

Moderation/Moderation/src/Program.cs#L26-L27

Added lines #L26 - L27 were not covered by tests
}
}
Loading

0 comments on commit 0701679

Please sign in to comment.