* 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:
@@ -1,6 +1,5 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Attributes;
|
||||
using Telegrator.Filters;
|
||||
using Telegrator.Filters.Components;
|
||||
using Telegrator.Handlers.Components;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Telegram.Bot.Types;
|
||||
using System.Text.RegularExpressions;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Filters.Components;
|
||||
|
||||
namespace Telegrator.Filters
|
||||
@@ -48,4 +49,25 @@ namespace Telegrator.Filters
|
||||
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) { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,8 +45,8 @@ namespace Telegrator.Filters
|
||||
return false;
|
||||
|
||||
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);
|
||||
return entityFilter.FoundEntities.Any(ent => Target.Text.Substring(ent.Offset + 1, ent.Length - 1) == userName);
|
||||
IEnumerable<MessageHasEntityFilter> entityFilter = context.CompletedFilters.Get<MessageHasEntityFilter>();
|
||||
return entityFilter.Any(fltr => fltr.FoundEntities.Any(ent => Target.Text.Substring(ent.Offset + 1, ent.Length - 1) == userName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,23 @@ namespace Telegrator.Filters
|
||||
protected Message Target { get; private set; } = null!;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override bool CanPass(FilterExecutionContext<Message> context)
|
||||
protected virtual bool CanPassBase(FilterExecutionContext<Message> context)
|
||||
{
|
||||
FromReplyChainFilter? repliedFilter = context.CompletedFilters.Get<FromReplyChainFilter>().SingleOrDefault();
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace Telegrator.Filters
|
||||
/// <returns>True if the message has a valid sender; otherwise, false.</returns>
|
||||
public override bool CanPass(FilterExecutionContext<Message> context)
|
||||
{
|
||||
if (!base.CanPass(context))
|
||||
if (!base.CanPassBase(context))
|
||||
return false;
|
||||
|
||||
User = Target.From!;
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Filters.Components;
|
||||
|
||||
namespace Telegrator.Filters
|
||||
@@ -15,7 +13,7 @@ namespace Telegrator.Filters
|
||||
/// Gets the current message being processed by the filter.
|
||||
/// </summary>
|
||||
public Message Message { get; private set; } = null!;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the extracted text content from the current message.
|
||||
/// </summary>
|
||||
@@ -29,14 +27,13 @@ namespace Telegrator.Filters
|
||||
/// <returns>True if the message is valid and can be processed further; otherwise, false.</returns>
|
||||
public override bool CanPass(FilterExecutionContext<Message> context)
|
||||
{
|
||||
if (!base.CanPass(context))
|
||||
if (!base.CanPassBase(context))
|
||||
return false;
|
||||
|
||||
Message = context.Input!;
|
||||
if (Message is not { Id: > 0 })
|
||||
if (Target is not { Id: > 0, Text.Length: > 0 })
|
||||
return false;
|
||||
|
||||
Text = Message.Text ?? string.Empty;
|
||||
Text = Target.Text;
|
||||
return CanPassNext(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
||||
/// <returns>A string in the format (C:importance, P:priority).</returns>
|
||||
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)
|
||||
throw new InvalidOperationException();
|
||||
|
||||
while (_innerCollection.TryGetValue(descriptor.Indexer, out HandlerDescriptor? conflictDescriptor))
|
||||
{
|
||||
int newIndex = count++;
|
||||
if (_options?.DescendDescriptorIndex ?? false)
|
||||
newIndex += -1;
|
||||
|
||||
descriptor.Indexer = descriptor.Indexer.UpdateIndex(count);
|
||||
}
|
||||
|
||||
descriptor.Indexer = descriptor.Indexer.UpdateIndex(count++);
|
||||
_innerCollection.Add(descriptor.Indexer, descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,14 +67,14 @@ namespace Telegrator.Providers
|
||||
{
|
||||
return descriptor.SingletonInstance ??= (descriptor.InstanceFactory != null
|
||||
? 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.General:
|
||||
{
|
||||
return descriptor.InstanceFactory == null
|
||||
? (UpdateHandlerBase)Activator.CreateInstance(descriptor.HandlerType, [descriptor.UpdateType])
|
||||
? (UpdateHandlerBase)Activator.CreateInstance(descriptor.HandlerType)
|
||||
: descriptor.InstanceFactory.Invoke();
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
<EnableNETAnalyzers>True</EnableNETAnalyzers>
|
||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
<Version>1.0.3</Version>
|
||||
<Version>1.0.5</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -194,7 +194,7 @@ namespace Telegrator
|
||||
/// </summary>
|
||||
public static partial class HandlersCollectionExtensions
|
||||
{
|
||||
private static readonly string[] skippingAssemblies = ["System", "Microsoft", "Telegrator"];
|
||||
private static readonly string[] skippingAssemblies = ["System.", "Microsoft."];
|
||||
|
||||
/// <summary>
|
||||
/// Collects all handlers from the current app domain.
|
||||
@@ -206,7 +206,8 @@ namespace Telegrator
|
||||
{
|
||||
AppDomain.CurrentDomain
|
||||
.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())
|
||||
.Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null)
|
||||
.Where(type => type.IsHandlerRealization())
|
||||
|
||||
Reference in New Issue
Block a user