* BugFixes
* Added basic command arguments filters
This commit is contained in:
@@ -20,8 +20,6 @@ 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
|
||||
@@ -65,12 +63,6 @@ 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
|
||||
|
||||
@@ -47,7 +47,8 @@ namespace Telegrator.Annotations
|
||||
/// </summary>
|
||||
/// <param name="alliases">The command aliases to match against.</param>
|
||||
public CommandAlliasAttribute(params string[] alliases)
|
||||
: base(new CommandAlliasFilter(alliases)) => Alliases = alliases.Select(c => c.TrimStart('/')).ToArray();
|
||||
: base(new CommandAlliasFilter(alliases.Select(c => c.TrimStart('/')).ToArray()))
|
||||
=> Alliases = alliases.Select(c => c.TrimStart('/')).ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the filtering target (Message) from the update.
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
{
|
||||
public class ArgumentStartsWithAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentStartsWithFilter(content, comparison, index))
|
||||
{ }
|
||||
|
||||
public class ArgumentEndsWithAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentEndsWithFilter(content, comparison, index))
|
||||
{ }
|
||||
|
||||
public class ArgumentContainsAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentContainsFilter(content, comparison, index))
|
||||
{ }
|
||||
|
||||
public class ArgumentEqualsAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentEqualsFilter(content, comparison, index))
|
||||
{ }
|
||||
|
||||
public class ArgumentRegexAttribute(string pattern, RegexOptions options = RegexOptions.None, TimeSpan matchTimeout = default, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentRegexFilter(pattern, options, matchTimeout, index))
|
||||
{ }
|
||||
}
|
||||
@@ -1,20 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Filters.Components;
|
||||
using Telegrator.Handlers;
|
||||
|
||||
namespace Telegrator.Filters
|
||||
{
|
||||
public class CommandArgumentFilter : Filter<Message>
|
||||
public abstract class CommandArgumentFilterBase(int index) : Filter<Message>
|
||||
{
|
||||
protected string Target { get; private set; } = null!;
|
||||
|
||||
public override bool CanPass(FilterExecutionContext<Message> context)
|
||||
{
|
||||
CommandHandlerAttribute attr = context.CompletedFilters.Get<CommandHandlerAttribute>(0);
|
||||
string[] args = attr.Arguments ??= context.Input.SplitArgs();
|
||||
Target = args.ElementAtOrDefault(index);
|
||||
|
||||
return alliases.Contains(ReceivedCommand, StringComparer.InvariantCultureIgnoreCase);
|
||||
if (Target == null)
|
||||
return false;
|
||||
|
||||
return CanPassNext(context);
|
||||
}
|
||||
|
||||
protected abstract bool CanPassNext(FilterExecutionContext<Message> context);
|
||||
}
|
||||
|
||||
public class ArgumentStartsWithFilter(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0) : CommandArgumentFilterBase(index)
|
||||
{
|
||||
protected readonly string Content = content;
|
||||
protected readonly StringComparison Comparison = comparison;
|
||||
|
||||
protected override bool CanPassNext(FilterExecutionContext<Message> _)
|
||||
=> Target.StartsWith(Content, Comparison);
|
||||
}
|
||||
|
||||
public class ArgumentEndsWithFilter(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0) : CommandArgumentFilterBase(index)
|
||||
{
|
||||
protected readonly string Content = content;
|
||||
protected readonly StringComparison Comparison = comparison;
|
||||
|
||||
protected override bool CanPassNext(FilterExecutionContext<Message> _)
|
||||
=> Target.EndsWith(Content, Comparison);
|
||||
}
|
||||
|
||||
public class ArgumentContainsFilter(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0) : CommandArgumentFilterBase(index)
|
||||
{
|
||||
protected readonly string Content = content;
|
||||
protected readonly StringComparison Comparison = comparison;
|
||||
|
||||
protected override bool CanPassNext(FilterExecutionContext<Message> _)
|
||||
=> Target.IndexOf(Content, Comparison) >= 0;
|
||||
}
|
||||
|
||||
public class ArgumentEqualsFilter(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0) : CommandArgumentFilterBase(index)
|
||||
{
|
||||
protected readonly string Content = content;
|
||||
protected readonly StringComparison Comparison = comparison;
|
||||
|
||||
protected override bool CanPassNext(FilterExecutionContext<Message> _)
|
||||
=> Target.Equals(Content, Comparison);
|
||||
}
|
||||
|
||||
public class ArgumentRegexFilter : CommandArgumentFilterBase
|
||||
{
|
||||
private readonly Regex _regex;
|
||||
|
||||
public Match Match { get; private set; } = null!;
|
||||
|
||||
public ArgumentRegexFilter(Regex regex, int index = 0)
|
||||
: base(index) => _regex = regex;
|
||||
|
||||
public ArgumentRegexFilter(string pattern, RegexOptions options = RegexOptions.None, TimeSpan matchTimeout = default, int index = 0)
|
||||
: base(index) => _regex = new Regex(pattern, options, matchTimeout);
|
||||
|
||||
protected override bool CanPassNext(FilterExecutionContext<Message> context)
|
||||
{
|
||||
Match = _regex.Match(Target);
|
||||
return Match.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace Telegrator.Handlers.Components
|
||||
/// <summary>
|
||||
/// Gets the allowed return types for branch methods.
|
||||
/// </summary>
|
||||
protected virtual Type[] AllowedBranchReturnTypes => [typeof(void), typeof(Task)];
|
||||
protected virtual Type[] AllowedBranchReturnTypes => [typeof(void), typeof(Task<Result>)];
|
||||
|
||||
/// <summary>
|
||||
/// Gets the cancellation token for the current execution.
|
||||
@@ -75,10 +75,10 @@ namespace Telegrator.Handlers.Components
|
||||
Type thisType = GetType();
|
||||
|
||||
if (branch.GetParameters().Length != 0)
|
||||
throw new Exception();
|
||||
throw new Exception("Branch method must have no parameters.");
|
||||
|
||||
if (!AllowedBranchReturnTypes.Any(branch.ReturnType.Equals))
|
||||
throw new Exception();
|
||||
throw new Exception("Branch method must have one of allowed return types. [void, Task<Result>]");
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Filters;
|
||||
using Telegrator.Filters.Components;
|
||||
using Telegrator.Handlers.Components;
|
||||
using Telegrator.Logging;
|
||||
|
||||
@@ -38,6 +38,7 @@ namespace Telegrator
|
||||
/// <list type="bullet">
|
||||
/// <item>Inside <see cref="IPreProcessor"/> - let handler's main block be executed</item>
|
||||
/// <item>Inside <see cref="UpdateHandlerBase.ExecuteInternal(IHandlerContainer, CancellationToken)"/> - tells <see cref="IUpdateRouter"/> that he can stop describing, as needed handler was found</item>
|
||||
/// <item>Inside <see cref="UpdateHandlerBase.FiltersFallback(FiltersFallbackReport, Telegram.Bot.ITelegramBotClient, CancellationToken)"/> - let <see cref="IUpdateRouter"/> continue describing</item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
@@ -58,7 +59,7 @@ namespace Telegrator
|
||||
/// <summary>
|
||||
/// Represents 'continue'. Use cases:
|
||||
/// <list type="bullet">
|
||||
/// <item>Inside <see cref="UpdateHandlerBase.FiltersFallback(FiltersFallbackReport, Telegram.Bot.ITelegramBotClient, CancellationToken)"/> - let router continue describing</item>
|
||||
/// <item>Inside <see cref="UpdateHandlerBase.FiltersFallback(FiltersFallbackReport, Telegram.Bot.ITelegramBotClient, CancellationToken)"/> - let <see cref="IUpdateRouter"/> continue describing</item>
|
||||
/// <item>Inside <see cref="UpdateHandlerBase.ExecuteInternal(IHandlerContainer, CancellationToken)"/> - Tells <see cref="IUpdateRouter"/> to continue describing handlers</item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user