* Fixed default's HandlersProvider instance creation

* Fixed all of the message filters faulting because of wrong execution sequence
* Removed default Host loggers
* Fixed descriptors indexing
This commit is contained in:
2025-07-28 01:40:04 +04:00
parent 1016132a65
commit 76cf6eb4a2
14 changed files with 60 additions and 33 deletions
+4 -3
View File
@@ -127,12 +127,13 @@ namespace Telegrator.Hosting
foreach (UpdateType updateType in handlers.Keys) foreach (UpdateType updateType in handlers.Keys)
{ {
HandlerDescriptorList descriptors = handlers[updateType]; HandlerDescriptorList descriptors = handlers[updateType];
logBuilder.AppendLine("\n\tUpdateType." + updateType + " :"); logBuilder.Append("\n\tUpdateType." + updateType + " :");
foreach (HandlerDescriptor descriptor in descriptors.Reverse()) foreach (HandlerDescriptor descriptor in descriptors.Reverse())
{ {
string indexerString = descriptor.Indexer.ToString(); logBuilder.AppendFormat("\n\t* {0} - {1}",
logBuilder.AppendLine("* " + indexerString + " " + (descriptor.DisplayString ?? descriptor.HandlerType.Name)); descriptor.Indexer.ToString(),
descriptor.ToString());
} }
} }
@@ -49,6 +49,8 @@ namespace Telegrator.Hosting
_settings = settings ?? new TelegramBotHostBuilderSettings(); _settings = settings ?? new TelegramBotHostBuilderSettings();
_handlers = new HostHandlersCollection(Services, _settings); _handlers = new HostHandlersCollection(Services, _settings);
_innerBuilder.Logging.ClearProviders();
Services.Configure<TelegramBotOptions>(Configuration.GetSection(nameof(TelegramBotOptions))); Services.Configure<TelegramBotOptions>(Configuration.GetSection(nameof(TelegramBotOptions)));
Services.Configure<ReceiverOptions>(Configuration.GetSection(nameof(ReceiverOptions))); Services.Configure<ReceiverOptions>(Configuration.GetSection(nameof(ReceiverOptions)));
Services.Configure<TelegramBotClientOptions>(Configuration.GetSection(nameof(TelegramBotClientOptions)), new TelegramBotClientOptionsProxy()); Services.Configure<TelegramBotClientOptions>(Configuration.GetSection(nameof(TelegramBotClientOptions)), new TelegramBotClientOptionsProxy());
+1 -1
View File
@@ -15,7 +15,7 @@
<EnableNETAnalyzers>True</EnableNETAnalyzers> <EnableNETAnalyzers>True</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.0.3</Version> <Version>1.0.5</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -1,6 +1,5 @@
using Telegram.Bot.Types; using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
using Telegrator.Attributes;
using Telegrator.Filters; using Telegrator.Filters;
using Telegrator.Filters.Components; using Telegrator.Filters.Components;
using Telegrator.Handlers.Components; using Telegrator.Handlers.Components;
+23 -1
View File
@@ -1,4 +1,5 @@
using Telegram.Bot.Types; using System.Text.RegularExpressions;
using Telegram.Bot.Types;
using Telegrator.Filters.Components; using Telegrator.Filters.Components;
namespace Telegrator.Filters namespace Telegrator.Filters
@@ -48,4 +49,25 @@ namespace Telegrator.Filters
return context.Input.InlineMessageId == _inlineMessageId; return context.Input.InlineMessageId == _inlineMessageId;
} }
} }
/// <summary>
/// Filters callback queries by matching their data with a regular expression.
/// </summary>
public class CallbackRegexFilter : RegexFilterBase<CallbackQuery>
{
/// <summary>
/// Initializes a new instance of the <see cref="CallbackRegexFilter"/> class with a pattern and options.
/// </summary>
/// <param name="pattern">The regex pattern.</param>
/// <param name="regexOptions">The regex options.</param>
public CallbackRegexFilter(string pattern, RegexOptions regexOptions = default)
: base(clb => clb.Data, pattern, regexOptions) { }
/// <summary>
/// Initializes a new instance of the <see cref="CallbackRegexFilter"/> class with a regex object.
/// </summary>
/// <param name="regex">The regex object.</param>
public CallbackRegexFilter(Regex regex)
: base(clb => clb.Data, regex) { }
}
} }
+2 -2
View File
@@ -45,8 +45,8 @@ namespace Telegrator.Filters
return false; return false;
string userName = Mention ?? context.BotInfo.User.Username ?? throw new ArgumentNullException(nameof(context), "MentionedFilter requires BotInfo to be initialized"); string userName = Mention ?? context.BotInfo.User.Username ?? throw new ArgumentNullException(nameof(context), "MentionedFilter requires BotInfo to be initialized");
MessageHasEntityFilter entityFilter = context.CompletedFilters.Get<MessageHasEntityFilter>(0); IEnumerable<MessageHasEntityFilter> entityFilter = context.CompletedFilters.Get<MessageHasEntityFilter>();
return entityFilter.FoundEntities.Any(ent => Target.Text.Substring(ent.Offset + 1, ent.Length - 1) == userName); return entityFilter.Any(fltr => fltr.FoundEntities.Any(ent => Target.Text.Substring(ent.Offset + 1, ent.Length - 1) == userName));
} }
} }
} }
+14 -1
View File
@@ -16,10 +16,23 @@ namespace Telegrator.Filters
protected Message Target { get; private set; } = null!; protected Message Target { get; private set; } = null!;
/// <inheritdoc/> /// <inheritdoc/>
public override bool CanPass(FilterExecutionContext<Message> context) protected virtual bool CanPassBase(FilterExecutionContext<Message> context)
{ {
FromReplyChainFilter? repliedFilter = context.CompletedFilters.Get<FromReplyChainFilter>().SingleOrDefault(); FromReplyChainFilter? repliedFilter = context.CompletedFilters.Get<FromReplyChainFilter>().SingleOrDefault();
Target = repliedFilter?.Reply ?? context.Input; Target = repliedFilter?.Reply ?? context.Input;
if (Target is not { Id: > 0 })
return false;
return true;
}
/// <inheritdoc/>
public override bool CanPass(FilterExecutionContext<Message> context)
{
if (!CanPassBase(context))
return false;
return CanPassNext(context); return CanPassNext(context);
} }
+1 -1
View File
@@ -22,7 +22,7 @@ namespace Telegrator.Filters
/// <returns>True if the message has a valid sender; otherwise, false.</returns> /// <returns>True if the message has a valid sender; otherwise, false.</returns>
public override bool CanPass(FilterExecutionContext<Message> context) public override bool CanPass(FilterExecutionContext<Message> context)
{ {
if (!base.CanPass(context)) if (!base.CanPassBase(context))
return false; return false;
User = Target.From!; User = Target.From!;
+4 -7
View File
@@ -1,6 +1,4 @@
using System; using Telegram.Bot.Types;
using System.Linq;
using Telegram.Bot.Types;
using Telegrator.Filters.Components; using Telegrator.Filters.Components;
namespace Telegrator.Filters namespace Telegrator.Filters
@@ -29,14 +27,13 @@ namespace Telegrator.Filters
/// <returns>True if the message is valid and can be processed further; otherwise, false.</returns> /// <returns>True if the message is valid and can be processed further; otherwise, false.</returns>
public override bool CanPass(FilterExecutionContext<Message> context) public override bool CanPass(FilterExecutionContext<Message> context)
{ {
if (!base.CanPass(context)) if (!base.CanPassBase(context))
return false; return false;
Message = context.Input!; if (Target is not { Id: > 0, Text.Length: > 0 })
if (Message is not { Id: > 0 })
return false; return false;
Text = Message.Text ?? string.Empty; Text = Target.Text;
return CanPassNext(context); return CanPassNext(context);
} }
} }
@@ -82,7 +82,7 @@ namespace Telegrator.MadiatorCore.Descriptors
/// <returns>A string in the format (C:importance, P:priority).</returns> /// <returns>A string in the format (C:importance, P:priority).</returns>
public override string ToString() public override string ToString()
{ {
return string.Format("(I:{0}, C:{1}, P:{2})", RouterIndex, Importance, Priority); return string.Format("(Ix: {0,2}, Im: {1,2}, Pr: {2,2})", RouterIndex, Importance, Priority);
} }
} }
} }
@@ -77,15 +77,7 @@ namespace Telegrator.MadiatorCore.Descriptors
if (_handlingType != UpdateType.Unknown && descriptor.UpdateType != _handlingType) if (_handlingType != UpdateType.Unknown && descriptor.UpdateType != _handlingType)
throw new InvalidOperationException(); throw new InvalidOperationException();
while (_innerCollection.TryGetValue(descriptor.Indexer, out HandlerDescriptor? conflictDescriptor)) descriptor.Indexer = descriptor.Indexer.UpdateIndex(count++);
{
int newIndex = count++;
if (_options?.DescendDescriptorIndex ?? false)
newIndex += -1;
descriptor.Indexer = descriptor.Indexer.UpdateIndex(count);
}
_innerCollection.Add(descriptor.Indexer, descriptor); _innerCollection.Add(descriptor.Indexer, descriptor);
} }
} }
+2 -2
View File
@@ -67,14 +67,14 @@ namespace Telegrator.Providers
{ {
return descriptor.SingletonInstance ??= (descriptor.InstanceFactory != null return descriptor.SingletonInstance ??= (descriptor.InstanceFactory != null
? descriptor.SingletonInstance = descriptor.InstanceFactory.Invoke() ? descriptor.SingletonInstance = descriptor.InstanceFactory.Invoke()
: descriptor.SingletonInstance = (UpdateHandlerBase)Activator.CreateInstance(descriptor.HandlerType, [descriptor.UpdateType])); : descriptor.SingletonInstance = (UpdateHandlerBase)Activator.CreateInstance(descriptor.HandlerType));
} }
case DescriptorType.Keyed: case DescriptorType.Keyed:
case DescriptorType.General: case DescriptorType.General:
{ {
return descriptor.InstanceFactory == null return descriptor.InstanceFactory == null
? (UpdateHandlerBase)Activator.CreateInstance(descriptor.HandlerType, [descriptor.UpdateType]) ? (UpdateHandlerBase)Activator.CreateInstance(descriptor.HandlerType)
: descriptor.InstanceFactory.Invoke(); : descriptor.InstanceFactory.Invoke();
} }
+1 -1
View File
@@ -17,7 +17,7 @@
<EnableNETAnalyzers>True</EnableNETAnalyzers> <EnableNETAnalyzers>True</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.0.3</Version> <Version>1.0.5</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
+3 -2
View File
@@ -194,7 +194,7 @@ namespace Telegrator
/// </summary> /// </summary>
public static partial class HandlersCollectionExtensions public static partial class HandlersCollectionExtensions
{ {
private static readonly string[] skippingAssemblies = ["System", "Microsoft", "Telegrator"]; private static readonly string[] skippingAssemblies = ["System.", "Microsoft."];
/// <summary> /// <summary>
/// Collects all handlers from the current app domain. /// Collects all handlers from the current app domain.
@@ -206,7 +206,8 @@ namespace Telegrator
{ {
AppDomain.CurrentDomain AppDomain.CurrentDomain
.GetAssemblies() .GetAssemblies()
.Where(ass => skippingAssemblies.All(skip => !ass.FullName.Contains(skip))) .Where(ass => ass.GetName().Name != "Telegrator")
.Where(ass => skippingAssemblies.All(skip => !ass.FullName.StartsWith(skip)))
.SelectMany(ass => ass.GetExportedTypes()) .SelectMany(ass => ass.GetExportedTypes())
.Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null) .Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null)
.Where(type => type.IsHandlerRealization()) .Where(type => type.IsHandlerRealization())