* Added new handler type "InlineQueryHandler"

* Acts as complex multi-type handler that supports InlineQuery AND ChosenInlineQuery updates
* Added "UpdateTypeExtensions.SuppressTypes" to suppress some of update types for multi-type handlers
* Logging adjustments
* Changed extension method "HasPublicProperties" to extned Type
This commit is contained in:
2025-08-12 00:09:45 +04:00
parent a1af9bdc3e
commit 3b7908756d
16 changed files with 227 additions and 54 deletions
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using Telegrator.Logging; using Telegrator.Logging;
namespace Telegrator.Hosting.Logging namespace Telegrator.Hosting.Logging
@@ -8,27 +9,27 @@ namespace Telegrator.Hosting.Logging
/// </summary> /// </summary>
public class MicrosoftLoggingAdapter : ITelegratorLogger public class MicrosoftLoggingAdapter : ITelegratorLogger
{ {
private readonly Microsoft.Extensions.Logging.ILogger _logger; private readonly ILogger _logger;
/// <summary> /// <summary>
/// Initializes a new instance of MicrosoftLoggingAdapter. /// Initializes a new instance of MicrosoftLoggingAdapter.
/// </summary> /// </summary>
/// <param name="logger">The Microsoft.Extensions.Logging logger instance.</param> /// <param name="logger">The Microsoft.Extensions.Logging logger instance.</param>
public MicrosoftLoggingAdapter(Microsoft.Extensions.Logging.ILogger logger) public MicrosoftLoggingAdapter(ILogger logger)
{ {
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
} }
/// <inheritdoc/> /// <inheritdoc/>
public void Log(LogLevel level, string message, Exception? exception = null) public void Log(Telegrator.Logging.LogLevel level, string message, Exception? exception = null)
{ {
var msLogLevel = level switch var msLogLevel = level switch
{ {
LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace, Telegrator.Logging.LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace,
LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug, Telegrator.Logging.LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug,
LogLevel.Information => Microsoft.Extensions.Logging.LogLevel.Information, Telegrator.Logging.LogLevel.Information => Microsoft.Extensions.Logging.LogLevel.Information,
LogLevel.Warning => Microsoft.Extensions.Logging.LogLevel.Warning, Telegrator.Logging.LogLevel.Warning => Microsoft.Extensions.Logging.LogLevel.Warning,
LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error, Telegrator.Logging.LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error,
_ => Microsoft.Extensions.Logging.LogLevel.Information _ => Microsoft.Extensions.Logging.LogLevel.Information
}; };
-2
View File
@@ -1,6 +1,5 @@
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
@@ -50,7 +49,6 @@ namespace Telegrator.Hosting
services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>(); services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>();
services.AddSingleton<IHandlersProvider, HostHandlersProvider>(); services.AddSingleton<IHandlersProvider, HostHandlersProvider>();
services.AddSingleton<IUpdateRouter, HostUpdateRouter>(); services.AddSingleton<IUpdateRouter, HostUpdateRouter>();
//services.AddSingleton<ITelegramBotInfo, TelegramBotInfo>(services => new TelegramBotInfo(services.GetRequiredService<ITelegramBotClient>().GetMe().Result));
services.AddSingleton<ITelegramBotInfo, HostedTelegramBotInfo>(); services.AddSingleton<ITelegramBotInfo, HostedTelegramBotInfo>();
return services; return services;
+8
View File
@@ -20,6 +20,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Telegrator.Analyzers", "Tel
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Telegrator.Hosting.Web", "Telegrator.Hosting.Web\Telegrator.Hosting.Web.csproj", "{98AB490F-6A36-CCFF-F6E6-B029D1665965}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Telegrator.Hosting.Web", "Telegrator.Hosting.Web\Telegrator.Hosting.Web.csproj", "{98AB490F-6A36-CCFF-F6E6-B029D1665965}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SosalBot", "..\SosalBot\SosalBot\SosalBot.csproj", "{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
AnalyzersDebug|Any CPU = AnalyzersDebug|Any CPU AnalyzersDebug|Any CPU = AnalyzersDebug|Any CPU
@@ -63,6 +65,12 @@ Global
{98AB490F-6A36-CCFF-F6E6-B029D1665965}.Debug|Any CPU.Build.0 = Debug|Any CPU {98AB490F-6A36-CCFF-F6E6-B029D1665965}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98AB490F-6A36-CCFF-F6E6-B029D1665965}.Release|Any CPU.ActiveCfg = Release|Any CPU {98AB490F-6A36-CCFF-F6E6-B029D1665965}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98AB490F-6A36-CCFF-F6E6-B029D1665965}.Release|Any CPU.Build.0 = Release|Any CPU {98AB490F-6A36-CCFF-F6E6-B029D1665965}.Release|Any CPU.Build.0 = Release|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.AnalyzersDebug|Any CPU.ActiveCfg = Release|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.AnalyzersDebug|Any CPU.Build.0 = Release|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -12,7 +12,7 @@ namespace Telegrator.Attributes.Components
public abstract class StateKeeperAttributeBase : Attribute, IFilter<Update> public abstract class StateKeeperAttributeBase : Attribute, IFilter<Update>
{ {
/// <inheritdoc/> /// <inheritdoc/>
public bool IsCollectible => this.HasPublicProperties(); public bool IsCollectible => GetType().HasPublicProperties();
/// <summary> /// <summary>
/// Creates a new instance <see cref="StateKeeperBase{TKey, TState}"/> /// Creates a new instance <see cref="StateKeeperBase{TKey, TState}"/>
@@ -13,7 +13,7 @@ namespace Telegrator.Attributes.Components
public abstract class UpdateHandlerAttributeBase : Attribute, IFilter<Update> public abstract class UpdateHandlerAttributeBase : Attribute, IFilter<Update>
{ {
/// <inheritdoc/> /// <inheritdoc/>
public bool IsCollectible => this.HasPublicProperties(); public bool IsCollectible => GetType().HasPublicProperties();
/// <summary> /// <summary>
/// Gets an array of <see cref="UpdateHandlerBase"/> that this attribute can be attached to /// Gets an array of <see cref="UpdateHandlerBase"/> that this attribute can be attached to
@@ -8,6 +8,8 @@ namespace Telegrator.Filters.Components
/// </summary> /// </summary>
public class AnonymousTypeFilter : Filter<Update>, INamedFilter public class AnonymousTypeFilter : Filter<Update>, INamedFilter
{ {
private static readonly Type[] IgnoreLog = [typeof(CompiledFilter<>), typeof(AnonymousCompiledFilter), typeof(AnonymousTypeFilter)];
private readonly Func<FilterExecutionContext<Update>, object, bool> FilterAction; private readonly Func<FilterExecutionContext<Update>, object, bool> FilterAction;
private readonly Func<Update, object?> GetFilterringTarget; private readonly Func<Update, object?> GetFilterringTarget;
private readonly string _name; private readonly string _name;
@@ -74,7 +76,7 @@ namespace Telegrator.Filters.Components
FilterExecutionContext<T> context = updateContext.CreateChild((T)filterringTarget); FilterExecutionContext<T> context = updateContext.CreateChild((T)filterringTarget);
if (!filter.CanPass(context)) if (!filter.CanPass(context))
{ {
if (filter is not AnonymousCompiledFilter && filter is not AnonymousTypeFilter) if (IgnoreLog.Contains(filter.GetType().MakeGenericType()))
Alligator.LogDebug("{0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]); Alligator.LogDebug("{0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]);
return false; return false;
@@ -49,7 +49,7 @@ namespace Telegrator.Filters.Components
if (!filter.CanPass(context)) if (!filter.CanPass(context))
{ {
if (filter is not AnonymousCompiledFilter && filter is not AnonymousTypeFilter) if (filter is not AnonymousCompiledFilter && filter is not AnonymousTypeFilter)
Alligator.LogDebug("{0} filter of {1} didnt pass! (Compiled)", filter.GetType().Name, context.Data["handler_name"]); Alligator.LogTrace("{0} filter of {1} didnt pass! (Compiled)", filter.GetType().Name, context.Data["handler_name"]);
return false; return false;
} }
@@ -47,7 +47,7 @@ namespace Telegrator.Filters.Components
if (!typeof(TFilter).IsFilterType()) if (!typeof(TFilter).IsFilterType())
throw new NotFilterTypeException(typeof(TFilter)); throw new NotFilterTypeException(typeof(TFilter));
return CompletedFilters.WhereCast<TFilter>(); return CompletedFilters.OfType<TFilter>();
} }
/// <summary> /// <summary>
+1 -1
View File
@@ -67,7 +67,7 @@ namespace Telegrator.Filters
/// <summary> /// <summary>
/// Gets a value indicating whether this filter is collectible. /// Gets a value indicating whether this filter is collectible.
/// </summary> /// </summary>
public bool IsCollectible => this.HasPublicProperties(); public bool IsCollectible => GetType().HasPublicProperties();
/// <summary> /// <summary>
/// Determines whether the filter can pass for the given context. /// Determines whether the filter can pass for the given context.
@@ -11,26 +11,62 @@ namespace Telegrator.Handlers
/// Provides access to the update, client, filters, and other execution context. /// Provides access to the update, client, filters, and other execution context.
/// </summary> /// </summary>
/// <typeparam name="TUpdate">The type of update being handled.</typeparam> /// <typeparam name="TUpdate">The type of update being handled.</typeparam>
public class AbstractHandlerContainer<TUpdate>(DescribedHandlerInfo handlerInfo) : IAbstractHandlerContainer<TUpdate> where TUpdate : class public class AbstractHandlerContainer<TUpdate> : IAbstractHandlerContainer<TUpdate> where TUpdate : class
{ {
/// <summary> /// <summary>
/// Gets the actual update object of type TUpdate. /// Gets the actual update object of type TUpdate.
/// </summary> /// </summary>
public TUpdate ActualUpdate { get; } = handlerInfo.HandlingUpdate.GetActualUpdateObject<TUpdate>(); public TUpdate ActualUpdate { get; }
/// <inheritdoc/> /// <inheritdoc/>
public Update HandlingUpdate { get; } = handlerInfo.HandlingUpdate; public Update HandlingUpdate { get; }
/// <inheritdoc/> /// <inheritdoc/>
public ITelegramBotClient Client { get; } = handlerInfo.Client; public ITelegramBotClient Client { get; }
/// <inheritdoc/> /// <inheritdoc/>
public Dictionary<string, object> ExtraData { get; } = handlerInfo.ExtraData; public Dictionary<string, object> ExtraData { get; }
/// <inheritdoc/> /// <inheritdoc/>
public CompletedFiltersList CompletedFilters { get; } = handlerInfo.CompletedFilters; public CompletedFiltersList CompletedFilters { get; }
/// <inheritdoc/> /// <inheritdoc/>
public IAwaitingProvider AwaitingProvider { get; } = handlerInfo.AwaitingProvider; public IAwaitingProvider AwaitingProvider { get; }
public AbstractHandlerContainer(DescribedHandlerInfo handlerInfo)
{
ActualUpdate = handlerInfo.HandlingUpdate.GetActualUpdateObject<TUpdate>();
HandlingUpdate = handlerInfo.HandlingUpdate;
Client = handlerInfo.Client;
ExtraData = handlerInfo.ExtraData;
CompletedFilters = handlerInfo.CompletedFilters;
AwaitingProvider = handlerInfo.AwaitingProvider;
}
public AbstractHandlerContainer(TUpdate actualUpdate, Update handlingUpdate, ITelegramBotClient client, Dictionary<string, object> extraData, CompletedFiltersList filters, IAwaitingProvider awaitingProvider)
{
ActualUpdate = actualUpdate;
HandlingUpdate = handlingUpdate;
Client = client;
ExtraData = extraData;
CompletedFilters = filters;
AwaitingProvider = awaitingProvider;
}
public AbstractHandlerContainer<QUpdate> CreateChild<QUpdate>() where QUpdate : class
{
return new AbstractHandlerContainer<QUpdate>(
HandlingUpdate.GetActualUpdateObject<QUpdate>(),
HandlingUpdate, Client, ExtraData,
CompletedFilters, AwaitingProvider);
}
public static AbstractHandlerContainer<TUpdate> From<QUpdate>(IAbstractHandlerContainer<QUpdate> other) where QUpdate : class
{
return new AbstractHandlerContainer<TUpdate>(
other.HandlingUpdate.GetActualUpdateObject<TUpdate>(),
other.HandlingUpdate, other.Client, other.ExtraData,
other.CompletedFilters, other.AwaitingProvider);
}
} }
} }
+64
View File
@@ -0,0 +1,64 @@
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InlineQueryResults;
using Telegrator.Attributes;
using Telegrator.Filters.Components;
using Telegrator.Handlers.Components;
namespace Telegrator.Handlers
{
public class InlineQueryHandlerAttribute(int importance = 0) : UpdateHandlerAttribute<InlineQueryHandler>(UpdateType.InlineQuery, importance)
{
public override bool CanPass(FilterExecutionContext<Update> context) => context.Input.InlineQuery is { } | context.Input.ChosenInlineResult is { };
}
public abstract class InlineQueryHandler() : AbstractUpdateHandler<Update>(UpdateType.InlineQuery)
{
protected IAbstractHandlerContainer<InlineQuery> QueryContainer { get; private set; } = null!;
protected IAbstractHandlerContainer<ChosenInlineResult> ChosenContainer { get; private set; } = null!;
protected InlineQuery InputQuery { get; private set; } = null!;
protected ChosenInlineResult InputChosen { get; private set; } = null!;
public override async Task<Result> Execute(IAbstractHandlerContainer<Update> container, CancellationToken cancellation)
{
switch (container.HandlingUpdate.Type)
{
case UpdateType.InlineQuery:
{
QueryContainer = AbstractHandlerContainer<InlineQuery>.From(container);
InputQuery = QueryContainer.ActualUpdate;
return await Requested(QueryContainer, cancellation);
}
case UpdateType.ChosenInlineResult:
{
ChosenContainer = AbstractHandlerContainer<ChosenInlineResult>.From(container);
InputChosen = ChosenContainer.ActualUpdate;
return await Chosen(ChosenContainer, cancellation);
}
default:
throw new NotImplementedException();
}
}
public abstract Task<Result> Requested(IAbstractHandlerContainer<InlineQuery> container, CancellationToken cancellation);
public abstract Task<Result> Chosen(IAbstractHandlerContainer<ChosenInlineResult> container, CancellationToken cancellation);
protected async Task Answer(
IEnumerable<InlineQueryResult> results,
int? cacheTime = null,
bool isPersonal = false,
string? nextOffset = null,
InlineQueryResultsButton? button = null,
CancellationToken cancellationToken = default)
=> await QueryContainer.AnswerInlineQuery(
results, cacheTime,
isPersonal, nextOffset,
button, cancellationToken);
}
}
+21 -18
View File
@@ -1,3 +1,5 @@
using System.Security.Cryptography.X509Certificates;
namespace Telegrator.Logging namespace Telegrator.Logging
{ {
/// <summary> /// <summary>
@@ -70,28 +72,29 @@ namespace Telegrator.Logging
/// <param name="level">The log level.</param> /// <param name="level">The log level.</param>
/// <param name="message">The message to log.</param> /// <param name="message">The message to log.</param>
/// <param name="exception">Optional exception.</param> /// <param name="exception">Optional exception.</param>
public static void Log(LogLevel level, string message, Exception? exception = null) /// <param name="args"></param>
public static void Log(LogLevel level, string message, Exception? exception = null, params object[] args)
{ {
// Fast path: if no adapters, do nothing // Fast path: if no adapters, do nothing
if (_adapters.Count == 0) if (_adapters.Count == 0)
return; return;
if (level < MinimalLevel) if (level != LogLevel.Trace & level < MinimalLevel)
return; return;
// Lock only during enumeration to prevent collection modification during iteration // Lock only during enumeration to prevent collection modification during iteration
lock (_lock) foreach (var adapter in _adapters)
{ {
foreach (var adapter in _adapters) try
{ {
try if (args != null)
{ message = string.Format(message, args);
adapter.Log(level, message, exception);
} adapter.Log(level, message, exception);
catch }
{ catch
_ = 0xBAD + 0xC0DE; // Ignore adapter errors to prevent logging failures {
} _ = 0xBAD + 0xC0DE; // Ignore adapter errors to prevent logging failures
} }
} }
} }
@@ -112,7 +115,7 @@ namespace Telegrator.Logging
/// <param name="args"></param> /// <param name="args"></param>
public static void LogTrace(string message, params object[] args) public static void LogTrace(string message, params object[] args)
{ {
Log(LogLevel.Trace, string.Format(message, args)); Log(LogLevel.Trace, message, args: args);
} }
/// <summary> /// <summary>
@@ -131,7 +134,7 @@ namespace Telegrator.Logging
/// <param name="args"></param> /// <param name="args"></param>
public static void LogDebug(string message, params object[] args) public static void LogDebug(string message, params object[] args)
{ {
Log(LogLevel.Debug, string.Format(message, args)); Log(LogLevel.Debug, message, args: args);
} }
/// <summary> /// <summary>
@@ -150,7 +153,7 @@ namespace Telegrator.Logging
/// <param name="args"></param> /// <param name="args"></param>
public static void LogInformation(string message, params object[] args) public static void LogInformation(string message, params object[] args)
{ {
Log(LogLevel.Information, string.Format(message, args)); Log(LogLevel.Information, message, args: args);
} }
/// <summary> /// <summary>
@@ -169,7 +172,7 @@ namespace Telegrator.Logging
/// <param name="args"></param> /// <param name="args"></param>
public static void LogWarning(string message, params object[] args) public static void LogWarning(string message, params object[] args)
{ {
Log(LogLevel.Warning, string.Format(message, args)); Log(LogLevel.Warning, message, args: args);
} }
/// <summary> /// <summary>
@@ -189,7 +192,7 @@ namespace Telegrator.Logging
/// <param name="args"></param> /// <param name="args"></param>
public static void LogError(string message, params object[] args) public static void LogError(string message, params object[] args)
{ {
Log(LogLevel.Error, string.Format(message, args)); Log(LogLevel.Error, message, args: args);
} }
/// <summary> /// <summary>
@@ -209,7 +212,7 @@ namespace Telegrator.Logging
/// <param name="args"></param> /// <param name="args"></param>
public static void LogError(string message, Exception? exception = null, params object[] args) public static void LogError(string message, Exception? exception = null, params object[] args)
{ {
Log(LogLevel.Error, string.Format(message, args), exception); Log(LogLevel.Error, message, exception, args);
} }
} }
} }
@@ -61,7 +61,7 @@ namespace Telegrator.MadiatorCore.Descriptors
if (!result) if (!result)
{ {
anyErrors = true; anyErrors = true;
Alligator.LogDebug("(E) UpdateValidator filter of '{0}' for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id); Alligator.LogTrace("(E) UpdateValidator filter of '{0}' for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id);
if (!formReport) if (!formReport)
return Result.Fault(); return Result.Fault();
@@ -84,7 +84,7 @@ namespace Telegrator.MadiatorCore.Descriptors
if (!result) if (!result)
{ {
anyErrors = true; anyErrors = true;
Alligator.LogDebug("(E) StateKeeperValidator filter of '{0}' for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id); Alligator.LogTrace("(E) StateKeeperValidator filter of '{0}' for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id);
if (!formReport) if (!formReport)
return Result.Fault(); return Result.Fault();
@@ -110,7 +110,7 @@ namespace Telegrator.MadiatorCore.Descriptors
if (!result) if (!result)
{ {
anyErrors = true; anyErrors = true;
Alligator.LogDebug("(E) '{0}' filter of '{1}' for Update ({2}) didnt pass!", filterName, filterContext.Data["handler_name"], filterContext.Update.Id); Alligator.LogTrace("(E) '{0}' filter of '{1}' for Update ({2}) didnt pass!", filterName, filterContext.Data["handler_name"], filterContext.Update.Id);
if (!formReport) if (!formReport)
return Result.Fault(); return Result.Fault();
+16 -4
View File
@@ -76,7 +76,15 @@ namespace Telegrator.Providers
_allowedTypes.Union(mightAwaits.SelectMany(attr => attr.UpdateTypes)); _allowedTypes.Union(mightAwaits.SelectMany(attr => attr.UpdateTypes));
IntersectCommands(descriptor); IntersectCommands(descriptor);
GetDescriptorList(descriptor).Add(descriptor); HandlerDescriptorList list = GetDescriptorList(descriptor);
if (descriptor.UpdateType == UpdateType.InlineQuery || descriptor.UpdateType == UpdateType.ChosenInlineResult)
{
if (list.Count > 0)
throw new Exception("Bot cannot have more than one InlineQuery handler");
}
list.Add(descriptor);
return this; return this;
} }
@@ -123,10 +131,14 @@ namespace Telegrator.Providers
/// <returns>The descriptor list for the update type.</returns> /// <returns>The descriptor list for the update type.</returns>
public virtual HandlerDescriptorList GetDescriptorList(HandlerDescriptor descriptor) public virtual HandlerDescriptorList GetDescriptorList(HandlerDescriptor descriptor)
{ {
if (!InnerDictionary.TryGetValue(descriptor.UpdateType, out HandlerDescriptorList? list)) UpdateType updateType = UpdateTypeExtensions.SuppressTypes.TryGetValue(descriptor.UpdateType, out UpdateType suppressType)
? suppressType
: descriptor.UpdateType;
if (!InnerDictionary.TryGetValue(updateType, out HandlerDescriptorList? list))
{ {
list = new HandlerDescriptorList(descriptor.UpdateType, Options); list = new HandlerDescriptorList(updateType, Options);
InnerDictionary.Add(descriptor.UpdateType, list); InnerDictionary.Add(updateType, list);
} }
return list; return list;
+3
View File
@@ -105,6 +105,9 @@ namespace Telegrator.Providers
/// <inheritdoc/> /// <inheritdoc/>
public virtual bool TryGetDescriptorList(UpdateType updateType, out HandlerDescriptorList? list) public virtual bool TryGetDescriptorList(UpdateType updateType, out HandlerDescriptorList? list)
{ {
if (UpdateTypeExtensions.SuppressTypes.TryGetValue(updateType, out UpdateType suppressType))
updateType = suppressType;
return HandlersDictionary.TryGetValue(updateType, out list); return HandlersDictionary.TryGetValue(updateType, out list);
} }
+52 -6
View File
@@ -1,9 +1,9 @@
using System.Collections; using System.Collections.ObjectModel;
using System.Collections.ObjectModel;
using System.Reflection; using System.Reflection;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types; using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InlineQueryResults;
using Telegram.Bot.Types.Payments; using Telegram.Bot.Types.Payments;
using Telegram.Bot.Types.ReplyMarkups; using Telegram.Bot.Types.ReplyMarkups;
using Telegrator.Annotations; using Telegrator.Annotations;
@@ -150,6 +150,26 @@ namespace Telegrator
/// </summary> /// </summary>
public static class AbstractHandlerContainerExtensions public static class AbstractHandlerContainerExtensions
{ {
public static async Task React(
this IAbstractHandlerContainer<Message> container,
IEnumerable<ReactionType> reactions,
bool isBig = false,
CancellationToken cancellationToken = default)
=> await container.Client.SetMessageReaction(
container.ActualUpdate.Chat,
container.ActualUpdate.Id,
reactions, isBig, cancellationToken);
public static async Task React(
this IAbstractHandlerContainer<Message> container,
bool isBig = false,
CancellationToken cancellationToken = default,
params IEnumerable<ReactionType> reactions)
=> await container.Client.SetMessageReaction(
container.ActualUpdate.Chat,
container.ActualUpdate.Id,
reactions, isBig, cancellationToken);
/// <summary> /// <summary>
/// Sends a reply message to the current message. /// Sends a reply message to the current message.
/// </summary> /// </summary>
@@ -344,6 +364,19 @@ namespace Telegrator
url: url, url: url,
cacheTime: cacheTime, cacheTime: cacheTime,
cancellationToken: cancellationToken); cancellationToken: cancellationToken);
public static async Task AnswerInlineQuery(
this IAbstractHandlerContainer<InlineQuery> container,
IEnumerable<InlineQueryResult> results,
int? cacheTime = null,
bool isPersonal = false,
string? nextOffset = null,
InlineQueryResultsButton? button = null,
CancellationToken cancellationToken = default)
{
string id = container.ActualUpdate.Id;
await container.Client.AnswerInlineQuery(id, results.Take(50), cacheTime, isPersonal, nextOffset, button, cancellationToken);
}
} }
/// <summary> /// <summary>
@@ -806,6 +839,7 @@ namespace Telegrator
return source; return source;
} }
/* Found built in method :_(
/// <summary> /// <summary>
/// Creates a new <see cref="IEnumerable{T}"/> with the elements of the <paramref name="source"/> that were successfully cast to the <typeparamref name="TResult"/> /// Creates a new <see cref="IEnumerable{T}"/> with the elements of the <paramref name="source"/> that were successfully cast to the <typeparamref name="TResult"/>
/// </summary> /// </summary>
@@ -820,6 +854,7 @@ namespace Telegrator
yield return result; yield return result;
} }
} }
*/
/// <summary> /// <summary>
/// Sets the value of a key in a dictionary, or if the key does not exist, adds it /// Sets the value of a key in a dictionary, or if the key does not exist, adds it
@@ -1000,12 +1035,12 @@ namespace Telegrator
*/ */
/// <summary> /// <summary>
/// Checks is <paramref name="obj"/> has public properties /// Checks is <paramref name="type"/> has public properties
/// </summary> /// </summary>
/// <param name="obj"></param> /// <param name="type"></param>
/// <returns></returns> /// <returns></returns>
public static bool HasPublicProperties(this object obj) public static bool HasPublicProperties(this Type type)
=> obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(prop => prop.Name != "IsCollectible").Any(); => type.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(prop => prop.Name != "IsCollectible").Any();
/// <summary> /// <summary>
/// Determines whether an instance of a specified type can be assigned to an instance of the current type /// Determines whether an instance of a specified type can be assigned to an instance of the current type
@@ -1181,6 +1216,9 @@ namespace Telegrator
/// <returns></returns> /// <returns></returns>
public static T GetActualUpdateObject<T>(this Update update) public static T GetActualUpdateObject<T>(this Update update)
{ {
if (update is T upd)
return upd;
object actualUpdate = update.GetActualUpdateObject() ?? throw new Exception(); object actualUpdate = update.GetActualUpdateObject() ?? throw new Exception();
if (actualUpdate is not T actualCasted) if (actualUpdate is not T actualCasted)
throw new Exception(); throw new Exception();
@@ -1207,6 +1245,14 @@ namespace Telegrator
UpdateType.EditedChannelPost UpdateType.EditedChannelPost
]; ];
/// <summary>
/// Dictionary of <see cref="UpdateType"/>s that suppresses to generic type for handling types that has complex multi-type handlers
/// </summary>
public static readonly Dictionary<UpdateType, UpdateType> SuppressTypes = new Dictionary<UpdateType, UpdateType>()
{
{ UpdateType.ChosenInlineResult, UpdateType.InlineQuery }
};
/// <summary> /// <summary>
/// Checks if <typeparamref name="T"/> matches one of the <see cref="UpdateType"/>'s give on <paramref name="allowedTypes"/> /// Checks if <typeparamref name="T"/> matches one of the <see cref="UpdateType"/>'s give on <paramref name="allowedTypes"/>
/// </summary> /// </summary>