Added localization addon

This commit is contained in:
gutii
2026-02-27 19:18:18 +04:00
parent e77894fde6
commit 5a3640bc9f
22 changed files with 172 additions and 61 deletions
@@ -31,7 +31,7 @@ namespace Telegrator.Handlers.Building
/// <param name="container">The handler container with execution context.</param>
/// <param name="cancellation">The cancellation token.</param>
/// <returns>A task representing the asynchronous execution.</returns>
public override Task<Result> Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation)
public override Task<Result> Execute(IHandlerContainer<TUpdate> container, CancellationToken cancellation)
=> HandlerAction.Invoke(container, cancellation);
}
}
@@ -11,7 +11,7 @@ namespace Telegrator.Handlers.Building
/// <param name="container">The handler container with execution context.</param>
/// <param name="cancellation">The cancellation token.</param>
/// <returns>A task representing the asynchronous execution.</returns>
public delegate Task<Result> AbstractHandlerAction<TUpdate>(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation) where TUpdate : class;
public delegate Task<Result> AbstractHandlerAction<TUpdate>(IHandlerContainer<TUpdate> container, CancellationToken cancellation) where TUpdate : class;
/// <summary>
/// Builder class for creating regular handlers that can process updates.
+1 -1
View File
@@ -77,7 +77,7 @@ namespace Telegrator.Handlers
int? directMessageTopicId = null,
SuggestedPostParameters? suggestedPostParameters = null,
CancellationToken cancellationToken = default)
=> await Container.Responce(
=> await Container.Response(
text, parseMode, replyParameters,
replyMarkup, linkPreviewOptions,
messageThreadId, entities,
@@ -10,12 +10,12 @@ namespace Telegrator.Handlers.Components
/// <summary>
/// Abstract handler for Telegram updates of type <typeparamref name="TUpdate"/>.
/// </summary>
public abstract class AbstractUpdateHandler<TUpdate> : UpdateHandlerBase, IHandlerContainerFactory where TUpdate : class
public abstract class AbstractUpdateHandler<TUpdate> : UpdateHandlerBase, IHandlerContainerFactory, IAbstractUpdateHandler<TUpdate> where TUpdate : class
{
/// <summary>
/// Handler container for the current update.
/// </summary>
protected IAbstractHandlerContainer<TUpdate> Container { get; private set; } = default!;
public IHandlerContainer<TUpdate> Container { get; private set; } = default!;
/// <summary>
/// Telegram Bot client associated with the current container.
@@ -64,7 +64,7 @@ namespace Telegrator.Handlers.Components
/// <returns>The created handler container.</returns>
public virtual IHandlerContainer CreateContainer(DescribedHandlerInfo handlerInfo)
{
return new AbstractHandlerContainer<TUpdate>(handlerInfo);
return new HandlerContainer<TUpdate>(handlerInfo);
}
/// <summary>
@@ -75,7 +75,7 @@ namespace Telegrator.Handlers.Components
/// <returns>A task representing the asynchronous operation.</returns>
protected override sealed async Task<Result> ExecuteInternal(IHandlerContainer container, CancellationToken cancellationToken)
{
Container = (IAbstractHandlerContainer<TUpdate>)container;
Container = (IHandlerContainer<TUpdate>)container;
return await Execute(Container, cancellationToken);
}
@@ -85,6 +85,6 @@ namespace Telegrator.Handlers.Components
/// <param name="container">The handler container.</param>
/// <param name="cancellation">Cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public abstract Task<Result> Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation);
public abstract Task<Result> Execute(IHandlerContainer<TUpdate> container, CancellationToken cancellation);
}
}
@@ -108,7 +108,7 @@ namespace Telegrator.Handlers.Components
/// <exception cref="Exception">Thrown when the awaiting provider is not of the expected type.</exception>
public override IHandlerContainer CreateContainer(DescribedHandlerInfo handlerInfo)
{
return new AbstractHandlerContainer<TUpdate>(handlerInfo);
return new HandlerContainer<TUpdate>(handlerInfo);
}
/// <summary>
@@ -117,7 +117,7 @@ namespace Telegrator.Handlers.Components
/// <param name="container">The handler container.</param>
/// <param name="cancellation">The cancellation token.</param>
/// <exception cref="Exception">Thrown when no branch method is set.</exception>
public override async Task<Result> Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation)
public override async Task<Result> Execute(IHandlerContainer<TUpdate> container, CancellationToken cancellation)
{
if (branchMethodInfo is null)
throw new Exception();
@@ -131,7 +131,7 @@ namespace Telegrator.Handlers.Components
/// </summary>
/// <param name="container">The handler container.</param>
/// <param name="methodInfo">The method to execute.</param>
protected virtual async Task<Result> BranchExecuteWrapper(IAbstractHandlerContainer<TUpdate> container, MethodInfo methodInfo)
protected virtual async Task<Result> BranchExecuteWrapper(IHandlerContainer<TUpdate> container, MethodInfo methodInfo)
{
if (methodInfo.ReturnType == typeof(void))
{
@@ -0,0 +1,20 @@
namespace Telegrator.Handlers.Components;
/// <summary>
/// Abstract handler for Telegram updates of type <typeparamref name="TUpdate"/>.
/// </summary>
public interface IAbstractUpdateHandler<TUpdate> where TUpdate : class
{
/// <summary>
/// Handler container for the current update.
/// </summary>
public IHandlerContainer<TUpdate> Container { get; }
/// <summary>
/// Abstract method to execute the update handling logic.
/// </summary>
/// <param name="container">The handler container.</param>
/// <param name="cancellation">Cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public Task<Result> Execute(IHandlerContainer<TUpdate> container, CancellationToken cancellation);
}
@@ -0,0 +1,42 @@
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
using Telegrator.Handlers.Diagnostics;
using Telegrator.MadiatorCore.Descriptors;
namespace Telegrator.Handlers.Components;
/// <summary>
/// Abstraction for update handlers, providing execution and lifetime management for Telegram updates.
/// </summary>
public interface IUpdateHandlerBase : IDisposable
{
/// <summary>
/// Gets the <see cref="UpdateType"/> that this handler processes.
/// </summary>
UpdateType HandlingUpdateType { get; }
/// <summary>
/// Gets the <see cref="HandlerLifetimeToken"/> associated with this handler instance.
/// </summary>
HandlerLifetimeToken LifetimeToken { get; }
/// <summary>
/// Executes the handler logic and marks the lifetime as ended after execution.
/// </summary>
/// <param name="described"></param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task<Result> Execute(DescribedHandlerInfo described, CancellationToken cancellationToken = default);
/// <summary>
/// Handles failed filters during handler describing.
/// Use <see cref="Result"/> to control how router should treat this fail.
/// <see cref="Result.Next"/> to silently continue decribing.
/// <see cref="Result.Fault"/> to stop\break desribing sequence.
/// </summary>
/// <param name="report"></param>
/// <param name="client"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<Result> FiltersFallback(FiltersFallbackReport report, ITelegramBotClient client, CancellationToken cancellationToken = default);
}
@@ -1,9 +1,6 @@
using System.ComponentModel;
using Telegram.Bot;
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegrator.Filters.Components;
using Telegrator.Handlers.Diagnostics;
using Telegrator.MadiatorCore.Descriptors;
@@ -12,7 +9,7 @@ namespace Telegrator.Handlers.Components
/// <summary>
/// Base class for update handlers, providing execution and lifetime management for Telegram updates.
/// </summary>
public abstract class UpdateHandlerBase(UpdateType handlingUpdateType) : IDisposable
public abstract class UpdateHandlerBase(UpdateType handlingUpdateType) : IUpdateHandlerBase
{
/// <summary>
/// Gets the <see cref="UpdateType"/> that this handler processes.
@@ -11,7 +11,7 @@ namespace Telegrator.Handlers
/// Provides access to the update, client, filters, and other execution context.
/// </summary>
/// <typeparam name="TUpdate">The type of update being handled.</typeparam>
public class AbstractHandlerContainer<TUpdate> : IAbstractHandlerContainer<TUpdate> where TUpdate : class
public class HandlerContainer<TUpdate> : IHandlerContainer<TUpdate> where TUpdate : class
{
/// <summary>
/// Gets the actual update object of type TUpdate.
@@ -34,10 +34,10 @@ namespace Telegrator.Handlers
public IAwaitingProvider AwaitingProvider { get; }
/// <summary>
/// Initializes new instance of <see cref="AbstractHandlerContainer{TUpdate}"/>
/// Initializes new instance of <see cref="HandlerContainer{TUpdate}"/>
/// </summary>
/// <param name="handlerInfo"></param>
public AbstractHandlerContainer(DescribedHandlerInfo handlerInfo)
public HandlerContainer(DescribedHandlerInfo handlerInfo)
{
ActualUpdate = handlerInfo.HandlingUpdate.GetActualUpdateObject<TUpdate>();
HandlingUpdate = handlerInfo.HandlingUpdate;
@@ -48,7 +48,7 @@ namespace Telegrator.Handlers
}
/// <summary>
/// Initializes new instance of <see cref="AbstractHandlerContainer{TUpdate}"/>
/// Initializes new instance of <see cref="HandlerContainer{TUpdate}"/>
/// </summary>
/// <param name="actualUpdate"></param>
/// <param name="handlingUpdate"></param>
@@ -56,7 +56,7 @@ namespace Telegrator.Handlers
/// <param name="extraData"></param>
/// <param name="filters"></param>
/// <param name="awaitingProvider"></param>
public AbstractHandlerContainer(TUpdate actualUpdate, Update handlingUpdate, ITelegramBotClient client, Dictionary<string, object> extraData, CompletedFiltersList filters, IAwaitingProvider awaitingProvider)
public HandlerContainer(TUpdate actualUpdate, Update handlingUpdate, ITelegramBotClient client, Dictionary<string, object> extraData, CompletedFiltersList filters, IAwaitingProvider awaitingProvider)
{
ActualUpdate = actualUpdate;
HandlingUpdate = handlingUpdate;
@@ -71,9 +71,9 @@ namespace Telegrator.Handlers
/// </summary>
/// <typeparam name="QUpdate"></typeparam>
/// <returns></returns>
public AbstractHandlerContainer<QUpdate> CreateChild<QUpdate>() where QUpdate : class
public HandlerContainer<QUpdate> CreateChild<QUpdate>() where QUpdate : class
{
return new AbstractHandlerContainer<QUpdate>(
return new HandlerContainer<QUpdate>(
HandlingUpdate.GetActualUpdateObject<QUpdate>(),
HandlingUpdate, Client, ExtraData,
CompletedFilters, AwaitingProvider);
@@ -85,9 +85,9 @@ namespace Telegrator.Handlers
/// <typeparam name="QUpdate"></typeparam>
/// <param name="other"></param>
/// <returns></returns>
public static AbstractHandlerContainer<TUpdate> From<QUpdate>(IAbstractHandlerContainer<QUpdate> other) where QUpdate : class
public static HandlerContainer<TUpdate> From<QUpdate>(IHandlerContainer<QUpdate> other) where QUpdate : class
{
return new AbstractHandlerContainer<TUpdate>(
return new HandlerContainer<TUpdate>(
other.HandlingUpdate.GetActualUpdateObject<TUpdate>(),
other.HandlingUpdate, other.Client, other.ExtraData,
other.CompletedFilters, other.AwaitingProvider);
@@ -6,7 +6,7 @@ namespace Telegrator.Handlers
/// Represents a handler container for a specific update type.
/// </summary>
/// <typeparam name="TUpdate">The type of update handled by the container.</typeparam>
public interface IAbstractHandlerContainer<TUpdate> : IHandlerContainer where TUpdate : class
public interface IHandlerContainer<TUpdate> : IHandlerContainer where TUpdate : class
{
/// <summary>
/// Gets the actual update object of type <typeparamref name="TUpdate"/>.
+7 -7
View File
@@ -26,12 +26,12 @@ namespace Telegrator.Handlers
/// <summary>
/// Handler container for the current <see cref="InlineQuery"/> update.
/// </summary>
protected IAbstractHandlerContainer<InlineQuery> QueryContainer { get; private set; } = null!;
protected IHandlerContainer<InlineQuery> QueryContainer { get; private set; } = null!;
/// <summary>
/// Handler container for the current <see cref="ChosenInlineResult"/> update.
/// </summary>
protected IAbstractHandlerContainer<ChosenInlineResult> ChosenContainer { get; private set; } = null!;
protected IHandlerContainer<ChosenInlineResult> ChosenContainer { get; private set; } = null!;
/// <summary>
/// Incoming update of type <see cref="InlineQuery"/>.
@@ -44,20 +44,20 @@ namespace Telegrator.Handlers
protected ChosenInlineResult InputChosen { get; private set; } = null!;
/// <inheritdoc/>
public override async Task<Result> Execute(IAbstractHandlerContainer<Update> container, CancellationToken cancellation)
public override async Task<Result> Execute(IHandlerContainer<Update> container, CancellationToken cancellation)
{
switch (container.HandlingUpdate.Type)
{
case UpdateType.InlineQuery:
{
QueryContainer = AbstractHandlerContainer<InlineQuery>.From(container);
QueryContainer = HandlerContainer<InlineQuery>.From(container);
InputQuery = QueryContainer.ActualUpdate;
return await Requested(QueryContainer, cancellation).ConfigureAwait(false);
}
case UpdateType.ChosenInlineResult:
{
ChosenContainer = AbstractHandlerContainer<ChosenInlineResult>.From(container);
ChosenContainer = HandlerContainer<ChosenInlineResult>.From(container);
InputChosen = ChosenContainer.ActualUpdate;
return await Chosen(ChosenContainer, cancellation).ConfigureAwait(false);
}
@@ -73,7 +73,7 @@ namespace Telegrator.Handlers
/// <param name="container"></param>
/// <param name="cancellation"></param>
/// <returns></returns>
public abstract Task<Result> Requested(IAbstractHandlerContainer<InlineQuery> container, CancellationToken cancellation);
public abstract Task<Result> Requested(IHandlerContainer<InlineQuery> container, CancellationToken cancellation);
/// <summary>
/// Executes handler logic if received update is <see cref="UpdateType.ChosenInlineResult"/>
@@ -81,7 +81,7 @@ namespace Telegrator.Handlers
/// <param name="container"></param>
/// <param name="cancellation"></param>
/// <returns></returns>
public abstract Task<Result> Chosen(IAbstractHandlerContainer<ChosenInlineResult> container, CancellationToken cancellation);
public abstract Task<Result> Chosen(IHandlerContainer<ChosenInlineResult> container, CancellationToken cancellation);
/// <summary>
/// Answers inline query
+3 -3
View File
@@ -88,7 +88,7 @@ namespace Telegrator.Handlers
/// <param name="suggestedPostParameters"></param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The sent message.</returns>
protected async Task<Message> Responce(
protected async Task<Message> Response(
string text,
ParseMode parseMode = ParseMode.None,
ReplyParameters? replyParameters = null,
@@ -104,7 +104,7 @@ namespace Telegrator.Handlers
int? directMessageTopicId = null,
SuggestedPostParameters? suggestedPostParameters = null,
CancellationToken cancellationToken = default)
=> await Container.Responce(
=> await Container.Response(
text, parseMode, replyParameters,
replyMarkup, linkPreviewOptions,
messageThreadId, entities,
@@ -197,7 +197,7 @@ namespace Telegrator.Handlers
int? directMessageTopicId = null,
SuggestedPostParameters? suggestedPostParameters = null,
CancellationToken cancellationToken = default)
=> await Container.Responce(
=> await Container.Response(
text, parseMode, replyParameters,
replyMarkup, linkPreviewOptions,
messageThreadId, entities,
+10 -10
View File
@@ -20,7 +20,7 @@ namespace Telegrator.Handlers
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task React(
this IAbstractHandlerContainer<Message> container,
this IHandlerContainer<Message> container,
ReactionType reaction,
bool isBig = false,
CancellationToken cancellationToken = default)
@@ -38,7 +38,7 @@ namespace Telegrator.Handlers
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task React(
this IAbstractHandlerContainer<Message> container,
this IHandlerContainer<Message> container,
IEnumerable<ReactionType> reactions,
bool isBig = false,
CancellationToken cancellationToken = default)
@@ -67,7 +67,7 @@ namespace Telegrator.Handlers
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The sent message.</returns>
public static async Task<Message> Reply(
this IAbstractHandlerContainer<Message> container,
this IHandlerContainer<Message> container,
string text,
ParseMode parseMode = ParseMode.None,
ReplyMarkup? replyMarkup = null,
@@ -111,8 +111,8 @@ namespace Telegrator.Handlers
/// <param name="suggestedPostParameters"></param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The sent message.</returns>
public static async Task<Message> Responce(
this IAbstractHandlerContainer<Message> container,
public static async Task<Message> Response(
this IHandlerContainer<Message> container,
string text,
ParseMode parseMode = ParseMode.None,
ReplyParameters? replyParameters = null,
@@ -158,8 +158,8 @@ namespace Telegrator.Handlers
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static async Task<Message> Responce(
this IAbstractHandlerContainer<CallbackQuery> container,
public static async Task<Message> Response(
this IHandlerContainer<CallbackQuery> container,
string text,
ParseMode parseMode = ParseMode.None,
ReplyParameters? replyParameters = null,
@@ -203,7 +203,7 @@ namespace Telegrator.Handlers
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static async Task<Message> EditMessage(
this IAbstractHandlerContainer<CallbackQuery> container,
this IHandlerContainer<CallbackQuery> container,
string text,
ParseMode parseMode = ParseMode.None,
InlineKeyboardMarkup? replyMarkup = null,
@@ -243,7 +243,7 @@ namespace Telegrator.Handlers
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task AnswerCallbackQuery(
this IAbstractHandlerContainer<CallbackQuery> container,
this IHandlerContainer<CallbackQuery> container,
string? text = null,
bool showAlert = false,
string? url = null,
@@ -269,7 +269,7 @@ namespace Telegrator.Handlers
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task AnswerInlineQuery(
this IAbstractHandlerContainer<InlineQuery> container,
this IHandlerContainer<InlineQuery> container,
IEnumerable<InlineQueryResult> results,
int? cacheTime = null,
bool isPersonal = false,