* 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:
@@ -1,3 +1,4 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Telegrator.Logging;
|
||||
|
||||
namespace Telegrator.Hosting.Logging
|
||||
@@ -8,27 +9,27 @@ namespace Telegrator.Hosting.Logging
|
||||
/// </summary>
|
||||
public class MicrosoftLoggingAdapter : ITelegratorLogger
|
||||
{
|
||||
private readonly Microsoft.Extensions.Logging.ILogger _logger;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of MicrosoftLoggingAdapter.
|
||||
/// </summary>
|
||||
/// <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));
|
||||
}
|
||||
|
||||
/// <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
|
||||
{
|
||||
LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace,
|
||||
LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug,
|
||||
LogLevel.Information => Microsoft.Extensions.Logging.LogLevel.Information,
|
||||
LogLevel.Warning => Microsoft.Extensions.Logging.LogLevel.Warning,
|
||||
LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error,
|
||||
Telegrator.Logging.LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace,
|
||||
Telegrator.Logging.LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug,
|
||||
Telegrator.Logging.LogLevel.Information => Microsoft.Extensions.Logging.LogLevel.Information,
|
||||
Telegrator.Logging.LogLevel.Warning => Microsoft.Extensions.Logging.LogLevel.Warning,
|
||||
Telegrator.Logging.LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error,
|
||||
_ => Microsoft.Extensions.Logging.LogLevel.Information
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -50,7 +49,6 @@ namespace Telegrator.Hosting
|
||||
services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>();
|
||||
services.AddSingleton<IHandlersProvider, HostHandlersProvider>();
|
||||
services.AddSingleton<IUpdateRouter, HostUpdateRouter>();
|
||||
//services.AddSingleton<ITelegramBotInfo, TelegramBotInfo>(services => new TelegramBotInfo(services.GetRequiredService<ITelegramBotClient>().GetMe().Result));
|
||||
services.AddSingleton<ITelegramBotInfo, HostedTelegramBotInfo>();
|
||||
|
||||
return services;
|
||||
|
||||
@@ -20,6 +20,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Telegrator.Analyzers", "Tel
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Telegrator.Hosting.Web", "Telegrator.Hosting.Web\Telegrator.Hosting.Web.csproj", "{98AB490F-6A36-CCFF-F6E6-B029D1665965}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SosalBot", "..\SosalBot\SosalBot\SosalBot.csproj", "{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
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}.Release|Any CPU.ActiveCfg = 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
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Telegrator.Attributes.Components
|
||||
public abstract class StateKeeperAttributeBase : Attribute, IFilter<Update>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public bool IsCollectible => this.HasPublicProperties();
|
||||
public bool IsCollectible => GetType().HasPublicProperties();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance <see cref="StateKeeperBase{TKey, TState}"/>
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Telegrator.Attributes.Components
|
||||
public abstract class UpdateHandlerAttributeBase : Attribute, IFilter<Update>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public bool IsCollectible => this.HasPublicProperties();
|
||||
public bool IsCollectible => GetType().HasPublicProperties();
|
||||
|
||||
/// <summary>
|
||||
/// Gets an array of <see cref="UpdateHandlerBase"/> that this attribute can be attached to
|
||||
|
||||
@@ -8,6 +8,8 @@ namespace Telegrator.Filters.Components
|
||||
/// </summary>
|
||||
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<Update, object?> GetFilterringTarget;
|
||||
private readonly string _name;
|
||||
@@ -74,7 +76,7 @@ namespace Telegrator.Filters.Components
|
||||
FilterExecutionContext<T> context = updateContext.CreateChild((T)filterringTarget);
|
||||
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"]);
|
||||
|
||||
return false;
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace Telegrator.Filters.Components
|
||||
if (!filter.CanPass(context))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace Telegrator.Filters.Components
|
||||
if (!typeof(TFilter).IsFilterType())
|
||||
throw new NotFilterTypeException(typeof(TFilter));
|
||||
|
||||
return CompletedFilters.WhereCast<TFilter>();
|
||||
return CompletedFilters.OfType<TFilter>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -67,7 +67,7 @@ namespace Telegrator.Filters
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this filter is collectible.
|
||||
/// </summary>
|
||||
public bool IsCollectible => this.HasPublicProperties();
|
||||
public bool IsCollectible => GetType().HasPublicProperties();
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// Gets the actual update object of type TUpdate.
|
||||
/// </summary>
|
||||
public TUpdate ActualUpdate { get; } = handlerInfo.HandlingUpdate.GetActualUpdateObject<TUpdate>();
|
||||
public TUpdate ActualUpdate { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Update HandlingUpdate { get; } = handlerInfo.HandlingUpdate;
|
||||
public Update HandlingUpdate { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ITelegramBotClient Client { get; } = handlerInfo.Client;
|
||||
public ITelegramBotClient Client { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Dictionary<string, object> ExtraData { get; } = handlerInfo.ExtraData;
|
||||
public Dictionary<string, object> ExtraData { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public CompletedFiltersList CompletedFilters { get; } = handlerInfo.CompletedFilters;
|
||||
public CompletedFiltersList CompletedFilters { get; }
|
||||
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace Telegrator.Logging
|
||||
{
|
||||
/// <summary>
|
||||
@@ -70,22 +72,24 @@ namespace Telegrator.Logging
|
||||
/// <param name="level">The log level.</param>
|
||||
/// <param name="message">The message to log.</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
|
||||
if (_adapters.Count == 0)
|
||||
return;
|
||||
|
||||
if (level < MinimalLevel)
|
||||
if (level != LogLevel.Trace & level < MinimalLevel)
|
||||
return;
|
||||
|
||||
// Lock only during enumeration to prevent collection modification during iteration
|
||||
lock (_lock)
|
||||
{
|
||||
foreach (var adapter in _adapters)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (args != null)
|
||||
message = string.Format(message, args);
|
||||
|
||||
adapter.Log(level, message, exception);
|
||||
}
|
||||
catch
|
||||
@@ -94,7 +98,6 @@ namespace Telegrator.Logging
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs a trace message to all registered adapters.
|
||||
@@ -112,7 +115,7 @@ namespace Telegrator.Logging
|
||||
/// <param name="args"></param>
|
||||
public static void LogTrace(string message, params object[] args)
|
||||
{
|
||||
Log(LogLevel.Trace, string.Format(message, args));
|
||||
Log(LogLevel.Trace, message, args: args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -131,7 +134,7 @@ namespace Telegrator.Logging
|
||||
/// <param name="args"></param>
|
||||
public static void LogDebug(string message, params object[] args)
|
||||
{
|
||||
Log(LogLevel.Debug, string.Format(message, args));
|
||||
Log(LogLevel.Debug, message, args: args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -150,7 +153,7 @@ namespace Telegrator.Logging
|
||||
/// <param name="args"></param>
|
||||
public static void LogInformation(string message, params object[] args)
|
||||
{
|
||||
Log(LogLevel.Information, string.Format(message, args));
|
||||
Log(LogLevel.Information, message, args: args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -169,7 +172,7 @@ namespace Telegrator.Logging
|
||||
/// <param name="args"></param>
|
||||
public static void LogWarning(string message, params object[] args)
|
||||
{
|
||||
Log(LogLevel.Warning, string.Format(message, args));
|
||||
Log(LogLevel.Warning, message, args: args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -189,7 +192,7 @@ namespace Telegrator.Logging
|
||||
/// <param name="args"></param>
|
||||
public static void LogError(string message, params object[] args)
|
||||
{
|
||||
Log(LogLevel.Error, string.Format(message, args));
|
||||
Log(LogLevel.Error, message, args: args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -209,7 +212,7 @@ namespace Telegrator.Logging
|
||||
/// <param name="args"></param>
|
||||
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)
|
||||
{
|
||||
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)
|
||||
return Result.Fault();
|
||||
@@ -84,7 +84,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
||||
if (!result)
|
||||
{
|
||||
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)
|
||||
return Result.Fault();
|
||||
@@ -110,7 +110,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
||||
if (!result)
|
||||
{
|
||||
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)
|
||||
return Result.Fault();
|
||||
|
||||
@@ -76,7 +76,15 @@ namespace Telegrator.Providers
|
||||
_allowedTypes.Union(mightAwaits.SelectMany(attr => attr.UpdateTypes));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -123,10 +131,14 @@ namespace Telegrator.Providers
|
||||
/// <returns>The descriptor list for the update type.</returns>
|
||||
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);
|
||||
InnerDictionary.Add(descriptor.UpdateType, list);
|
||||
list = new HandlerDescriptorList(updateType, Options);
|
||||
InnerDictionary.Add(updateType, list);
|
||||
}
|
||||
|
||||
return list;
|
||||
|
||||
@@ -105,6 +105,9 @@ namespace Telegrator.Providers
|
||||
/// <inheritdoc/>
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
using System.Collections;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Reflection;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegram.Bot.Types.InlineQueryResults;
|
||||
using Telegram.Bot.Types.Payments;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
using Telegrator.Annotations;
|
||||
@@ -150,6 +150,26 @@ namespace Telegrator
|
||||
/// </summary>
|
||||
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>
|
||||
/// Sends a reply message to the current message.
|
||||
/// </summary>
|
||||
@@ -344,6 +364,19 @@ namespace Telegrator
|
||||
url: url,
|
||||
cacheTime: cacheTime,
|
||||
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>
|
||||
@@ -806,6 +839,7 @@ namespace Telegrator
|
||||
return source;
|
||||
}
|
||||
|
||||
/* Found built in method :_(
|
||||
/// <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"/>
|
||||
/// </summary>
|
||||
@@ -820,6 +854,7 @@ namespace Telegrator
|
||||
yield return result;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
/// Checks is <paramref name="obj"/> has public properties
|
||||
/// Checks is <paramref name="type"/> has public properties
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static bool HasPublicProperties(this object obj)
|
||||
=> obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(prop => prop.Name != "IsCollectible").Any();
|
||||
public static bool HasPublicProperties(this Type type)
|
||||
=> type.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(prop => prop.Name != "IsCollectible").Any();
|
||||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
public static T GetActualUpdateObject<T>(this Update update)
|
||||
{
|
||||
if (update is T upd)
|
||||
return upd;
|
||||
|
||||
object actualUpdate = update.GetActualUpdateObject() ?? throw new Exception();
|
||||
if (actualUpdate is not T actualCasted)
|
||||
throw new Exception();
|
||||
@@ -1207,6 +1245,14 @@ namespace Telegrator
|
||||
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>
|
||||
/// Checks if <typeparamref name="T"/> matches one of the <see cref="UpdateType"/>'s give on <paramref name="allowedTypes"/>
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user