Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0dedd3c0f4 | |||
| 79d5df8291 | |||
| 899243c62b | |||
| 5f9c640930 | |||
| 0e445dd586 | |||
| da090627ff | |||
| 88bd12aadd | |||
| 162d4a1d05 | |||
| e28cc2b8da | |||
| c6cda703ef | |||
| 401c8d02aa | |||
| 2cf4910abd |
+2
-5
@@ -361,8 +361,5 @@ MigrationBackup/
|
||||
|
||||
# Fody - auto-generated XML schema
|
||||
FodyWeavers.xsd
|
||||
/GETTING_STARTED.md
|
||||
/ANNOTATION_OVERVIEW.md
|
||||
/.aider.input.history
|
||||
/.aider.chat.history.md
|
||||
/.aider.tags.cache.v4
|
||||
|
||||
.aider*
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
# Telegrator
|
||||
|
||||

|
||||
|
||||
> **A modern reactive framework for Telegram bots in C# with aspect-oriented design, mediator-based dispatching, and flexible architecture.**
|
||||
|
||||
---
|
||||
|
||||
## 🚀 About Telegrator
|
||||
|
||||
Telegrator is a next-generation framework for building Telegram bots in C#, inspired by AOP (Aspect-Oriented Programming) and the mediator pattern. It enables decentralized, easily extensible, and maintainable bot logic without traditional state machines or monolithic handlers.
|
||||
Telegrator is a modern C# framework for building Telegram bots, inspired by AOP (Aspect-Oriented Programming) and the mediator pattern. It enables decentralized, easily extensible, and maintainable bot logic without traditional state machines or monolithic handlers.
|
||||
|
||||
---
|
||||
|
||||
@@ -95,13 +91,12 @@ bot.Handlers.AddHandler<StartCommandHandler>();
|
||||
using Telegrator.Handlers;
|
||||
using Telegrator.Annotations;
|
||||
|
||||
[CommandHandler, CommandAlias("first"), NumericState(SpecialState.NoState)]
|
||||
[CommandHandler, CommandAlias("first"), State<SetupWizard>(null)]
|
||||
public class StateKeepFirst : CommandHandler
|
||||
{
|
||||
public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
|
||||
{
|
||||
container.CreateNumericState();
|
||||
container.ForwardNumericState();
|
||||
StateStorage.GetStateMachine<SetupWizard>().BysenderId().Advance();
|
||||
await Reply("first state moved (1)", cancellationToken: cancellation);
|
||||
return Result.Ok();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<Solution>
|
||||
<Project Path="dev/Telegrator.RoslynGenerators/Telegrator.RoslynGenerators.csproj" />
|
||||
<Project Path="src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj" />
|
||||
<Project Path="src/Telegrator.Analyzers/Telegrator.Analyzers.csproj" />
|
||||
<Project Path="src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj" />
|
||||
<Project Path="src/Telegrator.Hosting/Telegrator.Hosting.csproj" />
|
||||
|
||||
@@ -5,10 +5,6 @@ using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Telegrator.RoslynGenerators.RoslynExtensions;
|
||||
|
||||
#if DEBUG
|
||||
using System.Diagnostics;
|
||||
#endif
|
||||
|
||||
namespace Telegrator.RoslynGenerators;
|
||||
|
||||
[Generator(LanguageNames.CSharp)]
|
||||
@@ -72,6 +68,9 @@ public class ImplicitHandlerBuilderExtensionsGenerator : IIncrementalGenerator
|
||||
if (className == "FilterAnnotation")
|
||||
continue;
|
||||
|
||||
if (className == "StateAttribute")
|
||||
continue;
|
||||
|
||||
MethodDeclarationSyntax? targeter = classDeclaration.Members.OfType<MethodDeclarationSyntax>().SingleOrDefault(IsTargeterMethod);
|
||||
if (targeter != null)
|
||||
{
|
||||
@@ -101,7 +100,13 @@ public class ImplicitHandlerBuilderExtensionsGenerator : IIncrementalGenerator
|
||||
continue;
|
||||
|
||||
usings.UnionAdd(classDeclaration.FindAncestor<CompilationUnitSyntax>().Usings, UsingEqualityComparer);
|
||||
MethodDeclarationSyntax targeter = FindTargetterMethod(targetters, classDeclaration);
|
||||
MethodDeclarationSyntax? targeter = FindTargetterMethod(targetters, classDeclaration);
|
||||
|
||||
if (targeter == null)
|
||||
{
|
||||
debugExport.AppendLine("Targetter not found");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (classDeclaration.ParameterList != null && classDeclaration.BaseList != null)
|
||||
{
|
||||
@@ -140,16 +145,16 @@ public class ImplicitHandlerBuilderExtensionsGenerator : IIncrementalGenerator
|
||||
{
|
||||
ClassDeclarationSyntax extensionsClass = SyntaxFactory.ClassDeclaration("HandlerBuilderExtensions")
|
||||
.WithModifiers(Modifiers(SyntaxKind.PublicKeyword, SyntaxKind.StaticKeyword, SyntaxKind.PartialKeyword))
|
||||
.AddMembers([.. targetters.Values, .. extensions])
|
||||
.DecorateType(1);
|
||||
.AddMembers([.. targetters.Values, .. extensions]);
|
||||
|
||||
NamespaceDeclarationSyntax namespaceDeclaration = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName("Telegrator"))
|
||||
.WithMembers([extensionsClass])
|
||||
.Decorate();
|
||||
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia("#pragma warning disable CS1591"))
|
||||
.WithMembers([extensionsClass]);
|
||||
|
||||
CompilationUnitSyntax compilationUnit = SyntaxFactory.CompilationUnit()
|
||||
.WithUsings([.. usings])
|
||||
.WithMembers([namespaceDeclaration]);
|
||||
.WithMembers([namespaceDeclaration])
|
||||
.NormalizeWhitespace();
|
||||
|
||||
context.AddSource("GeneratedHandlerBuilderExtensions.cs", compilationUnit.ToFullString());
|
||||
}
|
||||
@@ -175,7 +180,7 @@ public class ImplicitHandlerBuilderExtensionsGenerator : IIncrementalGenerator
|
||||
if (targetterMethod.ExpressionBody != null)
|
||||
method = method.WithExpressionBody(targetterMethod.ExpressionBody).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
|
||||
|
||||
return method.DecorateMember(2);
|
||||
return method;
|
||||
}
|
||||
|
||||
private static MethodDeclarationSyntax GeneratedExtensionsMethod(ClassDeclarationSyntax classDeclaration, ParameterListSyntax methodParameters, ArgumentListSyntax invokerArguments, MethodDeclarationSyntax targetterMethod)
|
||||
@@ -204,11 +209,10 @@ public class ImplicitHandlerBuilderExtensionsGenerator : IIncrementalGenerator
|
||||
|
||||
MethodDeclarationSyntax method = SyntaxFactory.MethodDeclaration(returnType, identifier)
|
||||
.WithParameterList(parameters)
|
||||
.WithBody(body.DecorateBlock(2))
|
||||
.WithBody(body)
|
||||
.WithTypeParameterList(typeParameters)
|
||||
.WithModifiers(Modifiers(SyntaxKind.PublicKeyword, SyntaxKind.StaticKeyword))
|
||||
.WithConstraintClauses([typeParameterConstraint])
|
||||
.DecorateMember(2)
|
||||
.WithLeadingTrivia(xmlDoc);
|
||||
|
||||
return method;
|
||||
@@ -230,7 +234,7 @@ public class ImplicitHandlerBuilderExtensionsGenerator : IIncrementalGenerator
|
||||
private static IEnumerable<ConstructorDeclarationSyntax> GetConstructors(ClassDeclarationSyntax classDeclaration)
|
||||
=> classDeclaration.Members.OfType<ConstructorDeclarationSyntax>().Where(ctor => ctor.Modifiers.HasModifiers("public"));
|
||||
|
||||
private static MethodDeclarationSyntax FindTargetterMethod(Dictionary<string, MethodDeclarationSyntax> targeters, ClassDeclarationSyntax classDeclaration)
|
||||
private static MethodDeclarationSyntax? FindTargetterMethod(Dictionary<string, MethodDeclarationSyntax> targeters, ClassDeclarationSyntax classDeclaration)
|
||||
{
|
||||
if (targeters.TryGetValue(classDeclaration.Identifier.ValueText, out MethodDeclarationSyntax targeter))
|
||||
return targeter;
|
||||
@@ -238,7 +242,7 @@ public class ImplicitHandlerBuilderExtensionsGenerator : IIncrementalGenerator
|
||||
if (classDeclaration.BaseList != null && targeters.TryGetValue(classDeclaration.BaseList.Types.ElementAt(0).Type.ToString(), out targeter))
|
||||
return targeter;
|
||||
|
||||
throw new TargteterNotFoundException();
|
||||
return null;
|
||||
}
|
||||
|
||||
private static SyntaxTriviaList BuildExtensionXmlDocTrivia(ClassDeclarationSyntax classDeclaration, ParameterListSyntax methodParameters)
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
namespace Telegrator.RoslynGenerators.RoslynExtensions
|
||||
{
|
||||
public static class MemberDeclarationSyntaxExtensions
|
||||
{
|
||||
private static SyntaxTrivia TabulationTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, "\t");
|
||||
private static SyntaxTrivia WhitespaceTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " ");
|
||||
private static SyntaxTrivia NewLineTrivia => SyntaxFactory.SyntaxTrivia(SyntaxKind.EndOfLineTrivia, "\n");
|
||||
|
||||
public static SyntaxTokenList Decorate(this SyntaxTokenList tokens)
|
||||
=> new SyntaxTokenList(tokens.Select(token => token.WithoutTrivia().WithTrailingTrivia(WhitespaceTrivia)).ToArray());
|
||||
|
||||
public static BlockSyntax DecorateBlock(this BlockSyntax block, int times) => block
|
||||
.WithStatements([.. block.Statements.Select(statement => statement.DecorateStatememnt(times + 1))])
|
||||
.WithOpenBraceToken(SyntaxFactory.Token(SyntaxKind.OpenBraceToken).WithLeadingTrivia(WhitespaceTrivia).WithTrailingTrivia(NewLineTrivia))
|
||||
.WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken).WithLeadingTrivia(TabulationTrivia.Repeat(times)).WithTrailingTrivia(NewLineTrivia));
|
||||
|
||||
public static T DecorateStatememnt<T>(this T statememnt, int times) where T : StatementSyntax => statememnt
|
||||
.WithoutTrivia().WithLeadingTrivia(TabulationTrivia.Repeat(times)).WithTrailingTrivia(NewLineTrivia);
|
||||
|
||||
public static T DecorateMember<T>(this T typeDeclaration, int times) where T : MemberDeclarationSyntax => typeDeclaration
|
||||
.WithoutTrivia().WithLeadingTrivia(TabulationTrivia.Repeat(times)).WithTrailingTrivia(NewLineTrivia);
|
||||
|
||||
public static NamespaceDeclarationSyntax Decorate(this NamespaceDeclarationSyntax namespaceDeclaration) => namespaceDeclaration
|
||||
.WithName(namespaceDeclaration.Name.WithoutTrivia().WithLeadingTrivia(WhitespaceTrivia))
|
||||
.WithOpenBraceToken(SyntaxFactory.Token(SyntaxKind.OpenBraceToken).WithLeadingTrivia(NewLineTrivia).WithTrailingTrivia(NewLineTrivia))
|
||||
.WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken));
|
||||
|
||||
public static T DecorateType<T>(this T typeDeclaration, int times = 1) where T : TypeDeclarationSyntax => (T)typeDeclaration
|
||||
.WithoutTrivia().WithLeadingTrivia(TabulationTrivia.Repeat(times))
|
||||
.WithIdentifier(typeDeclaration.Identifier.WithoutTrivia().WithLeadingTrivia(WhitespaceTrivia).WithTrailingTrivia(NewLineTrivia))
|
||||
.WithOpenBraceToken(SyntaxFactory.Token(SyntaxKind.OpenBraceToken).WithLeadingTrivia(TabulationTrivia.Repeat(times)).WithTrailingTrivia(NewLineTrivia))
|
||||
.WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken).WithLeadingTrivia(TabulationTrivia.Repeat(times)).WithTrailingTrivia(NewLineTrivia));
|
||||
}
|
||||
}
|
||||
+29
-35
@@ -47,7 +47,7 @@ This guide will walk you through the core concepts and advanced features of **Te
|
||||
**Telegrator** is distributed as a NuGet package. You can install it using the .NET CLI, the NuGet Package Manager Console, or by managing NuGet packages in Visual Studio.
|
||||
|
||||
### Prerequisites
|
||||
- .NET >= 5.0 `or` .NET Core >= 2.0 `or` Framework >= 4.6.1 (.NET Standard 2.0 compatible)
|
||||
- .NET >= 5.0 `or` .NET Core >= 2.0 `or` Framework >= 4.6.1 (.NET Standard 2.1 compatible)
|
||||
- A Telegram Bot Token from [@BotFather](https://t.me/BotFather).
|
||||
|
||||
### .NET CLI
|
||||
@@ -61,7 +61,7 @@ Install-Package Telegrator
|
||||
```
|
||||
|
||||
### Hosting Integrations
|
||||
- .NET Core >= 8.0
|
||||
- .NET Core >= 10.0
|
||||
- `Telegrator.Hosting`: For console/background services
|
||||
- `Telegrator.Hosting.Web`: For ASP.NET Core/Webhook
|
||||
|
||||
@@ -325,7 +325,7 @@ builder.Handlers.AddMethod<CallbackQuery>(Option1Handler);
|
||||
```csharp
|
||||
public enum UserState
|
||||
{
|
||||
Start = SpecialState.NoState,
|
||||
Start,
|
||||
WaitingForName,
|
||||
WaitingForAge
|
||||
}
|
||||
@@ -333,39 +333,40 @@ public enum UserState
|
||||
// Start conversation
|
||||
[CommandHandler]
|
||||
[CommandAlias("register")]
|
||||
[EnumState<UserState>(UserState.Start)]
|
||||
[State<UserState>(UserState.Start)]
|
||||
private static async Task<Result> StartRegistration(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
||||
{
|
||||
container.ForwardEnumState<UserState>();
|
||||
StateStorage.GetStateMachine<UserState>().BySenderId().Advance();
|
||||
await container.Reply("Please enter your name:", cancellationToken: cancellationToken);
|
||||
return Ok;
|
||||
}
|
||||
|
||||
// Handle name input
|
||||
[MessageHandler]
|
||||
[EnumState<UserState>(UserState.WaitingForName)]
|
||||
[State<UserState>(UserState.WaitingForName)]
|
||||
private static async Task<Result> HandleName(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
||||
{
|
||||
var name = container.Input.Text;
|
||||
container.ForwardEnumState<UserState>();
|
||||
StateStorage.GetStateMachine<UserState>().BySenderId().Advance();
|
||||
await container.Reply($"Hello {name}! Please enter your age:", cancellationToken: cancellationToken);
|
||||
return Ok;
|
||||
}
|
||||
|
||||
// Handle age input
|
||||
[MessageHandler]
|
||||
[EnumState<UserState>(UserState.WaitingForAge)]
|
||||
[State<UserState>(UserState.WaitingForAge)]
|
||||
private static async Task<Result> HandleAge(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
||||
{
|
||||
if (int.TryParse(container.Input.Text, out int age))
|
||||
{
|
||||
container.DeleteEnumState<UserState>();
|
||||
StateStorage.GetStateMachine<UserState>().BySenderId().Reset();
|
||||
await container.Reply($"Registration complete! Name: {name}, Age: {age}", cancellationToken: cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
await container.Reply("Please enter a valid age (number):", cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
return Ok;
|
||||
}
|
||||
|
||||
@@ -497,50 +498,43 @@ public class RestrictedHandler : MessageHandler
|
||||
|
||||
### 3.3. State Management
|
||||
|
||||
Telegrator provides built-in state management for multi-step conversations (wizards, forms, quizzes) without a database.
|
||||
Telegrator provides built-in state management for multi-step conversations (wizards, forms, quizzes) with or without a database.
|
||||
|
||||
> [!NOTE]
|
||||
> Each type of `StateKeeper`'s (EnumStateKeeper, NumericStateKeeper) is shared between **EVERY** handler in project.
|
||||
|
||||
**Types of State:**
|
||||
- **NumericState**: Integer-based steps
|
||||
- **StringState**: Named steps
|
||||
- **EnumState**: Enum-based scenarios
|
||||
> Each type of `StateKeeper`'s keys and states are shared between **EVERY** handler in project.
|
||||
|
||||
**How to Use:**
|
||||
1. Define your state (enum/int/string)
|
||||
2. Use a state filter attribute on your handler:
|
||||
- `[EnumState<MyEnum>(MyEnum.Step1)]`
|
||||
- `[NumericState(1)]`
|
||||
- `[StringState("waiting_input")]`
|
||||
- `[State<MyEnum>(MyEnum.Step1)]`
|
||||
3. Change state inside the handler using extension methods:
|
||||
- `container.ForwardEnumState<MyEnum>()`
|
||||
- `container.ForwardNumericState()`
|
||||
- `container.ForwardStringState()`
|
||||
- `container.DeleteEnumState<MyEnum>()`
|
||||
- `StateStorage.GetStateMachine<MyEnum>().BySenderId().Current()`
|
||||
- `StateStorage.GetStateMachine<MyEnum>().BySenderId().Advance()`
|
||||
- `StateStorage.GetStateMachine<MyEnum>().BySenderId().Retreat()`
|
||||
- `StateStorage.GetStateMachine<MyEnum>().BySenderId().Reset()`
|
||||
|
||||
**Example:**
|
||||
```csharp
|
||||
public enum QuizState
|
||||
{
|
||||
Start = SpecialState.NoState, Q1, Q2
|
||||
Start, Q1, Q2
|
||||
}
|
||||
|
||||
[CommandHandler]
|
||||
[CommandAlias("quiz")]
|
||||
[EnumState<QuizState>(QuizState.Start)]
|
||||
[State<QuizState>(QuizState.Start)]
|
||||
public class StartQuizHandler : CommandHandler
|
||||
{
|
||||
public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
|
||||
{
|
||||
container.ForwardEnumState<QuizState>();
|
||||
StateStorage.GetStateMachine<QuizState>().BySenderId().Advance();
|
||||
await Reply("Quiz started! Question 1: What is the capital of France?");
|
||||
return Ok;
|
||||
}
|
||||
}
|
||||
|
||||
[MessageHandler]
|
||||
[EnumState<QuizState>(QuizState.Q1)]
|
||||
[State<QuizState>(QuizState.Q1)]
|
||||
public class Q1Handler : MessageHandler
|
||||
{
|
||||
public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
|
||||
@@ -550,7 +544,7 @@ public class Q1Handler : MessageHandler
|
||||
else
|
||||
await Reply("Incorrect. The answer is Paris.");
|
||||
|
||||
container.ForwardEnumState<QuizState>();
|
||||
StateStorage.GetStateMachine<QuizState>().BySenderId().Advance();
|
||||
await Reply("Question 2: What is 2 + 2?");
|
||||
return Ok;
|
||||
}
|
||||
@@ -559,8 +553,8 @@ public class Q1Handler : MessageHandler
|
||||
|
||||
> **How is it working?**
|
||||
> 1. **Enum State Definition**: `QuizState` enum defines the conversation flow with `Start = SpecialState.NoState` indicating no initial state.
|
||||
> 2. **State Filter**: `[EnumState<QuizState>(QuizState.Start)]` ensures the handler only runs when the user is in the "Start" state.
|
||||
> 3. **State Transition**: `container.ForwardEnumState<QuizState>()` moves the user to the next state (Q1).
|
||||
> 2. **State Filter**: `[State<QuizState>(QuizState.Start)]` ensures the handler only runs when the user is in the "Start" state.
|
||||
> 3. **State Transition**: `StateStorage.GetStateMachine<QuizState>().BySenderId().Advance()` moves the user to the next state (Q1).
|
||||
> 4. **Next Handler**: The `Q1Handler` will only run when the user is in state `QuizState.Q1`.
|
||||
> 5. **State Management**: Each handler manages its own state transition, creating a clear conversation flow.
|
||||
|
||||
@@ -1425,28 +1419,28 @@ public enum UserState
|
||||
// Start conversation
|
||||
[CommandHandler]
|
||||
[CommandAlias("register")]
|
||||
[EnumState<UserState>(UserState.Start)]
|
||||
[State<UserState>(UserState.Start)]
|
||||
private static async Task<Result> StartRegistration(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
||||
{
|
||||
container.ForwardEnumState<UserState>();
|
||||
StateStorage.GetStateMachine<UserState>().BySenderId().Advance();
|
||||
await container.Reply("Please enter your name:", cancellationToken: cancellationToken);
|
||||
return Ok;
|
||||
}
|
||||
|
||||
// Handle name input
|
||||
[MessageHandler]
|
||||
[EnumState<UserState>(UserState.WaitingForName)]
|
||||
[State<UserState>(UserState.WaitingForName)]
|
||||
private static async Task<Result> HandleName(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
||||
{
|
||||
var name = container.Input.Text;
|
||||
container.ForwardEnumState<UserState>();
|
||||
StateStorage.GetStateMachine<UserState>().BySenderId().Advance();
|
||||
await container.Reply($"Hello {name}! Please enter your age:", cancellationToken: cancellationToken);
|
||||
return Ok;
|
||||
}
|
||||
|
||||
// Handle age input
|
||||
[MessageHandler]
|
||||
[EnumState<UserState>(UserState.WaitingForAge)]
|
||||
[State<UserState>(UserState.WaitingForAge)]
|
||||
private static async Task<Result> HandleAge(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
||||
{
|
||||
if (int.TryParse(container.Input.Text, out int age))
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0"?>
|
||||
<doc>
|
||||
<assembly>
|
||||
<name>Telegartor.RedisStateStorage</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="T:Telegrator.States.RedisStateStorage">
|
||||
<summary>
|
||||
Provides a Redis-based implementation of the <see cref="T:Telegrator.Core.States.IStateStorage"/> interface.
|
||||
Serializes state objects to JSON format before storing them in the Redis database.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Telegrator.States.RedisStateStorage.#ctor(StackExchange.Redis.IConnectionMultiplexer)">
|
||||
<summary>
|
||||
Provides a Redis-based implementation of the <see cref="T:Telegrator.Core.States.IStateStorage"/> interface.
|
||||
Serializes state objects to JSON format before storing them in the Redis database.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Telegrator.States.RedisStateStorage.SetAsync``1(System.String,``0,System.Threading.CancellationToken)">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Telegrator.States.RedisStateStorage.GetAsync``1(System.String,System.Threading.CancellationToken)">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Telegrator.States.RedisStateStorage.DeleteAsync(System.String,System.Threading.CancellationToken)">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
||||
@@ -100,6 +100,12 @@
|
||||
<member name="P:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Environment">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="P:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Properties">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="P:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Metrics">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Microsoft.AspNetCore.Builder.WebApplicationOptions)">
|
||||
<summary>
|
||||
Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class.
|
||||
@@ -107,6 +113,14 @@
|
||||
<param name="webApplicationBuilder"></param>
|
||||
<param name="settings"></param>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Telegrator.TelegratorOptions,Microsoft.AspNetCore.Builder.WebApplicationOptions)">
|
||||
<summary>
|
||||
Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class.
|
||||
</summary>
|
||||
<param name="webApplicationBuilder"></param>
|
||||
<param name="options"></param>
|
||||
<param name="settings"></param>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Telegrator.Core.IHandlersCollection,Microsoft.AspNetCore.Builder.WebApplicationOptions)">
|
||||
<summary>
|
||||
Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class.
|
||||
@@ -115,12 +129,24 @@
|
||||
<param name="handlers"></param>
|
||||
<param name="settings"></param>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Telegrator.Core.IHandlersCollection,Telegrator.TelegratorOptions,Microsoft.AspNetCore.Builder.WebApplicationOptions)">
|
||||
<summary>
|
||||
Initializes a new instance of the <see cref="T:Telegrator.Hosting.Web.TelegramBotWebHostBuilder"/> class.
|
||||
</summary>
|
||||
<param name="webApplicationBuilder"></param>
|
||||
<param name="handlers"></param>
|
||||
<param name="options"></param>
|
||||
<param name="settings"></param>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.Build">
|
||||
<summary>
|
||||
Builds the host.
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHostBuilder.ConfigureContainer``1(Microsoft.Extensions.DependencyInjection.IServiceProviderFactory{``0},System.Action{``0})">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="T:Telegrator.Hosting.Web.WebhookerOptions">
|
||||
<summary>
|
||||
Configuration options for Telegram bot behavior and execution settings.
|
||||
@@ -185,7 +211,7 @@
|
||||
<member name="M:Telegrator.ServicesCollectionExtensions.get_Handlers(Microsoft.AspNetCore.Builder.WebApplicationBuilder)">
|
||||
<inheritdoc cref="P:Telegrator.ServicesCollectionExtensions.<G>$41F16C2D39AF52899E745C9C9F42FF83.Handlers"/>
|
||||
</member>
|
||||
<member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWeb(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Microsoft.AspNetCore.Builder.WebApplicationOptions,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
|
||||
<member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWeb(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
|
||||
<summary>
|
||||
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
|
||||
</summary>
|
||||
|
||||
+39
-32
@@ -13,7 +13,7 @@
|
||||
<param name="services"></param>
|
||||
<param name="configuration"></param>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.HostedTelegramBotInfo.#ctor(Telegram.Bot.ITelegramBotClient,System.IServiceProvider,Microsoft.Extensions.Configuration.IConfigurationManager)">
|
||||
<member name="M:Telegrator.Hosting.HostedTelegramBotInfo.#ctor(Telegram.Bot.ITelegramBotClient,System.IServiceProvider,Microsoft.Extensions.Configuration.IConfiguration)">
|
||||
<summary>
|
||||
Implementation of <see cref="T:Telegrator.Core.ITelegramBotInfo"/> that provides bot information.
|
||||
Contains metadata about the Telegram bot including user details and service provider for wider filterring abilities
|
||||
@@ -35,27 +35,6 @@
|
||||
Provides access to configuration of this Hosted telegram bot
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Telegrator.Hosting.ITelegramBotHostBuilder">
|
||||
<summary>
|
||||
Interface for building Telegram bot hosts with dependency injection support.
|
||||
Combines host application building capabilities with handler collection functionality.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Telegrator.Hosting.ITelegramBotHostBuilder.Configuration">
|
||||
<summary>
|
||||
Gets the set of key/value configuration properties.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Telegrator.Hosting.ITelegramBotHostBuilder.Logging">
|
||||
<summary>
|
||||
Gets a collection of logging providers for the application to compose. This is useful for adding new logging providers.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Telegrator.Hosting.ITelegramBotHostBuilder.Services">
|
||||
<summary>
|
||||
Gets a collection of services for the application to compose. This is useful for adding user provided or framework provided services.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Telegrator.Hosting.TelegramBotHost">
|
||||
<summary>
|
||||
Represents a hosted telegram bot
|
||||
@@ -133,6 +112,12 @@
|
||||
<member name="P:Telegrator.Hosting.TelegramBotHostBuilder.Environment">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="P:Telegrator.Hosting.TelegramBotHostBuilder.Properties">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="P:Telegrator.Hosting.TelegramBotHostBuilder.Metrics">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder,Microsoft.Extensions.Hosting.HostApplicationBuilderSettings)">
|
||||
<summary>
|
||||
Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class.
|
||||
@@ -140,6 +125,14 @@
|
||||
<param name="hostApplicationBuilder"></param>
|
||||
<param name="settings"></param>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder,Telegrator.TelegratorOptions,Microsoft.Extensions.Hosting.HostApplicationBuilderSettings)">
|
||||
<summary>
|
||||
Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class.
|
||||
</summary>
|
||||
<param name="hostApplicationBuilder"></param>
|
||||
<param name="options"></param>
|
||||
<param name="settings"></param>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder,Telegrator.Core.IHandlersCollection,Microsoft.Extensions.Hosting.HostApplicationBuilderSettings)">
|
||||
<summary>
|
||||
Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class.
|
||||
@@ -148,12 +141,24 @@
|
||||
<param name="handlers"></param>
|
||||
<param name="settings"></param>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.#ctor(Microsoft.Extensions.Hosting.HostApplicationBuilder,Telegrator.Core.IHandlersCollection,Telegrator.TelegratorOptions,Microsoft.Extensions.Hosting.HostApplicationBuilderSettings)">
|
||||
<summary>
|
||||
Initializes a new instance of the <see cref="T:Telegrator.Hosting.TelegramBotHostBuilder"/> class.
|
||||
</summary>
|
||||
<param name="hostApplicationBuilder"></param>
|
||||
<param name="handlers"></param>
|
||||
<param name="options"></param>
|
||||
<param name="settings"></param>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.Build">
|
||||
<summary>
|
||||
Builds the host.
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Telegrator.Hosting.TelegramBotHostBuilder.ConfigureContainer``1(Microsoft.Extensions.DependencyInjection.IServiceProviderFactory{``0},System.Action{``0})">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="T:Telegrator.Logging.MicrosoftLoggingAdapter">
|
||||
<summary>
|
||||
Adapter for Microsoft.Extensions.Logging to work with Telegrator logging system.
|
||||
@@ -190,12 +195,6 @@
|
||||
<member name="M:Telegrator.Polling.HostedUpdateReceiver.ExecuteAsync(System.Threading.CancellationToken)">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="T:Telegrator.Polling.HostUpdateHandlersPool">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Telegrator.Polling.HostUpdateHandlersPool.#ctor(Telegrator.Core.IUpdateRouter,Microsoft.Extensions.Options.IOptions{Telegrator.TelegratorOptions})">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="T:Telegrator.Polling.HostUpdateRouter">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
@@ -204,7 +203,7 @@
|
||||
<see cref="T:Microsoft.Extensions.Logging.ILogger"/> of this router
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Telegrator.Polling.HostUpdateRouter.#ctor(Telegrator.Core.IHandlersProvider,Telegrator.Core.IAwaitingProvider,Microsoft.Extensions.Options.IOptions{Telegrator.TelegratorOptions},Telegrator.Core.IUpdateHandlersPool,Telegrator.Core.ITelegramBotInfo,Microsoft.Extensions.Logging.ILogger{Telegrator.Polling.HostUpdateRouter})">
|
||||
<member name="M:Telegrator.Polling.HostUpdateRouter.#ctor(Telegrator.Core.IHandlersProvider,Telegrator.Core.IAwaitingProvider,Telegrator.Core.States.IStateStorage,Microsoft.Extensions.Options.IOptions{Telegrator.TelegratorOptions},Telegrator.Core.ITelegramBotInfo,Microsoft.Extensions.Logging.ILogger{Telegrator.Polling.HostUpdateRouter})">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Telegrator.Polling.HostUpdateRouter.HandleUpdateAsync(Telegram.Bot.ITelegramBotClient,Telegram.Bot.Types.Update,System.Threading.CancellationToken)">
|
||||
@@ -222,7 +221,7 @@
|
||||
<member name="T:Telegrator.Providers.HostAwaitingProvider">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Telegrator.Providers.HostAwaitingProvider.#ctor(Microsoft.Extensions.Options.IOptions{Telegrator.TelegratorOptions},Microsoft.Extensions.Logging.ILogger{Telegrator.Providers.HostAwaitingProvider})">
|
||||
<member name="M:Telegrator.Providers.HostAwaitingProvider.#ctor(Microsoft.Extensions.Options.IOptions{Telegrator.TelegratorOptions})">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="T:Telegrator.Providers.HostHandlersCollection">
|
||||
@@ -240,7 +239,7 @@
|
||||
<member name="T:Telegrator.Providers.HostHandlersProvider">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Telegrator.Providers.HostHandlersProvider.#ctor(Telegrator.Core.IHandlersCollection,Microsoft.Extensions.Options.IOptions{Telegrator.TelegratorOptions},System.IServiceProvider,Microsoft.Extensions.Logging.ILogger{Telegrator.Providers.HostHandlersProvider})">
|
||||
<member name="M:Telegrator.Providers.HostHandlersProvider.#ctor(Telegrator.Core.IHandlersCollection,Microsoft.Extensions.Options.IOptions{Telegrator.TelegratorOptions},System.IServiceProvider)">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Telegrator.Providers.HostHandlersProvider.GetHandlerInstance(Telegrator.Core.Descriptors.HandlerDescriptor,System.Threading.CancellationToken)">
|
||||
@@ -259,7 +258,7 @@
|
||||
<member name="M:Telegrator.HostBuilderExtensions.get_Handlers(Microsoft.Extensions.Hosting.IHostApplicationBuilder)">
|
||||
<inheritdoc cref="P:Telegrator.HostBuilderExtensions.<G>$605D8CCF64349EA050C790D67C500BD9.Handlers"/>
|
||||
</member>
|
||||
<member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Microsoft.Extensions.Hosting.HostApplicationBuilderSettings,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
|
||||
<member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
|
||||
<summary>
|
||||
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
|
||||
</summary>
|
||||
@@ -275,6 +274,14 @@
|
||||
Provides method to configure Telegram Bot Host
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Telegrator.ServicesCollectionExtensions.AddStateStorage``1(Microsoft.Extensions.DependencyInjection.IServiceCollection)">
|
||||
<summary>
|
||||
Registers <see cref="T:Telegrator.Core.States.IStateStorage"/> service
|
||||
</summary>
|
||||
<typeparam name="TStorage"></typeparam>
|
||||
<param name="services"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Telegrator.ServicesCollectionExtensions.AddTelegramBotHostDefaults(Microsoft.Extensions.DependencyInjection.IServiceCollection)">
|
||||
<summary>
|
||||
Registers <see cref="T:Telegrator.Hosting.TelegramBotHost"/> default services
|
||||
|
||||
+480
-900
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Telegrator" Version="1.16.2"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,11 @@
|
||||
using Telegrator;
|
||||
|
||||
namespace Telegrator.Examples;
|
||||
|
||||
public class EchoBot
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var client = new TelegratorClient();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
# Telegrator.RedisStateStorage
|
||||
|
||||
**Telegrator.RedisStateStorage** is an extension for the Telegrator framework that provides Redis powered IStateStorage implementation.
|
||||
|
||||
---
|
||||
|
||||
## Requirements
|
||||
- .NET standart 2.1 or later
|
||||
- [Telegrator](https://github.com/Rikitav/Telegrator)
|
||||
|
||||
---
|
||||
|
||||
## Installation
|
||||
|
||||
```shell
|
||||
dotnet add package Telegrator.RedisStateStorage
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Quick Start Example
|
||||
|
||||
**Program.cs:**
|
||||
```csharp
|
||||
using Telegrator.Hosting;
|
||||
|
||||
// Creating builder
|
||||
TelegramBotHostBuilder builder = TelegramBotHost.CreateBuilder(new HostApplicationBuilderSettings()
|
||||
{
|
||||
Args = args,
|
||||
ApplicationName = "TelegramBotHost example",
|
||||
});
|
||||
|
||||
// Registerring handlers
|
||||
builder.Handlers.CollectHandlersAssemblyWide();
|
||||
|
||||
// Register your services and
|
||||
builder.Services.AddService<IStateStorage, RedisStateStorage>(services =>
|
||||
new RedisStateStorage(ConnectionMultiplexer.Connect("server1:6379, server2:6379")));
|
||||
|
||||
// Building and running application
|
||||
TelegramBotHost telegramBot = builder.Build();
|
||||
telegramBot.SetBotCommands();
|
||||
telegramBot.Run();
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Documentation
|
||||
- [Telegrator Main Docs](https://github.com/Rikitav/Telegrator)
|
||||
- [Getting Started Guide](https://github.com/Rikitav/Telegrator/wiki/Getting-started)
|
||||
- [Annotation Overview](https://github.com/Rikitav/Telegrator/wiki/Annotation-overview)
|
||||
|
||||
---
|
||||
|
||||
## License
|
||||
GPLv3
|
||||
@@ -0,0 +1,39 @@
|
||||
using StackExchange.Redis;
|
||||
using System.Text.Json;
|
||||
using Telegrator.Core.States;
|
||||
|
||||
namespace Telegrator.States;
|
||||
|
||||
/// <summary>
|
||||
/// Provides a Redis-based implementation of the <see cref="IStateStorage"/> interface.
|
||||
/// Serializes state objects to JSON format before storing them in the Redis database.
|
||||
/// </summary>
|
||||
public class RedisStateStorage(IConnectionMultiplexer redis) : IStateStorage
|
||||
{
|
||||
private readonly IDatabase _db = redis.GetDatabase();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task SetAsync<T>(string key, T state, CancellationToken cancellationToken)
|
||||
{
|
||||
string json = JsonSerializer.Serialize(state);
|
||||
await _db.StringSetAsync(key, json);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<T?> GetAsync<T>(string key, CancellationToken cancellationToken)
|
||||
{
|
||||
RedisValue json = await _db.StringGetAsync(key);
|
||||
string? jsonStr = json;
|
||||
|
||||
if (jsonStr is null)
|
||||
return default;
|
||||
|
||||
return JsonSerializer.Deserialize<T?>(json: jsonStr);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task DeleteAsync(string key, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _db.KeyDeleteAsync(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<RootNamespace>Telegrator</RootNamespace>
|
||||
<BaseOutputPath>..\..\bin</BaseOutputPath>
|
||||
<DocumentationFile>..\..\docs\$(AssemblyName).xml</DocumentationFile>
|
||||
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
<EnableNETAnalyzers>True</EnableNETAnalyzers>
|
||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||
|
||||
<Title>Telegrator.RedisStateStorage</Title>
|
||||
<Version>1.16.5</Version>
|
||||
<Authors>Rikitav Tim4ik</Authors>
|
||||
<Company>Rikitav Tim4ik</Company>
|
||||
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
|
||||
<PackageTags>telegram;bot;mediator;attributes;aspect;hosting;host;framework;easy;simple;handlers</PackageTags>
|
||||
|
||||
<PackageIcon>telegrator_nuget.png</PackageIcon>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Telegrator\Telegrator.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.11.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include=".\README.md" Pack="True" PackagePath="\" />
|
||||
<None Include="..\..\LICENSE" Pack="True" PackagePath="\" />
|
||||
<None Include="..\..\resources\telegrator_nuget.png" Pack="True" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -9,3 +9,4 @@ using System.Diagnostics.CodeAnalysis;
|
||||
[assembly: SuppressMessage("Style", "IDE0090")]
|
||||
[assembly: SuppressMessage("Usage", "CA2254")]
|
||||
[assembly: SuppressMessage("Maintainability", "CA1510")]
|
||||
[assembly: SuppressMessage("Style", "IDE0270")]
|
||||
|
||||
@@ -7,13 +7,13 @@ using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Telegrator.Core;
|
||||
|
||||
namespace Telegrator.Hosting.Web
|
||||
namespace Telegrator.Hosting.Web;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a web hosted telegram bot
|
||||
/// </summary>
|
||||
public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuilder, IAsyncDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a web hosted telegram bot
|
||||
/// </summary>
|
||||
public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuilder, IAsyncDisposable
|
||||
{
|
||||
private readonly WebApplication _innerApp;
|
||||
private readonly IUpdateRouter _updateRouter;
|
||||
private readonly ILogger<TelegramBotWebHost> _logger;
|
||||
@@ -53,7 +53,6 @@ namespace Telegrator.Hosting.Web
|
||||
{
|
||||
// Building proxy application
|
||||
_innerApp = webApplicationBuilder.Build();
|
||||
_innerApp.UseTelegratorWeb();
|
||||
|
||||
// Reruesting services for this host
|
||||
_updateRouter = Services.GetRequiredService<IUpdateRouter>();
|
||||
@@ -157,5 +156,4 @@ namespace Telegrator.Hosting.Web
|
||||
GC.SuppressFinalize(this);
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Diagnostics.Metrics;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Telegrator.Core;
|
||||
|
||||
#pragma warning disable IDE0001
|
||||
namespace Telegrator.Hosting.Web
|
||||
namespace Telegrator.Hosting.Web;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
|
||||
/// </summary>
|
||||
public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
|
||||
/// </summary>
|
||||
public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder
|
||||
{
|
||||
private readonly WebApplicationBuilder _innerBuilder;
|
||||
private readonly WebApplicationOptions _settings;
|
||||
internal IHandlersCollection _handlers = null!;
|
||||
@@ -32,17 +33,37 @@ namespace Telegrator.Hosting.Web
|
||||
/// <inheritdoc/>
|
||||
public IHostEnvironment Environment => _innerBuilder.Environment;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IDictionary<object, object> Properties => ((IHostApplicationBuilder)_innerBuilder).Properties;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IMetricsBuilder Metrics => _innerBuilder.Metrics;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TelegramBotWebHostBuilder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="webApplicationBuilder"></param>
|
||||
/// <param name="settings"></param>
|
||||
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, WebApplicationOptions settings)
|
||||
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, WebApplicationOptions? settings = null)
|
||||
{
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
|
||||
_innerBuilder.AddTelegratorWeb();
|
||||
this.AddTelegratorWeb();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TelegramBotWebHostBuilder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="webApplicationBuilder"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <param name="settings"></param>
|
||||
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegratorOptions? options, WebApplicationOptions? settings)
|
||||
{
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
|
||||
this.AddTelegratorWeb(options, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -56,7 +77,22 @@ namespace Telegrator.Hosting.Web
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
|
||||
_innerBuilder.AddTelegratorWeb(null, handlers);
|
||||
this.AddTelegratorWeb(null, handlers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TelegramBotWebHostBuilder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="webApplicationBuilder"></param>
|
||||
/// <param name="handlers"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <param name="settings"></param>
|
||||
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers, TelegratorOptions? options, WebApplicationOptions settings)
|
||||
{
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
|
||||
this.AddTelegratorWeb(options, handlers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -69,5 +105,10 @@ namespace Telegrator.Hosting.Web
|
||||
host.UseTelegrator();
|
||||
return host;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull
|
||||
{
|
||||
((IHostApplicationBuilder)_innerBuilder).ConfigureContainer(factory, configure);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Telegrator.Hosting.Web
|
||||
namespace Telegrator.Hosting.Web;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration options for Telegram bot behavior and execution settings.
|
||||
/// Controls various aspects of bot operation including concurrency, routing, webhook receiving, and execution policies.
|
||||
/// </summary>
|
||||
public class WebhookerOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration options for Telegram bot behavior and execution settings.
|
||||
/// Controls various aspects of bot operation including concurrency, routing, webhook receiving, and execution policies.
|
||||
/// </summary>
|
||||
public class WebhookerOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets HTTPS URL to send updates to. Use an empty string to remove webhook integration
|
||||
/// </summary>
|
||||
@@ -31,5 +31,4 @@ namespace Telegrator.Hosting.Web
|
||||
/// Pass true to drop all pending updates
|
||||
/// </summary>
|
||||
public bool DropPendingUpdates { get; set; } = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,13 +10,13 @@ using Telegram.Bot.Types;
|
||||
using Telegrator.Core;
|
||||
using Telegrator.Hosting.Web;
|
||||
|
||||
namespace Telegrator.Mediation
|
||||
namespace Telegrator.Mediation;
|
||||
|
||||
/// <summary>
|
||||
/// Service for receiving updates for Hosted telegram bots via Webhooks
|
||||
/// </summary>
|
||||
public class HostedUpdateWebhooker : IHostedService
|
||||
{
|
||||
/// <summary>
|
||||
/// Service for receiving updates for Hosted telegram bots via Webhooks
|
||||
/// </summary>
|
||||
public class HostedUpdateWebhooker : IHostedService
|
||||
{
|
||||
private const string SecretTokenHeader = "X-Telegram-Bot-Api-Secret-Token";
|
||||
|
||||
private readonly IEndpointRouteBuilder _botHost;
|
||||
@@ -93,5 +93,4 @@ namespace Telegrator.Mediation
|
||||
await _updateRouter.HandleUpdateAsync(_botClient, update, ctx.RequestAborted);
|
||||
return Results.Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Telegrator.Hosting;
|
||||
using Telegrator.Hosting.Web;
|
||||
@@ -43,10 +44,14 @@ internal class Program
|
||||
|
||||
public static void TelegramBotHostBuilder_Example(string[] args)
|
||||
{
|
||||
ConfigurationManager configuration = new ConfigurationManager();
|
||||
configuration.AddJsonFile("appsettings.json");
|
||||
|
||||
TelegramBotHostBuilder builder = TelegramBotHost.CreateBuilder(new HostApplicationBuilderSettings()
|
||||
{
|
||||
Args = args,
|
||||
ApplicationName = "TelegramBotHost example",
|
||||
Configuration = configuration
|
||||
});
|
||||
|
||||
builder.Handlers.CollectHandlersAssemblyWide();
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||
|
||||
<Title>Telegrator.Hosting.Web</Title>
|
||||
<Version>1.16.1</Version>
|
||||
<Version>1.16.5</Version>
|
||||
<Authors>Rikitav Tim4ik</Authors>
|
||||
<Company>Rikitav Tim4ik</Company>
|
||||
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
|
||||
|
||||
@@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Telegram.Bot;
|
||||
using Telegrator.Core;
|
||||
using Telegrator.Hosting;
|
||||
using Telegrator.Hosting.Web;
|
||||
using Telegrator.Mediation;
|
||||
using Telegrator.Providers;
|
||||
|
||||
@@ -9,3 +9,4 @@ using System.Diagnostics.CodeAnalysis;
|
||||
[assembly: SuppressMessage("Style", "IDE0090")]
|
||||
[assembly: SuppressMessage("Usage", "CA2254")]
|
||||
[assembly: SuppressMessage("Maintainability", "CA1510")]
|
||||
[assembly: SuppressMessage("Style", "IDE0270")]
|
||||
|
||||
@@ -3,17 +3,17 @@ using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Core;
|
||||
|
||||
namespace Telegrator.Hosting
|
||||
namespace Telegrator.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="ITelegramBotInfo"/> that provides bot information.
|
||||
/// Contains metadata about the Telegram bot including user details and service provider for wider filterring abilities
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public class HostedTelegramBotInfo(ITelegramBotClient client, IServiceProvider services, IConfiguration configuration) : ITelegramBotInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="ITelegramBotInfo"/> that provides bot information.
|
||||
/// Contains metadata about the Telegram bot including user details and service provider for wider filterring abilities
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public class HostedTelegramBotInfo(ITelegramBotClient client, IServiceProvider services, IConfigurationManager configuration) : ITelegramBotInfo
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public User User { get; } = client.GetMe().Result;
|
||||
|
||||
@@ -25,6 +25,5 @@ namespace Telegrator.Hosting
|
||||
/// <summary>
|
||||
/// Provides access to configuration of this Hosted telegram bot
|
||||
/// </summary>
|
||||
public IConfigurationManager Configuration { get; } = configuration;
|
||||
}
|
||||
public IConfiguration Configuration { get; } = configuration;
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Telegrator.Core;
|
||||
|
||||
namespace Telegrator.Hosting
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for building Telegram bot hosts with dependency injection support.
|
||||
/// Combines host application building capabilities with handler collection functionality.
|
||||
/// </summary>
|
||||
public interface ITelegramBotHostBuilder : ICollectingProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the set of key/value configuration properties.
|
||||
/// </summary>
|
||||
IConfigurationManager Configuration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of logging providers for the application to compose. This is useful for adding new logging providers.
|
||||
/// </summary>
|
||||
ILoggingBuilder Logging { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of services for the application to compose. This is useful for adding user provided or framework provided services.
|
||||
/// </summary>
|
||||
IServiceCollection Services { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,23 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Telegrator.Core;
|
||||
|
||||
namespace Telegrator.Hosting
|
||||
namespace Telegrator.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a hosted telegram bot
|
||||
/// </summary>
|
||||
public class TelegramBotHost : IHost, ITelegratorBot
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a hosted telegram bot
|
||||
/// </summary>
|
||||
public class TelegramBotHost : IHost, ITelegratorBot
|
||||
{
|
||||
private readonly IHost _innerHost;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IUpdateRouter _updateRouter;
|
||||
private readonly ILogger<TelegramBotHost> _logger;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IServiceProvider Services => _serviceProvider;
|
||||
public IServiceProvider Services => _innerHost.Services;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IUpdateRouter UpdateRouter => _updateRouter;
|
||||
@@ -40,8 +38,6 @@ namespace Telegrator.Hosting
|
||||
|
||||
// Building proxy hoster
|
||||
_innerHost = hostApplicationBuilder.Build();
|
||||
_serviceProvider = _innerHost.Services;
|
||||
_innerHost.UseTelegrator();
|
||||
|
||||
// Reruesting services for this host
|
||||
_updateRouter = Services.GetRequiredService<IUpdateRouter>();
|
||||
@@ -56,9 +52,6 @@ namespace Telegrator.Hosting
|
||||
{
|
||||
HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings: null);
|
||||
TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, null);
|
||||
|
||||
builder.Services.AddTelegramBotHostDefaults();
|
||||
builder.Services.AddTelegramReceiver();
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -70,9 +63,6 @@ namespace Telegrator.Hosting
|
||||
{
|
||||
HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings);
|
||||
TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, settings);
|
||||
|
||||
builder.Services.AddTelegramBotHostDefaults();
|
||||
builder.Services.AddTelegramReceiver();
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -121,5 +111,4 @@ namespace Telegrator.Hosting
|
||||
GC.SuppressFinalize(this);
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Diagnostics.Metrics;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Telegrator.Core;
|
||||
using Telegrator.Providers;
|
||||
|
||||
#pragma warning disable IDE0001
|
||||
namespace Telegrator.Hosting
|
||||
namespace Telegrator.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
|
||||
/// </summary>
|
||||
public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
|
||||
/// </summary>
|
||||
public class TelegramBotHostBuilder : ICollectingProvider
|
||||
{
|
||||
private readonly HostApplicationBuilder _innerBuilder;
|
||||
private readonly HostApplicationBuilderSettings _settings;
|
||||
internal IHandlersCollection _handlers = null!;
|
||||
@@ -32,6 +32,12 @@ namespace Telegrator.Hosting
|
||||
/// <inheritdoc/>
|
||||
public IHostEnvironment Environment => _innerBuilder.Environment;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IDictionary<object, object> Properties => ((IHostApplicationBuilder)_innerBuilder).Properties;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IMetricsBuilder Metrics => _innerBuilder.Metrics;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TelegramBotHostBuilder"/> class.
|
||||
/// </summary>
|
||||
@@ -42,8 +48,21 @@ namespace Telegrator.Hosting
|
||||
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
|
||||
_settings = settings ?? new HostApplicationBuilderSettings();
|
||||
|
||||
_innerBuilder.AddTelegrator();
|
||||
_innerBuilder.Logging.ClearProviders();
|
||||
this.AddTelegrator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TelegramBotHostBuilder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="hostApplicationBuilder"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <param name="settings"></param>
|
||||
public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, TelegratorOptions? options, HostApplicationBuilderSettings? settings)
|
||||
{
|
||||
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
|
||||
_settings = settings ?? new HostApplicationBuilderSettings();
|
||||
|
||||
this.AddTelegrator(options, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -52,13 +71,27 @@ namespace Telegrator.Hosting
|
||||
/// <param name="hostApplicationBuilder"></param>
|
||||
/// <param name="handlers"></param>
|
||||
/// <param name="settings"></param>
|
||||
public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers, HostApplicationBuilderSettings? settings = null)
|
||||
public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers, HostApplicationBuilderSettings? settings)
|
||||
{
|
||||
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
|
||||
_settings = settings ?? new HostApplicationBuilderSettings();
|
||||
|
||||
_innerBuilder.AddTelegrator(null, handlers);
|
||||
_innerBuilder.Logging.ClearProviders();
|
||||
this.AddTelegrator(null, handlers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TelegramBotHostBuilder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="hostApplicationBuilder"></param>
|
||||
/// <param name="handlers"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <param name="settings"></param>
|
||||
public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers, TelegratorOptions? options, HostApplicationBuilderSettings? settings)
|
||||
{
|
||||
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
|
||||
_settings = settings ?? new HostApplicationBuilderSettings();
|
||||
|
||||
this.AddTelegrator(options, handlers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -71,5 +104,10 @@ namespace Telegrator.Hosting
|
||||
host.UseTelegrator();
|
||||
return host;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull
|
||||
{
|
||||
this.ConfigureContainer(factory, configure);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
namespace Telegrator.Logging;
|
||||
|
||||
namespace Telegrator.Logging
|
||||
/// <summary>
|
||||
/// Adapter for Microsoft.Extensions.Logging to work with Telegrator logging system.
|
||||
/// This allows seamless integration with ASP.NET Core logging infrastructure.
|
||||
/// </summary>
|
||||
public class MicrosoftLoggingAdapter : ITelegratorLogger
|
||||
{
|
||||
/// <summary>
|
||||
/// Adapter for Microsoft.Extensions.Logging to work with Telegrator logging system.
|
||||
/// This allows seamless integration with ASP.NET Core logging infrastructure.
|
||||
/// </summary>
|
||||
public class MicrosoftLoggingAdapter : ITelegratorLogger
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly Microsoft.Extensions.Logging.ILogger _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of MicrosoftLoggingAdapter.
|
||||
/// </summary>
|
||||
/// <param name="logger">The Microsoft.Extensions.Logging logger instance.</param>
|
||||
public MicrosoftLoggingAdapter(ILogger logger)
|
||||
public MicrosoftLoggingAdapter(Microsoft.Extensions.Logging.ILogger logger)
|
||||
{
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
@@ -24,11 +22,11 @@ namespace Telegrator.Logging
|
||||
{
|
||||
var msLogLevel = level switch
|
||||
{
|
||||
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,
|
||||
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,
|
||||
_ => Microsoft.Extensions.Logging.LogLevel.Information
|
||||
};
|
||||
|
||||
@@ -41,5 +39,4 @@ namespace Telegrator.Logging
|
||||
_logger.Log(msLogLevel, default, message, null, (str, _) => str);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Telegrator.Core;
|
||||
using Telegrator.Mediation;
|
||||
|
||||
namespace Telegrator.Polling
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public class HostUpdateHandlersPool(IUpdateRouter router, IOptions<TelegratorOptions> options)
|
||||
: UpdateHandlersPool(router, options.Value, options.Value.GlobalCancellationToken)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,14 @@ using Telegram.Bot;
|
||||
using Telegram.Bot.Polling;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Core;
|
||||
using Telegrator.Core.States;
|
||||
using Telegrator.Mediation;
|
||||
|
||||
namespace Telegrator.Polling
|
||||
namespace Telegrator.Polling;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public class HostUpdateRouter : UpdateRouter
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public class HostUpdateRouter : UpdateRouter
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="ILogger"/> of this router
|
||||
/// </summary>
|
||||
@@ -20,10 +21,10 @@ namespace Telegrator.Polling
|
||||
public HostUpdateRouter(
|
||||
IHandlersProvider handlersProvider,
|
||||
IAwaitingProvider awaitingProvider,
|
||||
IStateStorage stateStorage,
|
||||
IOptions<TelegratorOptions> options,
|
||||
IUpdateHandlersPool handlersPool,
|
||||
ITelegramBotInfo botInfo,
|
||||
ILogger<HostUpdateRouter> logger) : base(handlersProvider, awaitingProvider, options.Value, handlersPool, botInfo)
|
||||
ILogger<HostUpdateRouter> logger) : base(handlersProvider, awaitingProvider, stateStorage, options.Value, botInfo)
|
||||
{
|
||||
Logger = logger;
|
||||
ExceptionHandler = new DefaultRouterExceptionHandler(HandleException);
|
||||
@@ -55,5 +56,4 @@ namespace Telegrator.Polling
|
||||
|
||||
Logger.LogError("Exception was thrown during update routing faulted :\n{exception}", exception.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,17 +6,17 @@ using Telegram.Bot.Polling;
|
||||
using Telegrator.Core;
|
||||
using Telegrator.Mediation;
|
||||
|
||||
namespace Telegrator.Polling
|
||||
namespace Telegrator.Polling;
|
||||
|
||||
/// <summary>
|
||||
/// Service for receiving updates for Hosted telegram bots
|
||||
/// </summary>
|
||||
/// <param name="botClient"></param>
|
||||
/// <param name="updateRouter"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <param name="logger"></param>
|
||||
public class HostedUpdateReceiver(ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<ReceiverOptions> options, ILogger<HostedUpdateReceiver> logger) : BackgroundService
|
||||
{
|
||||
/// <summary>
|
||||
/// Service for receiving updates for Hosted telegram bots
|
||||
/// </summary>
|
||||
/// <param name="botClient"></param>
|
||||
/// <param name="updateRouter"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <param name="logger"></param>
|
||||
public class HostedUpdateReceiver(ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<ReceiverOptions> options, ILogger<HostedUpdateReceiver> logger) : BackgroundService
|
||||
{
|
||||
private readonly ReceiverOptions _receiverOptions = options.Value;
|
||||
private readonly IUpdateRouter _updateRouter = updateRouter;
|
||||
|
||||
@@ -28,5 +28,4 @@ namespace Telegrator.Polling
|
||||
DefaultUpdateReceiver updateReceiver = new DefaultUpdateReceiver(botClient, _receiverOptions);
|
||||
await updateReceiver.ReceiveAsync(_updateRouter, stoppingToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Telegrator.Providers
|
||||
namespace Telegrator.Providers;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public class HostAwaitingProvider(IOptions<TelegratorOptions> options) : AwaitingProvider(options.Value)
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public class HostAwaitingProvider(IOptions<TelegratorOptions> options, ILogger<HostAwaitingProvider> logger) : AwaitingProvider(options.Value)
|
||||
{
|
||||
private readonly ILogger<HostAwaitingProvider> _logger = logger;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
using Telegrator.Core;
|
||||
using Telegrator.Core.Descriptors;
|
||||
|
||||
namespace Telegrator.Providers
|
||||
namespace Telegrator.Providers;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public class HostHandlersCollection(IServiceCollection hostServiceColletion, TelegratorOptions options) : HandlersCollection(options)
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public class HostHandlersCollection(IServiceCollection hostServiceColletion, TelegratorOptions options) : HandlersCollection(options)
|
||||
{
|
||||
private readonly IServiceCollection Services = hostServiceColletion;
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -58,5 +58,4 @@ namespace Telegrator.Providers
|
||||
|
||||
return base.AddDescriptor(descriptor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,23 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Telegrator.Core;
|
||||
using Telegrator.Core.Descriptors;
|
||||
using Telegrator.Core.Handlers;
|
||||
|
||||
namespace Telegrator.Providers
|
||||
namespace Telegrator.Providers;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public class HostHandlersProvider : HandlersProvider
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public class HostHandlersProvider : HandlersProvider
|
||||
{
|
||||
private readonly IServiceProvider Services;
|
||||
private readonly ILogger<HostHandlersProvider> Logger;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public HostHandlersProvider(
|
||||
IHandlersCollection handlers,
|
||||
IOptions<TelegratorOptions> options,
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<HostHandlersProvider> logger) : base(handlers, options.Value)
|
||||
IServiceProvider serviceProvider) : base(handlers, options.Value)
|
||||
{
|
||||
Services = serviceProvider;
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -41,5 +37,4 @@ namespace Telegrator.Providers
|
||||
updateHandler.LifetimeToken.OnLifetimeEnded += _ => scope.Dispose();
|
||||
return updateHandler;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||
|
||||
<Title>Telegrator.Hosting</Title>
|
||||
<Version>1.16.1</Version>
|
||||
<Version>1.16.5</Version>
|
||||
<Authors>Rikitav Tim4ik</Authors>
|
||||
<Company>Rikitav Tim4ik</Company>
|
||||
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
|
||||
|
||||
@@ -11,10 +11,12 @@ using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Core;
|
||||
using Telegrator.Core.Descriptors;
|
||||
using Telegrator.Core.States;
|
||||
using Telegrator.Hosting;
|
||||
using Telegrator.Logging;
|
||||
using Telegrator.Polling;
|
||||
using Telegrator.Providers;
|
||||
using Telegrator.States;
|
||||
|
||||
namespace Telegrator;
|
||||
|
||||
@@ -112,6 +114,18 @@ public static class HostBuilderExtensions
|
||||
/// </summary>
|
||||
public static class ServicesCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Registers <see cref="IStateStorage"/> service
|
||||
/// </summary>
|
||||
/// <typeparam name="TStorage"></typeparam>
|
||||
/// <param name="services"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddStateStorage<TStorage>(this IServiceCollection services) where TStorage : IStateStorage
|
||||
{
|
||||
services.Replace(new ServiceDescriptor(typeof(IStateStorage), typeof(TStorage), ServiceLifetime.Singleton));
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers <see cref="TelegramBotHost"/> default services
|
||||
/// </summary>
|
||||
@@ -120,11 +134,11 @@ public static class ServicesCollectionExtensions
|
||||
public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services)
|
||||
{
|
||||
services.AddLogging(builder => builder.AddConsole().AddDebug());
|
||||
services.AddSingleton<IUpdateHandlersPool, HostUpdateHandlersPool>();
|
||||
services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>();
|
||||
services.AddSingleton<IHandlersProvider, HostHandlersProvider>();
|
||||
services.AddSingleton<IUpdateRouter, HostUpdateRouter>();
|
||||
services.AddSingleton<ITelegramBotInfo, HostedTelegramBotInfo>();
|
||||
services.AddSingleton<IStateStorage, DefaultStateStorage>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -4,14 +4,13 @@ using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Handlers;
|
||||
|
||||
namespace Telegrator.Localized
|
||||
namespace Telegrator.Localized;
|
||||
|
||||
public static class LocalizedMessageHandlerExtensions
|
||||
{
|
||||
public static class LocalizedMessageHandlerExtensions
|
||||
{
|
||||
public static async Task<Message> ResponseLocalized(this ILocalizedHandler<Message> localizedHandler, string localizedReplyIdentifier, params IEnumerable<string> formatArgs)
|
||||
{
|
||||
LocalizedString localizedString = localizedHandler.LocalizationProvider[localizedReplyIdentifier, formatArgs];
|
||||
return await localizedHandler.Container.Responce(localizedString.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
@@ -4,15 +4,15 @@ using Telegrator.Attributes;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base attribute for filtering callback-based updates.
|
||||
/// Supports various message types including regular messages, edited messages, channel posts, and business messages.
|
||||
/// </summary>
|
||||
/// <param name="filters">The filters to apply to messages</param>
|
||||
public abstract class CallbackQueryAttribute(params IFilter<CallbackQuery>[] filters) : UpdateFilterAttribute<CallbackQuery>(filters)
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstract base attribute for filtering callback-based updates.
|
||||
/// Supports various message types including regular messages, edited messages, channel posts, and business messages.
|
||||
/// </summary>
|
||||
/// <param name="filters">The filters to apply to messages</param>
|
||||
public abstract class CallbackQueryAttribute(params IFilter<CallbackQuery>[] filters) : UpdateFilterAttribute<CallbackQuery>(filters)
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the allowed update types that this filter can process.
|
||||
/// </summary>
|
||||
@@ -25,20 +25,19 @@ namespace Telegrator.Annotations
|
||||
/// <returns>The message from the update, or null if not present</returns>
|
||||
public override CallbackQuery? GetFilterringTarget(Update update)
|
||||
=> update.CallbackQuery;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering <see cref="CallbackQuery"/>'s data
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
public class CallbackDataAttribute(string data)
|
||||
: CallbackQueryAttribute(new CallbackDataFilter(data))
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute to check if <see cref="CallbackQuery"/> belongs to a specific message by its ID
|
||||
/// </summary>
|
||||
public class CallbackInlineIdAttribute(string inlineMessageId)
|
||||
: CallbackQueryAttribute(new CallbackInlineIdFilter(inlineMessageId))
|
||||
{ }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering <see cref="CallbackQuery"/>'s data
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
public class CallbackDataAttribute(string data)
|
||||
: CallbackQueryAttribute(new CallbackDataFilter(data))
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute to check if <see cref="CallbackQuery"/> belongs to a specific message by its ID
|
||||
/// </summary>
|
||||
public class CallbackInlineIdAttribute(string inlineMessageId)
|
||||
: CallbackQueryAttribute(new CallbackInlineIdFilter(inlineMessageId))
|
||||
{ }
|
||||
|
||||
@@ -3,14 +3,14 @@ using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Filters;
|
||||
using Telegrator.Attributes;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on command aliases.
|
||||
/// Allows handlers to respond to multiple command variations using a single attribute.
|
||||
/// </summary>
|
||||
public class CommandAlliasAttribute : UpdateFilterAttribute<Message>
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on command aliases.
|
||||
/// Allows handlers to respond to multiple command variations using a single attribute.
|
||||
/// </summary>
|
||||
public class CommandAlliasAttribute : UpdateFilterAttribute<Message>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the allowed update types for this filter.
|
||||
/// </summary>
|
||||
@@ -56,5 +56,4 @@ namespace Telegrator.Annotations
|
||||
/// <param name="update">The Telegram update.</param>
|
||||
/// <returns>The message from the update, or null if not present.</returns>
|
||||
public override Message? GetFilterringTarget(Update update) => update.Message;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,63 +1,62 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command has arguments count >= <paramref name="count"/>.
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
public class ArgumentCountAttribute(int count)
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command has arguments count >= <paramref name="count"/>.
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
public class ArgumentCountAttribute(int count)
|
||||
: MessageFilterAttribute(new ArgumentCountFilter(count))
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command argument starts with the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The content that the command argument should start with.</param>
|
||||
/// <param name="comparison">The string comparison type to use for the check.</param>
|
||||
/// <param name="index">The index of the argument to check (0-based).</param>
|
||||
public class ArgumentStartsWithAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command argument starts with the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The content that the command argument should start with.</param>
|
||||
/// <param name="comparison">The string comparison type to use for the check.</param>
|
||||
/// <param name="index">The index of the argument to check (0-based).</param>
|
||||
public class ArgumentStartsWithAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentStartsWithFilter(content, comparison, index))
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command argument ends with the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The content that the command argument should end with.</param>
|
||||
/// <param name="comparison">The string comparison type to use for the check.</param>
|
||||
/// <param name="index">The index of the argument to check (0-based).</param>
|
||||
public class ArgumentEndsWithAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command argument ends with the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The content that the command argument should end with.</param>
|
||||
/// <param name="comparison">The string comparison type to use for the check.</param>
|
||||
/// <param name="index">The index of the argument to check (0-based).</param>
|
||||
public class ArgumentEndsWithAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentEndsWithFilter(content, comparison, index))
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command argument contains the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The content that the command argument should contain.</param>
|
||||
/// <param name="comparison">The string comparison type to use for the check.</param>
|
||||
/// <param name="index">The index of the argument to check (0-based).</param>
|
||||
public class ArgumentContainsAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command argument contains the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The content that the command argument should contain.</param>
|
||||
/// <param name="comparison">The string comparison type to use for the check.</param>
|
||||
/// <param name="index">The index of the argument to check (0-based).</param>
|
||||
public class ArgumentContainsAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentContainsFilter(content, comparison, index))
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command argument equals the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The content that the command argument should equal.</param>
|
||||
/// <param name="comparison">The string comparison type to use for the check.</param>
|
||||
/// <param name="index">The index of the argument to check (0-based).</param>
|
||||
public class ArgumentEqualsAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command argument equals the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The content that the command argument should equal.</param>
|
||||
/// <param name="comparison">The string comparison type to use for the check.</param>
|
||||
/// <param name="index">The index of the argument to check (0-based).</param>
|
||||
public class ArgumentEqualsAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentEqualsFilter(content, comparison, index))
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command argument matches a regular expression pattern.
|
||||
/// </summary>
|
||||
/// <param name="pattern">The regular expression pattern to match against the command argument.</param>
|
||||
/// <param name="options">The regex options to use for the pattern matching.</param>
|
||||
/// <param name="index">The index of the argument to check (0-based).</param>
|
||||
public class ArgumentRegexAttribute(string pattern, RegexOptions options = RegexOptions.None, int index = 0)
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command argument matches a regular expression pattern.
|
||||
/// </summary>
|
||||
/// <param name="pattern">The regular expression pattern to match against the command argument.</param>
|
||||
/// <param name="options">The regex options to use for the pattern matching.</param>
|
||||
/// <param name="index">The index of the argument to check (0-based).</param>
|
||||
public class ArgumentRegexAttribute(string pattern, RegexOptions options = RegexOptions.None, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentRegexFilter(pattern, options, index: index))
|
||||
{ }
|
||||
}
|
||||
{ }
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
namespace Telegrator.Annotations
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute that prevents a class from being automatically collected by the handler collection system.
|
||||
/// When applied to a class, it will be excluded from domain-wide handler collection operations.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
|
||||
public class DontCollectAttribute : Attribute
|
||||
{
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute that prevents a class from being automatically collected by the handler collection system.
|
||||
/// When applied to a class, it will be excluded from domain-wide handler collection operations.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
|
||||
public class DontCollectAttribute : Attribute
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,15 +4,15 @@ using Telegrator.Filters;
|
||||
using Telegrator.Attributes;
|
||||
using Telegrator.Core.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base attribute for filtering updates based on environment conditions.
|
||||
/// Can process all types of updates and provides environment-specific filtering logic.
|
||||
/// </summary>
|
||||
/// <param name="filters">The environment filters to apply</param>
|
||||
public abstract class EnvironmentFilterAttribute(params IFilter<Update>[] filters) : UpdateFilterAttribute<Update>(filters)
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstract base attribute for filtering updates based on environment conditions.
|
||||
/// Can process all types of updates and provides environment-specific filtering logic.
|
||||
/// </summary>
|
||||
/// <param name="filters">The environment filters to apply</param>
|
||||
public abstract class EnvironmentFilterAttribute(params IFilter<Update>[] filters) : UpdateFilterAttribute<Update>(filters)
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the allowed update types that this filter can process.
|
||||
/// Environment filters can process all update types.
|
||||
@@ -27,29 +27,29 @@ namespace Telegrator.Annotations
|
||||
/// <returns>The update object itself</returns>
|
||||
public override Update? GetFilterringTarget(Update update)
|
||||
=> update;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering updates that occur in debug environment.
|
||||
/// Only allows updates when the application is running in debug mode.
|
||||
/// </summary>
|
||||
public class IsDebugEnvironmentAttribute()
|
||||
/// <summary>
|
||||
/// Attribute for filtering updates that occur in debug environment.
|
||||
/// Only allows updates when the application is running in debug mode.
|
||||
/// </summary>
|
||||
public class IsDebugEnvironmentAttribute()
|
||||
: EnvironmentFilterAttribute(new IsDebugEnvironmentFilter())
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering updates that occur in release environment.
|
||||
/// Only allows updates when the application is running in release mode.
|
||||
/// </summary>
|
||||
public class IsReleaseEnvironmentAttribute()
|
||||
/// <summary>
|
||||
/// Attribute for filtering updates that occur in release environment.
|
||||
/// Only allows updates when the application is running in release mode.
|
||||
/// </summary>
|
||||
public class IsReleaseEnvironmentAttribute()
|
||||
: EnvironmentFilterAttribute(new IsReleaseEnvironmentFilter())
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering updates based on environment variable values.
|
||||
/// </summary>
|
||||
public class EnvironmentVariableAttribute : EnvironmentFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering updates based on environment variable values.
|
||||
/// </summary>
|
||||
public class EnvironmentVariableAttribute : EnvironmentFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the attribute to filter based on an environment variable with a specific value and comparison method.
|
||||
/// </summary>
|
||||
@@ -81,5 +81,4 @@ namespace Telegrator.Annotations
|
||||
/// <param name="comparison">The string comparison method</param>
|
||||
public EnvironmentVariableAttribute(string variable, StringComparison comparison)
|
||||
: base(new EnvironmentVariableFilter(variable, comparison)) { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages that contain mentions.
|
||||
/// Allows handlers to respond only to messages that mention the bot or specific users.
|
||||
/// </summary>
|
||||
public class MentionedAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages that contain mentions.
|
||||
/// Allows handlers to respond only to messages that mention the bot or specific users.
|
||||
/// </summary>
|
||||
public class MentionedAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the MentionedAttribute that matches any mention.
|
||||
/// </summary>
|
||||
@@ -36,5 +36,4 @@ namespace Telegrator.Annotations
|
||||
/// <param name="offset">The offset position where the mention should occur.</param>
|
||||
public MentionedAttribute(string mention, int offset)
|
||||
: base(new MessageHasEntityFilter(MessageEntityType.Mention, offset, null), new MentionedFilter(mention)) { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent in forum chats.
|
||||
/// </summary>
|
||||
public class ChatIsForumAttribute()
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent in forum chats.
|
||||
/// </summary>
|
||||
public class ChatIsForumAttribute()
|
||||
: MessageFilterAttribute(new MessageChatIsForumFilter())
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent in a specific chat by ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The chat ID to match</param>
|
||||
public class ChatIdAttribute(long id)
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent in a specific chat by ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The chat ID to match</param>
|
||||
public class ChatIdAttribute(long id)
|
||||
: MessageFilterAttribute(new MessageChatIdFilter(id))
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent in chats of a specific type.
|
||||
/// </summary>
|
||||
public class ChatTypeAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent in chats of a specific type.
|
||||
/// </summary>
|
||||
public class ChatTypeAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initialize new instance of <see cref="ChatTypeAttribute"/> to filter messages from chat from specific chats
|
||||
/// </summary>
|
||||
@@ -36,13 +36,13 @@ namespace Telegrator.Annotations
|
||||
/// <param name="flags"></param>
|
||||
public ChatTypeAttribute(ChatTypeFlags flags)
|
||||
: base(new MessageChatTypeFilter(flags)) { }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on the chat title.
|
||||
/// </summary>
|
||||
public class ChatTitleAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on the chat title.
|
||||
/// </summary>
|
||||
public class ChatTitleAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the attribute to filter messages from chats with a specific title and comparison method.
|
||||
/// </summary>
|
||||
@@ -57,13 +57,13 @@ namespace Telegrator.Annotations
|
||||
/// <param name="title">The chat title to match</param>
|
||||
public ChatTitleAttribute(string? title)
|
||||
: base(new MessageChatTitleFilter(title)) { }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on the chat username.
|
||||
/// </summary>
|
||||
public class ChatUsernameAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on the chat username.
|
||||
/// </summary>
|
||||
public class ChatUsernameAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the attribute to filter messages from chats with a specific username and comparison method.
|
||||
/// </summary>
|
||||
@@ -78,13 +78,13 @@ namespace Telegrator.Annotations
|
||||
/// <param name="userName">The chat username to match</param>
|
||||
public ChatUsernameAttribute(string? userName)
|
||||
: base(new MessageChatUsernameFilter(userName, StringComparison.InvariantCulture)) { }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on the chat name (first name and optionally last name).
|
||||
/// </summary>
|
||||
public class ChatNameAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on the chat name (first name and optionally last name).
|
||||
/// </summary>
|
||||
public class ChatNameAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the attribute to filter messages from chats with specific first and last names.
|
||||
/// </summary>
|
||||
@@ -101,5 +101,4 @@ namespace Telegrator.Annotations
|
||||
/// <param name="lastName">The last name to match (optional)</param>
|
||||
public ChatNameAttribute(string? firstName, string? lastName)
|
||||
: base(new MessageChatNameFilter(firstName, lastName)) { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,15 +5,15 @@ using Telegrator.Filters;
|
||||
using Telegrator.Attributes;
|
||||
using Telegrator.Core.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base attribute for filtering message-based updates.
|
||||
/// Supports various message types including regular messages, edited messages, channel posts, and business messages.
|
||||
/// </summary>
|
||||
/// <param name="filters">The filters to apply to messages</param>
|
||||
public abstract class MessageFilterAttribute(params IFilter<Message>[] filters) : UpdateFilterAttribute<Message>(filters)
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstract base attribute for filtering message-based updates.
|
||||
/// Supports various message types including regular messages, edited messages, channel posts, and business messages.
|
||||
/// </summary>
|
||||
/// <param name="filters">The filters to apply to messages</param>
|
||||
public abstract class MessageFilterAttribute(params IFilter<Message>[] filters) : UpdateFilterAttribute<Message>(filters)
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the allowed update types that this filter can process.
|
||||
/// </summary>
|
||||
@@ -45,13 +45,13 @@ namespace Telegrator.Annotations
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on regular expression patterns.
|
||||
/// </summary>
|
||||
public class MessageRegexAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on regular expression patterns.
|
||||
/// </summary>
|
||||
public class MessageRegexAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the attribute with a regex pattern and options.
|
||||
/// </summary>
|
||||
@@ -66,13 +66,13 @@ namespace Telegrator.Annotations
|
||||
/// <param name="regex">The precompiled regular expression</param>
|
||||
public MessageRegexAttribute(Regex regex)
|
||||
: base(new MessageRegexFilter(regex)) { }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages that contain dice throws with specific values.
|
||||
/// </summary>
|
||||
public class DiceThrowedAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages that contain dice throws with specific values.
|
||||
/// </summary>
|
||||
public class DiceThrowedAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the attribute to filter dice throws with a specific value.
|
||||
/// </summary>
|
||||
@@ -87,41 +87,41 @@ namespace Telegrator.Annotations
|
||||
/// <param name="value">The dice value to match</param>
|
||||
public DiceThrowedAttribute(DiceType diceType, int value)
|
||||
: base(new DiceThrowedFilter(diceType, value)) { }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages that are automatically forwarded.
|
||||
/// </summary>
|
||||
public class IsAutomaticFormwardMessageAttribute()
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages that are automatically forwarded.
|
||||
/// </summary>
|
||||
public class IsAutomaticFormwardMessageAttribute()
|
||||
: MessageFilterAttribute(new IsAutomaticFormwardMessageFilter())
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent while the user was offline.
|
||||
/// </summary>
|
||||
public class IsFromOfflineMessageAttribute()
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent while the user was offline.
|
||||
/// </summary>
|
||||
public class IsFromOfflineMessageAttribute()
|
||||
: MessageFilterAttribute(new IsFromOfflineMessageFilter())
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering service messages (e.g., user joined, left, etc.).
|
||||
/// </summary>
|
||||
public class IsServiceMessageMessageAttribute()
|
||||
/// <summary>
|
||||
/// Attribute for filtering service messages (e.g., user joined, left, etc.).
|
||||
/// </summary>
|
||||
public class IsServiceMessageMessageAttribute()
|
||||
: MessageFilterAttribute(new IsServiceMessageMessageFilter())
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering topic messages in forum chats.
|
||||
/// </summary>
|
||||
public class IsTopicMessageMessageAttribute()
|
||||
/// <summary>
|
||||
/// Attribute for filtering topic messages in forum chats.
|
||||
/// </summary>
|
||||
public class IsTopicMessageMessageAttribute()
|
||||
: MessageFilterAttribute(new IsServiceMessageMessageFilter())
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on their entities (mentions, links, etc.).
|
||||
/// </summary>
|
||||
public class MessageHasEntityAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on their entities (mentions, links, etc.).
|
||||
/// </summary>
|
||||
public class MessageHasEntityAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the attribute to filter messages with a specific entity type.
|
||||
/// </summary>
|
||||
@@ -157,5 +157,4 @@ namespace Telegrator.Annotations
|
||||
/// <param name="stringComparison">The string comparison method</param>
|
||||
public MessageHasEntityAttribute(MessageEntityType type, int offset, int? length, string content, StringComparison stringComparison = StringComparison.CurrentCulture)
|
||||
: base(new MessageHasEntityFilter(type, offset, length, content, stringComparison)) { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,26 @@
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages with reply to messages of this bot.
|
||||
/// </summary>
|
||||
public class MeRepliedAttribute()
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages with reply to messages of this bot.
|
||||
/// </summary>
|
||||
public class MeRepliedAttribute()
|
||||
: MessageFilterAttribute(new MeRepliedFilter())
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for checking message's reply chain.
|
||||
/// </summary>
|
||||
public class HasReplyAttribute(int replyDepth = 1)
|
||||
/// <summary>
|
||||
/// Attribute for checking message's reply chain.
|
||||
/// </summary>
|
||||
public class HasReplyAttribute(int replyDepth = 1)
|
||||
: MessageFilterAttribute(new MessageHasReplyFilter(replyDepth))
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Helper filter class for filters that operate on replied messages.
|
||||
/// Provides functionality to traverse reply chains and access replied message content.
|
||||
/// </summary>
|
||||
/// <param name="replyDepth"></param>
|
||||
public class FromReplyChainAttribute(int replyDepth = 1)
|
||||
/// <summary>
|
||||
/// Helper filter class for filters that operate on replied messages.
|
||||
/// Provides functionality to traverse reply chains and access replied message content.
|
||||
/// </summary>
|
||||
/// <param name="replyDepth"></param>
|
||||
public class FromReplyChainAttribute(int replyDepth = 1)
|
||||
: MessageFilterAttribute(new FromReplyChainFilter(replyDepth))
|
||||
{ }
|
||||
}
|
||||
{ }
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on the sender's username.
|
||||
/// </summary>
|
||||
public class FromUsernameAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on the sender's username.
|
||||
/// </summary>
|
||||
public class FromUsernameAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the attribute to filter messages from a specific username.
|
||||
/// </summary>
|
||||
@@ -21,13 +21,13 @@ namespace Telegrator.Annotations
|
||||
/// <param name="comparison">The string comparison method</param>
|
||||
public FromUsernameAttribute(string username, StringComparison comparison)
|
||||
: base(new FromUsernameFilter(username, comparison)) { }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on the sender's name (first name and optionally last name).
|
||||
/// </summary>
|
||||
public class FromUserAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages based on the sender's name (first name and optionally last name).
|
||||
/// </summary>
|
||||
public class FromUserAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the attribute to filter messages from a user with specific first and last names.
|
||||
/// </summary>
|
||||
@@ -59,34 +59,33 @@ namespace Telegrator.Annotations
|
||||
/// <param name="comparison">The string comparison method</param>
|
||||
public FromUserAttribute(string firstName, StringComparison comparison)
|
||||
: base(new FromUserFilter(firstName, null, comparison)) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages from a specific user ID.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user ID to match</param>
|
||||
public class FromUserIdAttribute(long userId)
|
||||
: MessageFilterAttribute(new FromUserIdFilter(userId))
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent by not bots (users).
|
||||
/// </summary>
|
||||
public class NotFromBotAttribute()
|
||||
: MessageFilterAttribute(new FromBotFilter().Not())
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent by bots.
|
||||
/// </summary>
|
||||
public class FromBotAttribute()
|
||||
: MessageFilterAttribute(new FromBotFilter())
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent by premium users.
|
||||
/// </summary>
|
||||
public class FromPremiumUserAttribute()
|
||||
: MessageFilterAttribute(new FromPremiumUserFilter())
|
||||
{ }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages from a specific user ID.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user ID to match</param>
|
||||
public class FromUserIdAttribute(long userId)
|
||||
: MessageFilterAttribute(new FromUserIdFilter(userId))
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent by not bots (users).
|
||||
/// </summary>
|
||||
public class NotFromBotAttribute()
|
||||
: MessageFilterAttribute(new FromBotFilter().Not())
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent by bots.
|
||||
/// </summary>
|
||||
public class FromBotAttribute()
|
||||
: MessageFilterAttribute(new FromBotFilter())
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages sent by premium users.
|
||||
/// </summary>
|
||||
public class FromPremiumUserAttribute()
|
||||
: MessageFilterAttribute(new FromPremiumUserFilter())
|
||||
{ }
|
||||
|
||||
@@ -1,58 +1,57 @@
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where the text starts with the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The string that the message text should start with</param>
|
||||
/// <param name="comparison">The string comparison type</param>
|
||||
public class TextStartsWithAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture)
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where the text starts with the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The string that the message text should start with</param>
|
||||
/// <param name="comparison">The string comparison type</param>
|
||||
public class TextStartsWithAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture)
|
||||
: MessageFilterAttribute(new TextStartsWithFilter(content, comparison))
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where the text ends with the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The string that the message text should end with</param>
|
||||
/// <param name="comparison">The string comparison type</param>
|
||||
public class TextEndsWithAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture)
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where the text ends with the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The string that the message text should end with</param>
|
||||
/// <param name="comparison">The string comparison type</param>
|
||||
public class TextEndsWithAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture)
|
||||
: MessageFilterAttribute(new TextEndsWithFilter(content, comparison))
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where the text contains the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The string that the message text should contain</param>
|
||||
/// <param name="comparison">The string comparison type</param>
|
||||
public class TextContainsAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture)
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where the text contains the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The string that the message text should contain</param>
|
||||
/// <param name="comparison">The string comparison type</param>
|
||||
public class TextContainsAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture)
|
||||
: MessageFilterAttribute(new TextContainsFilter(content, comparison))
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where the text equals the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The string that the message text should equal</param>
|
||||
/// <param name="comparison">The string comparison type</param>
|
||||
public class TextEqualsAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture)
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where the text equals the specified content.
|
||||
/// </summary>
|
||||
/// <param name="content">The string that the message text should equal</param>
|
||||
/// <param name="comparison">The string comparison type</param>
|
||||
public class TextEqualsAttribute(string content, StringComparison comparison = StringComparison.InvariantCulture)
|
||||
: MessageFilterAttribute(new TextEqualsFilter(content, comparison))
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages that contain any non-empty text.
|
||||
/// </summary>
|
||||
public class HasTextAttribute()
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages that contain any non-empty text.
|
||||
/// </summary>
|
||||
public class HasTextAttribute()
|
||||
: MessageFilterAttribute(new TextNotNullOrEmptyFilter())
|
||||
{ }
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where the text contains a 'word'.
|
||||
/// 'Word' must be a separate member of the text, and not have any alphabetic characters next to it.
|
||||
/// </summary>
|
||||
/// <param name="word"></param>
|
||||
/// <param name="comparison"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
public class TextContainsWordAttribute(string word, StringComparison comparison = StringComparison.InvariantCulture, int startIndex = 0)
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where the text contains a 'word'.
|
||||
/// 'Word' must be a separate member of the text, and not have any alphabetic characters next to it.
|
||||
/// </summary>
|
||||
/// <param name="word"></param>
|
||||
/// <param name="comparison"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
public class TextContainsWordAttribute(string word, StringComparison comparison = StringComparison.InvariantCulture, int startIndex = 0)
|
||||
: MessageFilterAttribute(new TextContainsWordFilter(word, comparison, startIndex))
|
||||
{ }
|
||||
}
|
||||
{ }
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute that says if this handler can await some of await types, that is not listed by its handler base.
|
||||
/// Used for automatic collecting allowed to receiving <see cref="UpdateType"/>'s.
|
||||
/// If you don't use it, you won't be able to await the updates inside handler.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public class MightAwaitAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute that says if this handler can await some of await types, that is not listed by its handler base.
|
||||
/// Used for automatic collecting allowed to receiving <see cref="UpdateType"/>'s.
|
||||
/// If you don't use it, you won't be able to await the updates inside handler.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public class MightAwaitAttribute : Attribute
|
||||
{
|
||||
private readonly UpdateType[] _updateTypes;
|
||||
|
||||
/// <summary>
|
||||
@@ -23,5 +23,4 @@ namespace Telegrator.Annotations
|
||||
/// <param name="updateTypes"></param>
|
||||
public MightAwaitAttribute(params UpdateType[] updateTypes)
|
||||
=> _updateTypes = updateTypes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Attributes;
|
||||
using Telegrator.Core.States;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering updates where resolved state matches target value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <typeparam name="TValue"></typeparam>
|
||||
/// <param name="value"></param>
|
||||
public class StateAttribute<TKey, TValue>(TValue? value) : UpdateFilterAttribute<Update>(new StateKeyFilter<TKey, TValue>(value))
|
||||
where TKey : IStateKeyResolver, new()
|
||||
where TValue : IEquatable<TValue>
|
||||
{
|
||||
/// <summary>
|
||||
/// The targetting state value.
|
||||
/// </summary>
|
||||
public TValue? Value => value;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override UpdateType[] AllowedTypes => Update.AllTypes;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override Update? GetFilterringTarget(Update update)
|
||||
{
|
||||
return update;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
using Telegrator.StateKeeping;
|
||||
using Telegrator.Attributes;
|
||||
using Telegrator.Core.StateKeeping;
|
||||
|
||||
namespace Telegrator.Annotations.StateKeeping
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for managing enum-based states in Telegram bot handlers.
|
||||
/// Provides a convenient way to associate enum values with state management functionality.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEnum">The enum type to be used for state management.</typeparam>
|
||||
public class EnumStateAttribute<TEnum> : StateKeeperAttribute<long, TEnum, EnumStateKeeper<TEnum>> where TEnum : Enum
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the EnumStateAttribute with a special state and custom key resolver.
|
||||
/// </summary>
|
||||
/// <param name="specialState">The special state to be managed.</param>
|
||||
/// <param name="keyResolver">The resolver for extracting keys from updates.</param>
|
||||
public EnumStateAttribute(SpecialState specialState, IStateKeyResolver<long> keyResolver)
|
||||
: base(specialState, keyResolver) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the EnumStateAttribute with a specific enum state and custom key resolver.
|
||||
/// </summary>
|
||||
/// <param name="myState">The specific enum state to be managed.</param>
|
||||
/// <param name="keyResolver">The resolver for extracting keys from updates.</param>
|
||||
public EnumStateAttribute(TEnum myState, IStateKeyResolver<long> keyResolver)
|
||||
: base(myState, keyResolver) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the EnumStateAttribute with a special state and default sender ID resolver.
|
||||
/// </summary>
|
||||
/// <param name="specialState">The special state to be managed.</param>
|
||||
public EnumStateAttribute(SpecialState specialState)
|
||||
: base(specialState, new SenderIdResolver()) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the EnumStateAttribute with a specific enum state and default sender ID resolver.
|
||||
/// </summary>
|
||||
/// <param name="myState">The specific enum state to be managed.</param>
|
||||
public EnumStateAttribute(TEnum myState)
|
||||
: this(myState, new SenderIdResolver()) { }
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using Telegrator.StateKeeping;
|
||||
using Telegrator.Attributes;
|
||||
using Telegrator.Core.StateKeeping;
|
||||
|
||||
namespace Telegrator.Annotations.StateKeeping
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for associating a handler or method with a numeric (integer) state keeper.
|
||||
/// Provides constructors for flexible state and key resolver configuration.
|
||||
/// </summary>
|
||||
public class NumericStateAttribute : StateKeeperAttribute<long, int, NumericStateKeeper>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the attribute with a special state and a custom key resolver.
|
||||
/// </summary>
|
||||
/// <param name="specialState">The special state to associate</param>
|
||||
/// <param name="keyResolver">The key resolver for state keeping</param>
|
||||
public NumericStateAttribute(SpecialState specialState, IStateKeyResolver<long> keyResolver)
|
||||
: base(specialState, keyResolver) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the attribute with a specific numeric state and a custom key resolver.
|
||||
/// </summary>
|
||||
/// <param name="myState">The integer state to associate</param>
|
||||
/// <param name="keyResolver">The key resolver for state keeping</param>
|
||||
public NumericStateAttribute(int myState, IStateKeyResolver<long> keyResolver)
|
||||
: base(myState, keyResolver) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the attribute with a special state and the default sender ID resolver.
|
||||
/// </summary>
|
||||
/// <param name="specialState">The special state to associate</param>
|
||||
public NumericStateAttribute(SpecialState specialState)
|
||||
: base(specialState, new SenderIdResolver()) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the attribute with a specific numeric state and the default sender ID resolver.
|
||||
/// </summary>
|
||||
/// <param name="myState">The integer state to associate</param>
|
||||
public NumericStateAttribute(int myState)
|
||||
: this(myState, new SenderIdResolver()) { }
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
namespace Telegrator.Annotations.StateKeeping
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents special states for state keeping logic.
|
||||
/// </summary>
|
||||
public enum SpecialState
|
||||
{
|
||||
/// <summary>
|
||||
/// No special state.
|
||||
/// </summary>
|
||||
None,
|
||||
/// <summary>
|
||||
/// Indicates that no state is present.
|
||||
/// </summary>
|
||||
NoState,
|
||||
/// <summary>
|
||||
/// Indicates that any state is acceptable.
|
||||
/// </summary>
|
||||
AnyState
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using Telegrator.StateKeeping;
|
||||
using Telegrator.Attributes;
|
||||
using Telegrator.Core.StateKeeping;
|
||||
|
||||
namespace Telegrator.Annotations.StateKeeping
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for associating a handler or method with a string-based state keeper.
|
||||
/// Provides various constructors for flexible state and key resolver configuration.
|
||||
/// </summary>
|
||||
public class StringStateAttribute : StateKeeperAttribute<long, string, StringStateKeeper>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the attribute with a special state and a custom key resolver.
|
||||
/// </summary>
|
||||
/// <param name="specialState">The special state to associate</param>
|
||||
/// <param name="keyResolver">The key resolver for state keeping</param>
|
||||
public StringStateAttribute(SpecialState specialState, IStateKeyResolver<long> keyResolver)
|
||||
: base(specialState, keyResolver) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the attribute with a specific state and a custom key resolver.
|
||||
/// </summary>
|
||||
/// <param name="myState">The string state to associate</param>
|
||||
/// <param name="keyResolver">The key resolver for state keeping</param>
|
||||
public StringStateAttribute(string myState, IStateKeyResolver<long> keyResolver)
|
||||
: base(myState, keyResolver) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the attribute with a special state and the default sender ID resolver.
|
||||
/// </summary>
|
||||
/// <param name="specialState">The special state to associate</param>
|
||||
public StringStateAttribute(SpecialState specialState)
|
||||
: base(specialState, new SenderIdResolver()) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the attribute with a specific state and the default sender ID resolver.
|
||||
/// </summary>
|
||||
/// <param name="myState">The string state to associate</param>
|
||||
public StringStateAttribute(string myState)
|
||||
: base(myState, new SenderIdResolver()) { }
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations.Targetted
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering message with command "start" in bot's private chats.
|
||||
/// Allows handlers to respond to "welcome" bot commands.
|
||||
/// </summary>
|
||||
public class WelcomeAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates new instance of <see cref="WelcomeAttribute"/>
|
||||
/// </summary>
|
||||
/// <param name="onlyFirst"></param>
|
||||
public WelcomeAttribute(bool onlyFirst = false) : base(
|
||||
new MessageChatTypeFilter(ChatType.Private),
|
||||
new CommandAlliasFilter("start"),
|
||||
Filter<Message>.If(ctx => !onlyFirst || ctx.Input.Id == 0))
|
||||
{ }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering message with command "start" in bot's private chats.
|
||||
/// Allows handlers to respond to "welcome" bot commands.
|
||||
/// </summary>
|
||||
public class WelcomeAttribute : MessageFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates new instance of <see cref="WelcomeAttribute"/>
|
||||
/// </summary>
|
||||
/// <param name="onlyFirst"></param>
|
||||
public WelcomeAttribute(bool onlyFirst = false) : base(
|
||||
new MessageChatTypeFilter(ChatType.Private),
|
||||
new CommandAlliasFilter("start"),
|
||||
Filter<Message>.If(ctx => !onlyFirst || ctx.Input.Id == 0))
|
||||
{ }
|
||||
}
|
||||
@@ -1,16 +1,15 @@
|
||||
namespace Telegrator.Aspects
|
||||
namespace Telegrator.Aspects;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute that specifies a post-execution processor to be executed after the handler.
|
||||
/// The processor type must implement <see cref="IPostProcessor"/> interface.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the post-processor that implements <see cref="IPostProcessor"/>.</typeparam>
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
||||
public class AfterExecutionAttribute<T> : Attribute where T : IPostProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute that specifies a post-execution processor to be executed after the handler.
|
||||
/// The processor type must implement <see cref="IPostProcessor"/> interface.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the post-processor that implements <see cref="IPostProcessor"/>.</typeparam>
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
||||
public class AfterExecutionAttribute<T> : Attribute where T : IPostProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the type of the post-processor.
|
||||
/// </summary>
|
||||
public Type ProcessorType => typeof(T);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
namespace Telegrator.Aspects
|
||||
namespace Telegrator.Aspects;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute that specifies a pre-execution processor to be executed before the handler.
|
||||
/// The processor type must implement <see cref="IPreProcessor"/> interface.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the pre-processor that implements <see cref="IPreProcessor"/>.</typeparam>
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
||||
public class BeforeExecutionAttribute<T> : Attribute where T : IPreProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute that specifies a pre-execution processor to be executed before the handler.
|
||||
/// The processor type must implement <see cref="IPreProcessor"/> interface.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the pre-processor that implements <see cref="IPreProcessor"/>.</typeparam>
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
||||
public class BeforeExecutionAttribute<T> : Attribute where T : IPreProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the type of the pre-processor.
|
||||
/// </summary>
|
||||
public Type ProcessorType => typeof(T);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
using Telegrator.Core.Handlers;
|
||||
|
||||
namespace Telegrator.Aspects
|
||||
namespace Telegrator.Aspects;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for post-execution processors that are executed after handler execution.
|
||||
/// Implement this interface to add cross-cutting concerns like logging, cleanup, or metrics collection.
|
||||
/// </summary>
|
||||
public interface IPostProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for post-execution processors that are executed after handler execution.
|
||||
/// Implement this interface to add cross-cutting concerns like logging, cleanup, or metrics collection.
|
||||
/// </summary>
|
||||
public interface IPostProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes after the handler's main execution logic.
|
||||
/// </summary>
|
||||
@@ -15,5 +15,4 @@ namespace Telegrator.Aspects
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns>A <see cref="Result"/> indicating the final execution result.</returns>
|
||||
public Task<Result> AfterExecution(IHandlerContainer container, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
using Telegrator.Core.Handlers;
|
||||
|
||||
namespace Telegrator.Aspects
|
||||
namespace Telegrator.Aspects;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for pre-execution processors that are executed before handler execution.
|
||||
/// Implement this interface to add cross-cutting concerns like validation, logging, or authorization.
|
||||
/// </summary>
|
||||
public interface IPreProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for pre-execution processors that are executed before handler execution.
|
||||
/// Implement this interface to add cross-cutting concerns like validation, logging, or authorization.
|
||||
/// </summary>
|
||||
public interface IPreProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes before the handler's main execution logic.
|
||||
/// </summary>
|
||||
@@ -15,5 +15,4 @@ namespace Telegrator.Aspects
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns>A <see cref="Result"/> indicating whether execution should continue or be stopped.</returns>
|
||||
public Task<Result> BeforeExecution(IHandlerContainer container, CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,14 @@ using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Attributes
|
||||
namespace Telegrator.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// Reactive way to implement a new <see cref="UpdateFilterAttribute{T}"/> of type <typeparamref name="T"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class FilterAnnotation<T> : UpdateFilterAttribute<T>, IFilter<T>, INamedFilter where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Reactive way to implement a new <see cref="UpdateFilterAttribute{T}"/> of type <typeparamref name="T"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class FilterAnnotation<T> : UpdateFilterAttribute<T>, IFilter<T>, INamedFilter where T : class
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public virtual bool IsCollectible { get; } = false;
|
||||
|
||||
@@ -35,5 +35,4 @@ namespace Telegrator.Attributes
|
||||
|
||||
/// <inheritdoc/>
|
||||
public abstract bool CanPass(FilterExecutionContext<T> context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
namespace Telegrator.Attributes
|
||||
namespace Telegrator.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// Enumeration of filter modifiers that can be applied to update filters.
|
||||
/// Defines how filters should be combined and applied in filter chains.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum FilterModifier
|
||||
{
|
||||
/// <summary>
|
||||
/// Enumeration of filter modifiers that can be applied to update filters.
|
||||
/// Defines how filters should be combined and applied in filter chains.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum FilterModifier
|
||||
{
|
||||
/// <summary>
|
||||
/// No modifier applied. Filter is applied as-is.
|
||||
/// </summary>
|
||||
@@ -21,5 +21,4 @@
|
||||
/// NOT modifier. The inverse of this filter should match.
|
||||
/// </summary>
|
||||
Not = 4,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Annotations.StateKeeping;
|
||||
using Telegrator.Core.Attributes;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.StateKeeping;
|
||||
|
||||
namespace Telegrator.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstract attribute for associating a handler or method with a state keeper.
|
||||
/// Provides logic for state-based filtering and state management.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">The type of the key used for state keeping (e.g., chat ID).</typeparam>
|
||||
/// <typeparam name="TState">The type of the state value (e.g., string, int).</typeparam>
|
||||
/// <typeparam name="TKeeper">The type of the state keeper implementation.</typeparam>
|
||||
public abstract class StateKeeperAttribute<TKey, TState, TKeeper> : StateKeeperAttributeBase where TKey : notnull where TState : notnull where TKeeper : StateKeeperBase<TKey, TState>, new()
|
||||
{
|
||||
/*
|
||||
private static readonly TKeeper _shared = new TKeeper();
|
||||
private static readonly Dictionary<TKey, TKeeper> _keyed = [];
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the singleton instance of the state keeper for this attribute type.
|
||||
/// </summary>
|
||||
public static TKeeper Shared { get; } = new TKeeper();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default state value of this statekeeper.
|
||||
/// </summary>
|
||||
public static TState DefaultState => Shared.DefaultState;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the state value associated with this attribute instance.
|
||||
/// </summary>
|
||||
public TState MyState { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the special state mode for this attribute instance.
|
||||
/// </summary>
|
||||
public SpecialState SpecialState { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the attribute with a specific state and a custom key resolver.
|
||||
/// </summary>
|
||||
/// <param name="myState">The state value to associate</param>
|
||||
/// <param name="keyResolver">The key resolver for state keeping</param>
|
||||
protected StateKeeperAttribute(TState myState, IStateKeyResolver<TKey> keyResolver) : base(typeof(TKeeper))
|
||||
{
|
||||
Shared.KeyResolver = keyResolver;
|
||||
MyState = myState;
|
||||
SpecialState = SpecialState.None;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the attribute with a special state and a custom key resolver.
|
||||
/// </summary>
|
||||
/// <param name="specialState">The special state mode</param>
|
||||
/// <param name="keyResolver">The key resolver for state keeping</param>
|
||||
protected StateKeeperAttribute(SpecialState specialState, IStateKeyResolver<TKey> keyResolver) : base(typeof(TKeeper))
|
||||
{
|
||||
Shared.KeyResolver = keyResolver;
|
||||
MyState = Shared.DefaultState;
|
||||
SpecialState = specialState;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the current update context passes the state filter.
|
||||
/// </summary>
|
||||
/// <param name="context">The filter execution context</param>
|
||||
/// <returns>True if the state matches the filter; otherwise, false.</returns>
|
||||
public override bool CanPass(FilterExecutionContext<Update> context)
|
||||
{
|
||||
if (SpecialState == SpecialState.AnyState)
|
||||
return true;
|
||||
|
||||
if (!Shared.TryGetState(context.Input, out TState? state))
|
||||
return SpecialState == SpecialState.NoState;
|
||||
|
||||
if (state == null)
|
||||
return false;
|
||||
|
||||
return MyState.Equals(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,15 @@ using Telegrator.Core.Attributes;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Attributes
|
||||
namespace Telegrator.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base attribute for defining update filters for a specific type of update target.
|
||||
/// Provides logic for filter composition, modifier processing, and target extraction.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the update target to filter (e.g., Message, Update).</typeparam>
|
||||
public abstract class UpdateFilterAttribute<T> : UpdateFilterAttributeBase where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstract base attribute for defining update filters for a specific type of update target.
|
||||
/// Provides logic for filter composition, modifier processing, and target extraction.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the update target to filter (e.g., Message, Update).</typeparam>
|
||||
public abstract class UpdateFilterAttribute<T> : UpdateFilterAttributeBase where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the compiled anonymous filter for this attribute.
|
||||
/// </summary>
|
||||
@@ -81,5 +81,4 @@ namespace Telegrator.Attributes
|
||||
/// <param name="update">The Telegram update</param>
|
||||
/// <returns>The target object to filter, or null if not applicable</returns>
|
||||
public abstract T? GetFilterringTarget(Update update);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,15 @@
|
||||
using Telegrator.Core.Attributes;
|
||||
using Telegrator.Core.Handlers;
|
||||
|
||||
namespace Telegrator.Attributes
|
||||
namespace Telegrator.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base attribute for marking update handler classes.
|
||||
/// Provides a type-safe way to associate handler types with specific update types and importance settings.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the update handler that this attribute is applied to.</typeparam>
|
||||
public abstract class UpdateHandlerAttribute<T> : UpdateHandlerAttributeBase where T : UpdateHandlerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstract base attribute for marking update handler classes.
|
||||
/// Provides a type-safe way to associate handler types with specific update types and importance settings.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the update handler that this attribute is applied to.</typeparam>
|
||||
public abstract class UpdateHandlerAttribute<T> : UpdateHandlerAttributeBase where T : UpdateHandlerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes new instance of <see cref="UpdateHandlerAttribute{T}"/>
|
||||
/// </summary>
|
||||
@@ -42,5 +42,4 @@ namespace Telegrator.Attributes
|
||||
/// <param name="importance">The importance level for this handler</param>
|
||||
protected UpdateHandlerAttribute(Type[] types, UpdateType updateType, int importance)
|
||||
: base([.. types, typeof(T)], updateType, importance) { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.Handlers;
|
||||
using Telegrator.Core.StateKeeping;
|
||||
|
||||
namespace Telegrator.Core.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the state in which the <see cref="UpdateHandlerBase"/> can be executed
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public abstract class StateKeeperAttributeBase : Attribute, IFilter<Update>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public bool IsCollectible => GetType().HasPublicProperties();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance <see cref="StateKeeperBase{TKey, TState}"/>
|
||||
/// </summary>
|
||||
/// <param name="stateKeeperType"></param>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
protected StateKeeperAttributeBase(Type stateKeeperType)
|
||||
{
|
||||
if (!stateKeeperType.IsAssignableToGenericType(typeof(StateKeeperBase<,>)))
|
||||
throw new ArgumentException(stateKeeperType + " is not a StateKeeperBase", nameof(stateKeeperType));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Realizes a <see cref="IFilter{T}"/> for validation of the current <see cref="StateKeeperBase{TKey, TState}"/> in the polling routing
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public abstract bool CanPass(FilterExecutionContext<Update> context);
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,14 @@ using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.Handlers;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Core.Attributes
|
||||
namespace Telegrator.Core.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IFilter{T}"/> to <see cref="Update"/> validation for entry into execution of the <see cref="UpdateHandlerBase"/>
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
|
||||
public abstract class UpdateFilterAttributeBase : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IFilter{T}"/> to <see cref="Update"/> validation for entry into execution of the <see cref="UpdateHandlerBase"/>
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
|
||||
public abstract class UpdateFilterAttributeBase : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="UpdateType"/>'s that <see cref="UpdateHandlerBase"/> processing
|
||||
/// </summary>
|
||||
@@ -43,5 +43,4 @@ namespace Telegrator.Core.Attributes
|
||||
/// <param name="previous"></param>
|
||||
/// <returns></returns>
|
||||
public abstract bool ProcessModifiers(UpdateFilterAttributeBase? previous);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,14 @@ using Telegrator.Core.Descriptors;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.Handlers;
|
||||
|
||||
namespace Telegrator.Core.Attributes
|
||||
namespace Telegrator.Core.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="UpdateType"/>'s and validator (<see cref="IFilter{T}"/>) of the <see cref="Update"/> that <see cref="UpdateHandlerBase"/> will process
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public abstract class UpdateHandlerAttributeBase : Attribute, IFilter<Update>
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the <see cref="UpdateType"/>'s and validator (<see cref="IFilter{T}"/>) of the <see cref="Update"/> that <see cref="UpdateHandlerBase"/> will process
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public abstract class UpdateHandlerAttributeBase : Attribute, IFilter<Update>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public bool IsCollectible => GetType().HasPublicProperties();
|
||||
|
||||
@@ -78,5 +78,4 @@ namespace Telegrator.Core.Attributes
|
||||
/// <param name="context">The filter execution context containing the update to validate.</param>
|
||||
/// <returns>True if the update passes validation; otherwise, false.</returns>
|
||||
public abstract bool CanPass(FilterExecutionContext<Update> context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@ using Telegrator.Core.Handlers;
|
||||
using Telegrator.Handlers;
|
||||
using Telegrator.Handlers.Building;
|
||||
|
||||
namespace Telegrator.Core.Descriptors
|
||||
namespace Telegrator.Core.Descriptors;
|
||||
|
||||
/// <summary>
|
||||
/// Descriptor for creating handlers from methods
|
||||
/// </summary>
|
||||
/// <typeparam name="TUpdate"></typeparam>
|
||||
public class MethodHandlerDescriptor<TUpdate> : HandlerDescriptor where TUpdate : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Descriptor for creating handlers from methods
|
||||
/// </summary>
|
||||
/// <typeparam name="TUpdate"></typeparam>
|
||||
public class MethodHandlerDescriptor<TUpdate> : HandlerDescriptor where TUpdate : class
|
||||
{
|
||||
private readonly MethodInfo Method;
|
||||
|
||||
/// <summary>
|
||||
@@ -24,7 +24,7 @@ namespace Telegrator.Core.Descriptors
|
||||
public MethodHandlerDescriptor(AbstractHandlerAction<TUpdate> action) : base(DescriptorType.General, typeof(MethodHandler), true)
|
||||
{
|
||||
UpdateHandlerAttributeBase handlerAttribute = HandlerInspector.GetHandlerAttribute(action.Method);
|
||||
StateKeeperAttributeBase? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(action.Method);
|
||||
IFilter<Update>? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(action.Method);
|
||||
IFilter<Update>[] filters = HandlerInspector.GetFilterAttributes(action.Method, handlerAttribute.Type).ToArray();
|
||||
|
||||
UpdateType = handlerAttribute.Type;
|
||||
@@ -66,5 +66,4 @@ namespace Telegrator.Core.Descriptors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.Handlers;
|
||||
using Telegrator.Core.States;
|
||||
|
||||
namespace Telegrator.Core.Descriptors
|
||||
namespace Telegrator.Core.Descriptors;
|
||||
|
||||
/// <summary>
|
||||
/// Contains information about a described handler, including its context, client, and execution logic.
|
||||
/// </summary>
|
||||
public class DescribedHandlerDescriptor
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains information about a described handler, including its context, client, and execution logic.
|
||||
/// </summary>
|
||||
public class DescribedHandlerDescriptor
|
||||
{
|
||||
private readonly ManualResetEventSlim ResetEvent = new ManualResetEventSlim(false);
|
||||
|
||||
/// <summary>
|
||||
@@ -27,6 +28,11 @@ namespace Telegrator.Core.Descriptors
|
||||
/// </summary>
|
||||
public IAwaitingProvider AwaitingProvider { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The state storage to handling state machines
|
||||
/// </summary>
|
||||
public IStateStorage StateStorage { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The Telegram bot client used for this handler.
|
||||
/// </summary>
|
||||
@@ -73,6 +79,7 @@ namespace Telegrator.Core.Descriptors
|
||||
/// <param name="fromDescriptor">The descriptor from which this handler was described.</param>
|
||||
/// <param name="updateRouter">The update router.</param>
|
||||
/// <param name="awaitingProvider">The awaiting provider.</param>
|
||||
/// <param name="stateStorage">The state storage.</param>
|
||||
/// <param name="client">The Telegram bot client.</param>
|
||||
/// <param name="handlerInstance">The handler instance.</param>
|
||||
/// <param name="filterContext">The filter execution context.</param>
|
||||
@@ -81,6 +88,7 @@ namespace Telegrator.Core.Descriptors
|
||||
HandlerDescriptor fromDescriptor,
|
||||
IUpdateRouter updateRouter,
|
||||
IAwaitingProvider awaitingProvider,
|
||||
IStateStorage stateStorage,
|
||||
ITelegramBotClient client,
|
||||
UpdateHandlerBase handlerInstance,
|
||||
FilterExecutionContext<Update> filterContext,
|
||||
@@ -89,6 +97,7 @@ namespace Telegrator.Core.Descriptors
|
||||
From = fromDescriptor;
|
||||
UpdateRouter = updateRouter;
|
||||
AwaitingProvider = awaitingProvider;
|
||||
StateStorage = stateStorage;
|
||||
Client = client;
|
||||
HandlerInstance = handlerInstance;
|
||||
ExtraData = filterContext.Data;
|
||||
@@ -114,7 +123,7 @@ namespace Telegrator.Core.Descriptors
|
||||
/// <param name="result">The execution result.</param>
|
||||
public void ReportResult(Result? result)
|
||||
{
|
||||
if (result != null)
|
||||
if (Result != null)
|
||||
throw new InvalidOperationException("Result already reported");
|
||||
|
||||
Result = result;
|
||||
@@ -124,5 +133,4 @@ namespace Telegrator.Core.Descriptors
|
||||
/// <inheritdoc/>
|
||||
public override string ToString()
|
||||
=> DisplayString ?? From.HandlerType.Name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
using Telegrator.Aspects;
|
||||
using Telegrator.Core.Handlers;
|
||||
|
||||
namespace Telegrator.Core.Descriptors
|
||||
namespace Telegrator.Core.Descriptors;
|
||||
|
||||
/// <summary>
|
||||
/// Manages the execution of pre and post-execution aspects for a handler.
|
||||
/// This class coordinates between self-processing (handler implements interfaces)
|
||||
/// and typed processing (external processor classes).
|
||||
/// </summary>
|
||||
public sealed class DescriptorAspectsSet
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages the execution of pre and post-execution aspects for a handler.
|
||||
/// This class coordinates between self-processing (handler implements interfaces)
|
||||
/// and typed processing (external processor classes).
|
||||
/// </summary>
|
||||
public sealed class DescriptorAspectsSet
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the type of the external pre-processor, if specified via <see cref="BeforeExecutionAttribute{T}"/>.
|
||||
/// </summary>
|
||||
@@ -76,5 +76,4 @@ namespace Telegrator.Core.Descriptors
|
||||
|
||||
return Result.Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@ using Telegrator.Core.Filters;
|
||||
using Telegrator.Handlers.Diagnostics;
|
||||
using Telegrator.Logging;
|
||||
|
||||
namespace Telegrator.Core.Descriptors
|
||||
namespace Telegrator.Core.Descriptors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a set of filters for a handler descriptor, including update and state keeper validators.
|
||||
/// </summary>
|
||||
public sealed class DescriptorFiltersSet
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a set of filters for a handler descriptor, including update and state keeper validators.
|
||||
/// </summary>
|
||||
public sealed class DescriptorFiltersSet
|
||||
{
|
||||
/// <summary>
|
||||
/// Validator for the update object.
|
||||
/// </summary>
|
||||
@@ -141,5 +141,4 @@ namespace Telegrator.Core.Descriptors
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using Telegrator.Core.Attributes;
|
||||
|
||||
namespace Telegrator.Core.Descriptors
|
||||
namespace Telegrator.Core.Descriptors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents an indexer for handler descriptors, containing importance and priority information.
|
||||
/// </summary>
|
||||
public readonly struct DescriptorIndexer(int routerIndex, int importance, int priority) : IComparable<DescriptorIndexer>
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an indexer for handler descriptors, containing importance and priority information.
|
||||
/// </summary>
|
||||
public readonly struct DescriptorIndexer(int routerIndex, int importance, int priority) : IComparable<DescriptorIndexer>
|
||||
{
|
||||
/// <summary>
|
||||
/// Index of this descriptor when it was added to router
|
||||
/// </summary>
|
||||
@@ -84,5 +84,4 @@ namespace Telegrator.Core.Descriptors
|
||||
{
|
||||
return string.Format("(Ix: {0,2}, Im: {1,2}, Pr: {2,2})", RouterIndex, Importance, Priority);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@ using Telegrator.Core.Attributes;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.Handlers;
|
||||
|
||||
namespace Telegrator.Core.Descriptors
|
||||
namespace Telegrator.Core.Descriptors;
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the type of handler descriptor.
|
||||
/// </summary>
|
||||
public enum DescriptorType
|
||||
{
|
||||
/// <summary>
|
||||
/// Specifies the type of handler descriptor.
|
||||
/// </summary>
|
||||
public enum DescriptorType
|
||||
{
|
||||
/// <summary>
|
||||
/// General handler descriptor.
|
||||
/// </summary>
|
||||
@@ -30,13 +30,13 @@ namespace Telegrator.Core.Descriptors
|
||||
/// Singleton handler descriptor (single instance).
|
||||
/// </summary>
|
||||
Singleton
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Describes a handler, its type, filters, and instantiation logic.
|
||||
/// </summary>
|
||||
public class HandlerDescriptor
|
||||
{
|
||||
/// <summary>
|
||||
/// Describes a handler, its type, filters, and instantiation logic.
|
||||
/// </summary>
|
||||
public class HandlerDescriptor
|
||||
{
|
||||
/// <summary>
|
||||
/// The type of the descriptor.
|
||||
/// </summary>
|
||||
@@ -167,7 +167,7 @@ namespace Telegrator.Core.Descriptors
|
||||
if (handlerAttribute.ExpectingHandlerType != null && !handlerAttribute.ExpectingHandlerType.Contains(handlerType.BaseType))
|
||||
throw new ArgumentException(string.Format("This handler attribute cannot be attached to this class. Attribute can be attached on next handlers : {0}", string.Join(", ", handlerAttribute.ExpectingHandlerType.AsEnumerable())));
|
||||
|
||||
StateKeeperAttributeBase? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(handlerType);
|
||||
IFilter<Update>? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(handlerType);
|
||||
IFilter<Update>[] filters = HandlerInspector.GetFilterAttributes(handlerType, handlerAttribute.Type).ToArray();
|
||||
|
||||
UpdateType = handlerAttribute.Type;
|
||||
@@ -471,5 +471,4 @@ namespace Telegrator.Core.Descriptors
|
||||
/// <inheritdoc/>
|
||||
public override string ToString()
|
||||
=> DisplayString ?? HandlerType.Name;
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,13 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace Telegrator.Core.Descriptors
|
||||
namespace Telegrator.Core.Descriptors;
|
||||
|
||||
/// <summary>
|
||||
/// The collection containing the <see cref="HandlerDescriptor"/>'s. Used to route <see cref="Update"/>'s in <see cref="IHandlersProvider"/>
|
||||
/// </summary>
|
||||
public sealed class HandlerDescriptorList : IEnumerable<HandlerDescriptor>
|
||||
{
|
||||
/// <summary>
|
||||
/// The collection containing the <see cref="HandlerDescriptor"/>'s. Used to route <see cref="Update"/>'s in <see cref="IHandlersProvider"/>
|
||||
/// </summary>
|
||||
public sealed class HandlerDescriptorList : IEnumerable<HandlerDescriptor>
|
||||
{
|
||||
private readonly object _lock = new object();
|
||||
private readonly SortedList<DescriptorIndexer, HandlerDescriptor> _innerCollection;
|
||||
private readonly TelegratorOptions? _options;
|
||||
@@ -155,5 +155,4 @@ namespace Telegrator.Core.Descriptors
|
||||
{
|
||||
return _innerCollection.Values.GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,17 +2,18 @@
|
||||
using System.Reflection;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Annotations;
|
||||
using Telegrator.Aspects;
|
||||
using Telegrator.Core.Attributes;
|
||||
using Telegrator.Core.Filters;
|
||||
|
||||
namespace Telegrator.Core.Descriptors
|
||||
namespace Telegrator.Core.Descriptors;
|
||||
|
||||
/// <summary>
|
||||
/// Provides methods for inspecting handler types and retrieving their attributes and filters.
|
||||
/// </summary>
|
||||
public static class HandlerInspector
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides methods for inspecting handler types and retrieving their attributes and filters.
|
||||
/// </summary>
|
||||
public static class HandlerInspector
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets handler's display name
|
||||
/// </summary>
|
||||
@@ -42,13 +43,13 @@ namespace Telegrator.Core.Descriptors
|
||||
/// </summary>
|
||||
/// <param name="handlerType">The member info representing the handler type.</param>
|
||||
/// <returns>The state keeper attribute, or null if not present.</returns>
|
||||
public static StateKeeperAttributeBase? GetStateKeeperAttribute(MemberInfo handlerType)
|
||||
public static IFilter<Update>? GetStateKeeperAttribute(MemberInfo handlerType)
|
||||
{
|
||||
// Getting polling handler attribute
|
||||
IEnumerable<StateKeeperAttributeBase> handlerAttrs = handlerType.GetCustomAttributes<StateKeeperAttributeBase>();
|
||||
Attribute stateAttr = handlerType.GetCustomAttribute(typeof(StateAttribute<,>));
|
||||
|
||||
//
|
||||
return handlerAttrs.Any() ? handlerAttrs.Single() : null;
|
||||
return stateAttr as IFilter<Update>;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -94,5 +95,4 @@ namespace Telegrator.Core.Descriptors
|
||||
Type? typedPost = handlerType.GetCustomAttribute(typeof(AfterExecutionAttribute<>))?.GetType().GetGenericArguments()[0];
|
||||
return new DescriptorAspectsSet(typedPre, typedPost);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
using Telegrator.Filters;
|
||||
using Telegrator.Logging;
|
||||
|
||||
namespace Telegrator.Core.Filters
|
||||
namespace Telegrator.Core.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a compiled filter that applies a set of filters to an anonymous target type.
|
||||
/// </summary>
|
||||
public class AnonymousCompiledFilter : Filter<Update>, INamedFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a compiled filter that applies a set of filters to an anonymous target type.
|
||||
/// </summary>
|
||||
public class AnonymousCompiledFilter : Filter<Update>, INamedFilter
|
||||
{
|
||||
private readonly Func<FilterExecutionContext<Update>, object, bool> FilterAction;
|
||||
private readonly Func<Update, object?> GetFilterringTarget;
|
||||
private readonly string _name;
|
||||
@@ -105,5 +105,4 @@ namespace Telegrator.Core.Filters
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
using Telegrator.Filters;
|
||||
using Telegrator.Logging;
|
||||
|
||||
namespace Telegrator.Core.Filters
|
||||
namespace Telegrator.Core.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a filter that applies a filter action to an anonymous target type extracted from an update.
|
||||
/// </summary>
|
||||
public class AnonymousTypeFilter : Filter<Update>, INamedFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a filter that applies a filter action to an anonymous target type extracted from an update.
|
||||
/// </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;
|
||||
@@ -106,5 +106,4 @@ namespace Telegrator.Core.Filters
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
using Telegrator.Filters;
|
||||
using Telegrator.Logging;
|
||||
|
||||
namespace Telegrator.Core.Filters
|
||||
namespace Telegrator.Core.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a filter that composes multiple filters and passes only if all of them pass.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the input for the filter.</typeparam>
|
||||
public class CompiledFilter<T> : Filter<T>, INamedFilter where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a filter that composes multiple filters and passes only if all of them pass.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the input for the filter.</typeparam>
|
||||
public class CompiledFilter<T> : Filter<T>, INamedFilter where T : class
|
||||
{
|
||||
private readonly IFilter<T>[] Filters;
|
||||
private readonly string _name;
|
||||
|
||||
@@ -60,5 +60,4 @@ namespace Telegrator.Core.Filters
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Telegrator.Core.Filters
|
||||
namespace Telegrator.Core.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// The list containing filters worked out during Polling to further obtain additional filtering information
|
||||
/// </summary>
|
||||
public class CompletedFiltersList : IEnumerable<IFilterCollectable>
|
||||
{
|
||||
/// <summary>
|
||||
/// The list containing filters worked out during Polling to further obtain additional filtering information
|
||||
/// </summary>
|
||||
public class CompletedFiltersList : IEnumerable<IFilterCollectable>
|
||||
{
|
||||
private readonly List<IFilterCollectable> CompletedFilters = [];
|
||||
|
||||
/// <summary>
|
||||
@@ -82,5 +82,4 @@ namespace Telegrator.Core.Filters
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator IEnumerable.GetEnumerator() => CompletedFilters.GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace Telegrator.Core.Filters
|
||||
namespace Telegrator.Core.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the context for filter execution, including update, input, and additional data.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the input for the filter.</typeparam>
|
||||
public class FilterExecutionContext<T> where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the context for filter execution, including update, input, and additional data.
|
||||
/// Gets the <see cref="ITelegramBotInfo"/> for the current context.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the input for the filter.</typeparam>
|
||||
public class FilterExecutionContext<T> where T : class
|
||||
{
|
||||
public IUpdateRouter UpdateRouter { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ITelegramBotInfo"/> for the current context.
|
||||
/// </summary>
|
||||
@@ -42,13 +47,15 @@ namespace Telegrator.Core.Filters
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FilterExecutionContext{T}"/> class with all parameters.
|
||||
/// </summary>
|
||||
/// <param name="router">The router, that invoked filter.</param>
|
||||
/// <param name="botInfo">The bot info.</param>
|
||||
/// <param name="update">The update.</param>
|
||||
/// <param name="input">The input object.</param>
|
||||
/// <param name="data">The additional data dictionary.</param>
|
||||
/// <param name="completedFilters">The list of completed filters.</param>
|
||||
public FilterExecutionContext(ITelegramBotInfo botInfo, Update update, T input, Dictionary<string, object> data, CompletedFiltersList completedFilters)
|
||||
public FilterExecutionContext(IUpdateRouter router, ITelegramBotInfo botInfo, Update update, T input, Dictionary<string, object> data, CompletedFiltersList completedFilters)
|
||||
{
|
||||
UpdateRouter = router;
|
||||
BotInfo = botInfo;
|
||||
Data = data;
|
||||
CompletedFilters = completedFilters;
|
||||
@@ -60,11 +67,12 @@ namespace Telegrator.Core.Filters
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FilterExecutionContext{T}"/> class with default data and filters.
|
||||
/// </summary>
|
||||
/// <param name="router">The router, that invoked filter.</param>
|
||||
/// <param name="botInfo">The bot info.</param>
|
||||
/// <param name="update">The update.</param>
|
||||
/// <param name="input">The input object.</param>
|
||||
public FilterExecutionContext(ITelegramBotInfo botInfo, Update update, T input)
|
||||
: this(botInfo, update, input, [], []) { }
|
||||
public FilterExecutionContext(IUpdateRouter router, ITelegramBotInfo botInfo, Update update, T input)
|
||||
: this(router, botInfo, update, input, [], []) { }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a child context for a different input type, sharing the same data and completed filters.
|
||||
@@ -73,6 +81,5 @@ namespace Telegrator.Core.Filters
|
||||
/// <param name="input">The new input object.</param>
|
||||
/// <returns>A new <see cref="FilterExecutionContext{C}"/> instance.</returns>
|
||||
public FilterExecutionContext<C> CreateChild<C>(C input) where C : class
|
||||
=> new FilterExecutionContext<C>(BotInfo, Update, input, Data, CompletedFilters);
|
||||
}
|
||||
=> new FilterExecutionContext<C>(UpdateRouter, BotInfo, Update, input, Data, CompletedFilters);
|
||||
}
|
||||
|
||||
@@ -1,39 +1,50 @@
|
||||
namespace Telegrator.Core.Filters
|
||||
namespace Telegrator.Core.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for filters that have a name for identification and debugging purposes.
|
||||
/// </summary>
|
||||
public interface INamedFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for filters that have a name for identification and debugging purposes.
|
||||
/// </summary>
|
||||
public interface INamedFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the name of the filter.
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface for filters that can be collected into a completed filters list.
|
||||
/// Provides information about whether a filter should be tracked during execution.
|
||||
/// </summary>
|
||||
public interface IFilterCollectable
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for filters that can be collected into a completed filters list.
|
||||
/// Provides information about whether a filter should be tracked during execution.
|
||||
/// </summary>
|
||||
public interface IFilterCollectable
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets if filter can be collected to <see cref="CompletedFiltersList"/>
|
||||
/// </summary>
|
||||
public bool IsCollectible { get; }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a filter for a specific update type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the update to filter.</typeparam>
|
||||
public interface IFilter<T> : IFilterCollectable where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a filter for a specific update type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the update to filter.</typeparam>
|
||||
public interface IFilter<T> : IFilterCollectable where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines whether the filter can pass for the given context.
|
||||
/// </summary>
|
||||
/// <param name="info">The filter execution context.</param>
|
||||
/// <returns>True if the filter passes; otherwise, false.</returns>
|
||||
public bool CanPass(FilterExecutionContext<T> info);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a filter that joins multiple filters together.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the input for the filter.</typeparam>
|
||||
public interface IJoinedFilter<T> : IFilter<T> where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the array of joined filters.
|
||||
/// </summary>
|
||||
public IFilter<T>[] Filters { get; }
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
namespace Telegrator.Core.Filters
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a filter that joins multiple filters together.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the input for the filter.</typeparam>
|
||||
public interface IJoinedFilter<T> : IFilter<T> where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the array of joined filters.
|
||||
/// </summary>
|
||||
public IFilter<T>[] Filters { get; }
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,16 @@ using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Core.Descriptors;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.States;
|
||||
using Telegrator.Handlers;
|
||||
|
||||
namespace Telegrator.Core.Handlers
|
||||
namespace Telegrator.Core.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract handler for Telegram updates of type <typeparamref name="TUpdate"/>.
|
||||
/// </summary>
|
||||
public abstract class AbstractUpdateHandler<TUpdate> : UpdateHandlerBase, IHandlerContainerFactory, IAbstractUpdateHandler<TUpdate> where TUpdate : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstract handler for Telegram updates of type <typeparamref name="TUpdate"/>.
|
||||
/// </summary>
|
||||
public abstract class AbstractUpdateHandler<TUpdate> : UpdateHandlerBase, IHandlerContainerFactory, IAbstractUpdateHandler<TUpdate> where TUpdate : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Handler container for the current update.
|
||||
/// </summary>
|
||||
@@ -47,6 +48,11 @@ namespace Telegrator.Core.Handlers
|
||||
/// </summary>
|
||||
protected IAwaitingProvider AwaitingProvider => Container.AwaitingProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Storage of bot states.
|
||||
/// </summary>
|
||||
protected IStateStorage StateStorage => Container.StateStorage;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance and checks that the update type matches <typeparamref name="TUpdate"/>.
|
||||
/// </summary>
|
||||
@@ -86,5 +92,4 @@ namespace Telegrator.Core.Handlers
|
||||
/// <param name="cancellation">Cancellation token.</param>
|
||||
/// <returns>A task representing the asynchronous operation.</returns>
|
||||
public abstract Task<Result> Execute(IHandlerContainer<TUpdate> container, CancellationToken cancellation);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,15 +6,15 @@ using Telegrator.Core.Descriptors;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Handlers;
|
||||
|
||||
namespace Telegrator.Core.Handlers
|
||||
namespace Telegrator.Core.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base class for handlers that support branching execution based on different methods.
|
||||
/// Allows multiple handler methods to be defined in a single class, each with its own filters.
|
||||
/// </summary>
|
||||
/// <typeparam name="TUpdate">The type of update being handled.</typeparam>
|
||||
public abstract class BranchingUpdateHandler<TUpdate> : AbstractUpdateHandler<TUpdate>, IHandlerContainerFactory, ICustomDescriptorsProvider where TUpdate : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstract base class for handlers that support branching execution based on different methods.
|
||||
/// Allows multiple handler methods to be defined in a single class, each with its own filters.
|
||||
/// </summary>
|
||||
/// <typeparam name="TUpdate">The type of update being handled.</typeparam>
|
||||
public abstract class BranchingUpdateHandler<TUpdate> : AbstractUpdateHandler<TUpdate>, IHandlerContainerFactory, ICustomDescriptorsProvider where TUpdate : class
|
||||
{
|
||||
/// <summary>
|
||||
/// The method info for the current branch being executed.
|
||||
/// </summary>
|
||||
@@ -161,5 +161,4 @@ namespace Telegrator.Core.Handlers
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Annotations.StateKeeping;
|
||||
using Telegrator.Core.Descriptors;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.StateKeeping;
|
||||
using Telegrator.Core.States;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Core.Handlers.Building
|
||||
namespace Telegrator.Core.Handlers.Building;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for building handler descriptors and managing handler filters.
|
||||
/// </summary>
|
||||
public abstract class HandlerBuilderBase(Type buildingHandlerType, UpdateType updateType, IHandlersCollection? handlerCollection) : IHandlerBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for building handler descriptors and managing handler filters.
|
||||
/// </summary>
|
||||
public abstract class HandlerBuilderBase(Type buildingHandlerType, UpdateType updateType, IHandlersCollection? handlerCollection) : IHandlerBuilder
|
||||
{
|
||||
private static int HandlerServiceKeyIndex = 0;
|
||||
|
||||
/// <summary>
|
||||
@@ -140,35 +140,15 @@ namespace Telegrator.Core.Handlers.Building
|
||||
/// <summary>
|
||||
/// Sets a state keeper for the handler using a specific state and key resolver.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">The type of the key.</typeparam>
|
||||
/// <typeparam name="TState">The type of the state.</typeparam>
|
||||
/// <typeparam name="TKeeper">The type of the state keeper.</typeparam>
|
||||
/// <param name="myState">The state value.</param>
|
||||
/// <param name="keyResolver">The key resolver.</param>
|
||||
/// <typeparam name="TKey">The key resolver.</typeparam>
|
||||
/// <typeparam name="TValue">The state value.</typeparam>
|
||||
/// <param name="state">The state value.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public void SetStateKeeper<TKey, TState, TKeeper>(TState myState, IStateKeyResolver<TKey> keyResolver)
|
||||
where TKey : notnull
|
||||
where TState : IEquatable<TState>
|
||||
where TKeeper : StateKeeperBase<TKey, TState>, new()
|
||||
public void SetState<TKey, TValue>(TValue? state)
|
||||
where TKey : IStateKeyResolver, new()
|
||||
where TValue : IEquatable<TValue>
|
||||
{
|
||||
StateKeeper = new StateKeepFilter<TKey, TState, TKeeper>(myState, keyResolver);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a state keeper for the handler using a special state and key resolver.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">The type of the key.</typeparam>
|
||||
/// <typeparam name="TState">The type of the state.</typeparam>
|
||||
/// <typeparam name="TKeeper">The type of the state keeper.</typeparam>
|
||||
/// <param name="specialState">The special state value.</param>
|
||||
/// <param name="keyResolver">The key resolver.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public void SetStateKeeper<TKey, TState, TKeeper>(SpecialState specialState, IStateKeyResolver<TKey> keyResolver)
|
||||
where TKey : notnull
|
||||
where TState : IEquatable<TState>
|
||||
where TKeeper : StateKeeperBase<TKey, TState>, new()
|
||||
{
|
||||
StateKeeper = new StateKeepFilter<TKey, TState, TKeeper>(specialState, keyResolver);
|
||||
StateKeeper = new StateKeyFilter<TKey, TValue>(state);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -196,5 +176,4 @@ namespace Telegrator.Core.Handlers.Building
|
||||
AnonymousCompiledFilter compiledPollingFilter = AnonymousCompiledFilter.Compile(filters, getFilterringTarget);
|
||||
Filters.Add(compiledPollingFilter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
using Telegrator.Core.StateKeeping;
|
||||
using Telegrator.Core.States;
|
||||
|
||||
namespace Telegrator.Core.Handlers.Building
|
||||
namespace Telegrator.Core.Handlers.Building;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a builder for awaiting handler logic for a specific update type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TUpdate">The type of update to await.</typeparam>
|
||||
public interface IAwaiterHandlerBuilder<TUpdate> : IHandlerBuilder where TUpdate : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a builder for awaiting handler logic for a specific update type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TUpdate">The type of update to await.</typeparam>
|
||||
public interface IAwaiterHandlerBuilder<TUpdate> : IHandlerBuilder where TUpdate : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Awaits an update using the specified key resolver and cancellation token.
|
||||
/// </summary>
|
||||
/// <param name="keyResolver">The <see cref="IStateKeyResolver{TKey}"/> to resolve the key.</param>
|
||||
/// <param name="keyResolver">The <see cref="IStateKeyResolver"/> to resolve the key.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>A <see cref="Task{TUpdate}"/> representing the awaited update.</returns>
|
||||
public Task<TUpdate> Await(IStateKeyResolver<long> keyResolver, CancellationToken cancellationToken = default);
|
||||
}
|
||||
public Task<TUpdate> Await(IStateKeyResolver keyResolver, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Annotations.StateKeeping;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.StateKeeping;
|
||||
using Telegrator.Core.States;
|
||||
|
||||
namespace Telegrator.Core.Handlers.Building
|
||||
namespace Telegrator.Core.Handlers.Building;
|
||||
|
||||
/// <summary>
|
||||
/// Defines builder actions for configuring handler builders.
|
||||
/// </summary>
|
||||
public interface IHandlerBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines builder actions for configuring handler builders.
|
||||
/// </summary>
|
||||
public interface IHandlerBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the update validating action for the handler.
|
||||
/// </summary>
|
||||
@@ -56,30 +55,13 @@ namespace Telegrator.Core.Handlers.Building
|
||||
/// <summary>
|
||||
/// Sets a state keeper for the handler using a specific state and key resolver.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">The type of the key.</typeparam>
|
||||
/// <typeparam name="TState">The type of the state.</typeparam>
|
||||
/// <typeparam name="TKeeper">The type of the state keeper.</typeparam>
|
||||
/// <param name="myState">The state value.</param>
|
||||
/// <param name="keyResolver">The key resolver.</param>
|
||||
/// <typeparam name="TKey">The key resolver.</typeparam>
|
||||
/// <typeparam name="TValue">The state value.</typeparam>
|
||||
/// <param name="state">The state value.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public void SetStateKeeper<TKey, TState, TKeeper>(TState myState, IStateKeyResolver<TKey> keyResolver)
|
||||
where TKey : notnull
|
||||
where TState : IEquatable<TState>
|
||||
where TKeeper : StateKeeperBase<TKey, TState>, new();
|
||||
|
||||
/// <summary>
|
||||
/// Sets a state keeper for the handler using a special state and key resolver.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">The type of the key.</typeparam>
|
||||
/// <typeparam name="TState">The type of the state.</typeparam>
|
||||
/// <typeparam name="TKeeper">The type of the state keeper.</typeparam>
|
||||
/// <param name="specialState">The special state value.</param>
|
||||
/// <param name="keyResolver">The key resolver.</param>
|
||||
/// <returns>The builder instance.</returns>
|
||||
public void SetStateKeeper<TKey, TState, TKeeper>(SpecialState specialState, IStateKeyResolver<TKey> keyResolver)
|
||||
where TKey : notnull
|
||||
where TState : IEquatable<TState>
|
||||
where TKeeper : StateKeeperBase<TKey, TState>, new();
|
||||
public void SetState<TKey, TValue>(TValue? state)
|
||||
where TKey : IStateKeyResolver, new()
|
||||
where TValue : IEquatable<TValue>;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a targeted filter for a specific filter target type.
|
||||
@@ -100,5 +82,4 @@ namespace Telegrator.Core.Handlers.Building
|
||||
/// <returns>The builder instance.</returns>
|
||||
public void AddTargetedFilters<TFilterTarget>(Func<Update, TFilterTarget?> getFilterringTarget, params IFilter<TFilterTarget>[] filters)
|
||||
where TFilterTarget : class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
using Telegrator.Handlers.Building;
|
||||
|
||||
namespace Telegrator.Core.Handlers.Building
|
||||
namespace Telegrator.Core.Handlers.Building;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a builder for regular handler logic for a specific update type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TUpdate">The type of update to handle.</typeparam>
|
||||
public interface IRegularHandlerBuilder<TUpdate> : IHandlerBuilder where TUpdate : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a builder for regular handler logic for a specific update type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TUpdate">The type of update to handle.</typeparam>
|
||||
public interface IRegularHandlerBuilder<TUpdate> : IHandlerBuilder where TUpdate : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Builds the handler logic using the specified execution delegate.
|
||||
/// </summary>
|
||||
/// <param name="executeHandler">The delegate to execute the handler logic.</param>
|
||||
public IHandlersCollection Build(AbstractHandlerAction<TUpdate> executeHandler);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Annotations.StateKeeping;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.StateKeeping;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Core.Handlers.Building
|
||||
{
|
||||
/// <summary>
|
||||
/// Filter for state keeping logic, allowing filtering based on state and special state conditions.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">The type of the key for state resolution.</typeparam>
|
||||
/// <typeparam name="TState">The type of the state.</typeparam>
|
||||
/// <typeparam name="TKeeper">The type of the state keeper.</typeparam>
|
||||
public class StateKeepFilter<TKey, TState, TKeeper> : Filter<Update>
|
||||
where TKey : notnull
|
||||
where TState : IEquatable<TState>
|
||||
where TKeeper : StateKeeperBase<TKey, TState>, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the state keeper instance.
|
||||
/// </summary>
|
||||
public static TKeeper StateKeeper { get; internal set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the state value for this filter.
|
||||
/// </summary>
|
||||
public TState MyState { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the special state value for this filter.
|
||||
/// </summary>
|
||||
public SpecialState SpecialState { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StateKeepFilter{TKey, TState, TKeeper}"/> class with a specific state.
|
||||
/// </summary>
|
||||
/// <param name="myState">The state value.</param>
|
||||
/// <param name="keyResolver">The key resolver.</param>
|
||||
public StateKeepFilter(TState myState, IStateKeyResolver<TKey> keyResolver)
|
||||
{
|
||||
StateKeeper ??= new TKeeper();
|
||||
StateKeeper.KeyResolver = keyResolver;
|
||||
MyState = myState;
|
||||
SpecialState = SpecialState.None;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StateKeepFilter{TKey, TState, TKeeper}"/> class with a special state.
|
||||
/// </summary>
|
||||
/// <param name="specialState">The special state value.</param>
|
||||
/// <param name="keyResolver">The key resolver.</param>
|
||||
public StateKeepFilter(SpecialState specialState, IStateKeyResolver<TKey> keyResolver)
|
||||
{
|
||||
StateKeeper ??= new TKeeper();
|
||||
StateKeeper.KeyResolver = keyResolver;
|
||||
MyState = StateKeeper.DefaultState;
|
||||
SpecialState = specialState;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the filter can pass for the given context based on state logic.
|
||||
/// </summary>
|
||||
/// <param name="context">The filter execution context.</param>
|
||||
/// <returns>True if the filter passes; otherwise, false.</returns>
|
||||
public override bool CanPass(FilterExecutionContext<Update> context)
|
||||
{
|
||||
if (SpecialState == SpecialState.AnyState)
|
||||
return true;
|
||||
|
||||
if (!StateKeeper.TryGetState(context.Input, out TState? state))
|
||||
return SpecialState == SpecialState.NoState;
|
||||
|
||||
if (state == null)
|
||||
return false;
|
||||
|
||||
return MyState.Equals(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,20 @@
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Core.Filters;
|
||||
|
||||
namespace Telegrator.Core.Handlers.Building
|
||||
{
|
||||
/// <summary>
|
||||
/// Delegate for validating an update in a filter context.
|
||||
/// </summary>
|
||||
/// <param name="context">The filter execution context.</param>
|
||||
/// <returns>True if the update is valid; otherwise, false.</returns>
|
||||
public delegate bool UpdateValidateAction(FilterExecutionContext<Update> context);
|
||||
namespace Telegrator.Core.Handlers.Building;
|
||||
|
||||
/// <summary>
|
||||
/// Filter that uses a delegate to validate updates.
|
||||
/// </summary>
|
||||
public class UpdateValidateFilter : IFilter<Update>
|
||||
{
|
||||
/// <summary>
|
||||
/// Delegate for validating an update in a filter context.
|
||||
/// </summary>
|
||||
/// <param name="context">The filter execution context.</param>
|
||||
/// <returns>True if the update is valid; otherwise, false.</returns>
|
||||
public delegate bool UpdateValidateAction(FilterExecutionContext<Update> context);
|
||||
|
||||
/// <summary>
|
||||
/// Filter that uses a delegate to validate updates.
|
||||
/// </summary>
|
||||
public class UpdateValidateFilter : IFilter<Update>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this filter is collectable. Always false for this filter.
|
||||
/// </summary>
|
||||
@@ -37,5 +37,4 @@ namespace Telegrator.Core.Handlers.Building
|
||||
/// <returns>True if the filter passes; otherwise, false.</returns>
|
||||
public bool CanPass(FilterExecutionContext<Update> info)
|
||||
=> UpdateValidateAction.Invoke(info);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.States;
|
||||
|
||||
namespace Telegrator.Core.Handlers
|
||||
namespace Telegrator.Core.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Represents an empty handler container that throws <see cref="NotImplementedException"/> for all members.
|
||||
/// </summary>
|
||||
public class EmptyHandlerContainer : IHandlerContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an empty handler container that throws <see cref="NotImplementedException"/> for all members.
|
||||
/// </summary>
|
||||
public class EmptyHandlerContainer : IHandlerContainer
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public Update HandlingUpdate => throw new NotImplementedException();
|
||||
|
||||
@@ -23,5 +24,7 @@ namespace Telegrator.Core.Handlers
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IAwaitingProvider AwaitingProvider => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IStateStorage StateStorage => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
namespace Telegrator.Core.Handlers
|
||||
namespace Telegrator.Core.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a token that tracks the lifetime of a handler instance.
|
||||
/// </summary>
|
||||
public class HandlerLifetimeToken
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a token that tracks the lifetime of a handler instance.
|
||||
/// </summary>
|
||||
public class HandlerLifetimeToken
|
||||
{
|
||||
/// <summary>
|
||||
/// Event triggered when the handler's lifetime has ended.
|
||||
/// </summary>
|
||||
@@ -23,5 +23,4 @@
|
||||
IsEnded = true;
|
||||
OnLifetimeEnded?.Invoke(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegrator.Core.Filters;
|
||||
using Telegrator.Core.States;
|
||||
|
||||
namespace Telegrator.Core.Handlers
|
||||
namespace Telegrator.Core.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for handler containers that provide context and resources for update handlers.
|
||||
/// Contains all necessary information and services that handlers need during execution.
|
||||
/// </summary>
|
||||
public interface IHandlerContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for handler containers that provide context and resources for update handlers.
|
||||
/// Contains all necessary information and services that handlers need during execution.
|
||||
/// </summary>
|
||||
public interface IHandlerContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="Update"/> being handled.
|
||||
/// </summary>
|
||||
@@ -34,5 +35,9 @@ namespace Telegrator.Core.Handlers
|
||||
/// Gets the <see cref="IAwaitingProvider"/> for awaiting operations.
|
||||
/// </summary>
|
||||
public IAwaitingProvider AwaitingProvider { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IStateStorage"/> for state managment.
|
||||
/// </summary>
|
||||
public IStateStorage StateStorage { get; }
|
||||
}
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
using Telegrator.Core.Descriptors;
|
||||
|
||||
namespace Telegrator.Core.Handlers
|
||||
namespace Telegrator.Core.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Factory interface for creating handler containers.
|
||||
/// Provides a way to create handler containers with specific providers and handler information.
|
||||
/// </summary>
|
||||
public interface IHandlerContainerFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Factory interface for creating handler containers.
|
||||
/// Provides a way to create handler containers with specific providers and handler information.
|
||||
/// </summary>
|
||||
public interface IHandlerContainerFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="IHandlerContainer"/> for the specified awaiting provider and handler info.
|
||||
/// </summary>
|
||||
/// <param name="handlerInfo">The <see cref="DescribedHandlerDescriptor"/> for the handler.</param>
|
||||
/// <returns>A new <see cref="IHandlerContainer"/> instance.</returns>
|
||||
public IHandlerContainer CreateContainer(DescribedHandlerDescriptor handlerInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@ using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Core.Descriptors;
|
||||
using Telegrator.Handlers.Diagnostics;
|
||||
|
||||
namespace Telegrator.Core.Handlers
|
||||
namespace Telegrator.Core.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for update handlers, providing execution and lifetime management for Telegram updates.
|
||||
/// </summary>
|
||||
public abstract class UpdateHandlerBase(UpdateType handlingUpdateType) : IUpdateHandlerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for update handlers, providing execution and lifetime management for Telegram updates.
|
||||
/// </summary>
|
||||
public abstract class UpdateHandlerBase(UpdateType handlingUpdateType) : IUpdateHandlerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="UpdateType"/> that this handler processes.
|
||||
/// </summary>
|
||||
@@ -125,7 +125,7 @@ namespace Telegrator.Core.Handlers
|
||||
}
|
||||
}
|
||||
|
||||
internal IHandlerContainer GetContainer(DescribedHandlerDescriptor handlerInfo)
|
||||
private IHandlerContainer GetContainer(DescribedHandlerDescriptor handlerInfo)
|
||||
{
|
||||
if (this is IHandlerContainerFactory handlerDefainedContainerFactory)
|
||||
return handlerDefainedContainerFactory.CreateContainer(handlerInfo);
|
||||
@@ -178,5 +178,4 @@ namespace Telegrator.Core.Handlers
|
||||
if (Dispose(true))
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
using Telegrator.Core.Descriptors;
|
||||
|
||||
namespace Telegrator.Core
|
||||
namespace Telegrator.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Provider for managing awaiting handlers that can wait for specific update types.
|
||||
/// </summary>
|
||||
public interface IAwaitingProvider : IHandlersProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Provider for managing awaiting handlers that can wait for specific update types.
|
||||
/// </summary>
|
||||
public interface IAwaitingProvider : IHandlersProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Registers the usage of a handler and returns a disposable object to manage its lifetime.
|
||||
/// </summary>
|
||||
/// <param name="handlerDescriptor">The <see cref="HandlerDescriptor"/> to use.</param>
|
||||
/// <returns>An <see cref="IDisposable"/> that manages the handler's usage lifetime.</returns>
|
||||
public IDisposable UseHandler(HandlerDescriptor handlerDescriptor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
namespace Telegrator.Core
|
||||
namespace Telegrator.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for providers that collect and manage handler collections.
|
||||
/// Provides access to a collection of handlers for various processing operations.
|
||||
/// </summary>
|
||||
public interface ICollectingProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for providers that collect and manage handler collections.
|
||||
/// Provides access to a collection of handlers for various processing operations.
|
||||
/// </summary>
|
||||
public interface ICollectingProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the collection of handlers managed by this provider.
|
||||
/// </summary>
|
||||
public IHandlersCollection Handlers { get; }
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user