* Readme and docs updated
This commit is contained in:
@@ -91,13 +91,12 @@ bot.Handlers.AddHandler<StartCommandHandler>();
|
|||||||
using Telegrator.Handlers;
|
using Telegrator.Handlers;
|
||||||
using Telegrator.Annotations;
|
using Telegrator.Annotations;
|
||||||
|
|
||||||
[CommandHandler, CommandAlias("first"), NumericState(SpecialState.NoState)]
|
[CommandHandler, CommandAlias("first"), State<SetupWizard>(null)]
|
||||||
public class StateKeepFirst : CommandHandler
|
public class StateKeepFirst : CommandHandler
|
||||||
{
|
{
|
||||||
public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
|
public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
|
||||||
{
|
{
|
||||||
container.CreateNumericState();
|
StateStorage.GetStateMachine<SetupWizard>().BysenderId().Advance();
|
||||||
container.ForwardNumericState();
|
|
||||||
await Reply("first state moved (1)", cancellationToken: cancellation);
|
await Reply("first state moved (1)", cancellationToken: cancellation);
|
||||||
return Result.Ok();
|
return Result.Ok();
|
||||||
}
|
}
|
||||||
|
|||||||
+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.
|
**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
|
### 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).
|
- A Telegram Bot Token from [@BotFather](https://t.me/BotFather).
|
||||||
|
|
||||||
### .NET CLI
|
### .NET CLI
|
||||||
@@ -61,7 +61,7 @@ Install-Package Telegrator
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Hosting Integrations
|
### Hosting Integrations
|
||||||
- .NET Core >= 8.0
|
- .NET Core >= 10.0
|
||||||
- `Telegrator.Hosting`: For console/background services
|
- `Telegrator.Hosting`: For console/background services
|
||||||
- `Telegrator.Hosting.Web`: For ASP.NET Core/Webhook
|
- `Telegrator.Hosting.Web`: For ASP.NET Core/Webhook
|
||||||
|
|
||||||
@@ -325,7 +325,7 @@ builder.Handlers.AddMethod<CallbackQuery>(Option1Handler);
|
|||||||
```csharp
|
```csharp
|
||||||
public enum UserState
|
public enum UserState
|
||||||
{
|
{
|
||||||
Start = SpecialState.NoState,
|
Start,
|
||||||
WaitingForName,
|
WaitingForName,
|
||||||
WaitingForAge
|
WaitingForAge
|
||||||
}
|
}
|
||||||
@@ -333,39 +333,40 @@ public enum UserState
|
|||||||
// Start conversation
|
// Start conversation
|
||||||
[CommandHandler]
|
[CommandHandler]
|
||||||
[CommandAlias("register")]
|
[CommandAlias("register")]
|
||||||
[EnumState<UserState>(UserState.Start)]
|
[State<UserState>(UserState.Start)]
|
||||||
private static async Task<Result> StartRegistration(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
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);
|
await container.Reply("Please enter your name:", cancellationToken: cancellationToken);
|
||||||
return Ok;
|
return Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle name input
|
// Handle name input
|
||||||
[MessageHandler]
|
[MessageHandler]
|
||||||
[EnumState<UserState>(UserState.WaitingForName)]
|
[State<UserState>(UserState.WaitingForName)]
|
||||||
private static async Task<Result> HandleName(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
private static async Task<Result> HandleName(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var name = container.Input.Text;
|
var name = container.Input.Text;
|
||||||
container.ForwardEnumState<UserState>();
|
StateStorage.GetStateMachine<UserState>().BySenderId().Advance();
|
||||||
await container.Reply($"Hello {name}! Please enter your age:", cancellationToken: cancellationToken);
|
await container.Reply($"Hello {name}! Please enter your age:", cancellationToken: cancellationToken);
|
||||||
return Ok;
|
return Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle age input
|
// Handle age input
|
||||||
[MessageHandler]
|
[MessageHandler]
|
||||||
[EnumState<UserState>(UserState.WaitingForAge)]
|
[State<UserState>(UserState.WaitingForAge)]
|
||||||
private static async Task<Result> HandleAge(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
private static async Task<Result> HandleAge(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (int.TryParse(container.Input.Text, out int age))
|
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);
|
await container.Reply($"Registration complete! Name: {name}, Age: {age}", cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await container.Reply("Please enter a valid age (number):", cancellationToken: cancellationToken);
|
await container.Reply("Please enter a valid age (number):", cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok;
|
return Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -497,50 +498,43 @@ public class RestrictedHandler : MessageHandler
|
|||||||
|
|
||||||
### 3.3. State Management
|
### 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]
|
> [!NOTE]
|
||||||
> Each type of `StateKeeper`'s (EnumStateKeeper, NumericStateKeeper) is shared between **EVERY** handler in project.
|
> Each type of `StateKeeper`'s keys and states are shared between **EVERY** handler in project.
|
||||||
|
|
||||||
**Types of State:**
|
|
||||||
- **NumericState**: Integer-based steps
|
|
||||||
- **StringState**: Named steps
|
|
||||||
- **EnumState**: Enum-based scenarios
|
|
||||||
|
|
||||||
**How to Use:**
|
**How to Use:**
|
||||||
1. Define your state (enum/int/string)
|
1. Define your state (enum/int/string)
|
||||||
2. Use a state filter attribute on your handler:
|
2. Use a state filter attribute on your handler:
|
||||||
- `[EnumState<MyEnum>(MyEnum.Step1)]`
|
- `[State<MyEnum>(MyEnum.Step1)]`
|
||||||
- `[NumericState(1)]`
|
|
||||||
- `[StringState("waiting_input")]`
|
|
||||||
3. Change state inside the handler using extension methods:
|
3. Change state inside the handler using extension methods:
|
||||||
- `container.ForwardEnumState<MyEnum>()`
|
- `StateStorage.GetStateMachine<MyEnum>().BySenderId().Current()`
|
||||||
- `container.ForwardNumericState()`
|
- `StateStorage.GetStateMachine<MyEnum>().BySenderId().Advance()`
|
||||||
- `container.ForwardStringState()`
|
- `StateStorage.GetStateMachine<MyEnum>().BySenderId().Retreat()`
|
||||||
- `container.DeleteEnumState<MyEnum>()`
|
- `StateStorage.GetStateMachine<MyEnum>().BySenderId().Reset()`
|
||||||
|
|
||||||
**Example:**
|
**Example:**
|
||||||
```csharp
|
```csharp
|
||||||
public enum QuizState
|
public enum QuizState
|
||||||
{
|
{
|
||||||
Start = SpecialState.NoState, Q1, Q2
|
Start, Q1, Q2
|
||||||
}
|
}
|
||||||
|
|
||||||
[CommandHandler]
|
[CommandHandler]
|
||||||
[CommandAlias("quiz")]
|
[CommandAlias("quiz")]
|
||||||
[EnumState<QuizState>(QuizState.Start)]
|
[State<QuizState>(QuizState.Start)]
|
||||||
public class StartQuizHandler : CommandHandler
|
public class StartQuizHandler : CommandHandler
|
||||||
{
|
{
|
||||||
public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
|
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?");
|
await Reply("Quiz started! Question 1: What is the capital of France?");
|
||||||
return Ok;
|
return Ok;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[MessageHandler]
|
[MessageHandler]
|
||||||
[EnumState<QuizState>(QuizState.Q1)]
|
[State<QuizState>(QuizState.Q1)]
|
||||||
public class Q1Handler : MessageHandler
|
public class Q1Handler : MessageHandler
|
||||||
{
|
{
|
||||||
public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
|
public override async Task<Result> Execute(IHandlerContainer<Message> container, CancellationToken cancellation)
|
||||||
@@ -550,7 +544,7 @@ public class Q1Handler : MessageHandler
|
|||||||
else
|
else
|
||||||
await Reply("Incorrect. The answer is Paris.");
|
await Reply("Incorrect. The answer is Paris.");
|
||||||
|
|
||||||
container.ForwardEnumState<QuizState>();
|
StateStorage.GetStateMachine<QuizState>().BySenderId().Advance();
|
||||||
await Reply("Question 2: What is 2 + 2?");
|
await Reply("Question 2: What is 2 + 2?");
|
||||||
return Ok;
|
return Ok;
|
||||||
}
|
}
|
||||||
@@ -559,8 +553,8 @@ public class Q1Handler : MessageHandler
|
|||||||
|
|
||||||
> **How is it working?**
|
> **How is it working?**
|
||||||
> 1. **Enum State Definition**: `QuizState` enum defines the conversation flow with `Start = SpecialState.NoState` indicating no initial state.
|
> 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.
|
> 2. **State Filter**: `[State<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).
|
> 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`.
|
> 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.
|
> 5. **State Management**: Each handler manages its own state transition, creating a clear conversation flow.
|
||||||
|
|
||||||
@@ -1425,28 +1419,28 @@ public enum UserState
|
|||||||
// Start conversation
|
// Start conversation
|
||||||
[CommandHandler]
|
[CommandHandler]
|
||||||
[CommandAlias("register")]
|
[CommandAlias("register")]
|
||||||
[EnumState<UserState>(UserState.Start)]
|
[State<UserState>(UserState.Start)]
|
||||||
private static async Task<Result> StartRegistration(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
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);
|
await container.Reply("Please enter your name:", cancellationToken: cancellationToken);
|
||||||
return Ok;
|
return Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle name input
|
// Handle name input
|
||||||
[MessageHandler]
|
[MessageHandler]
|
||||||
[EnumState<UserState>(UserState.WaitingForName)]
|
[State<UserState>(UserState.WaitingForName)]
|
||||||
private static async Task<Result> HandleName(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
private static async Task<Result> HandleName(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var name = container.Input.Text;
|
var name = container.Input.Text;
|
||||||
container.ForwardEnumState<UserState>();
|
StateStorage.GetStateMachine<UserState>().BySenderId().Advance();
|
||||||
await container.Reply($"Hello {name}! Please enter your age:", cancellationToken: cancellationToken);
|
await container.Reply($"Hello {name}! Please enter your age:", cancellationToken: cancellationToken);
|
||||||
return Ok;
|
return Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle age input
|
// Handle age input
|
||||||
[MessageHandler]
|
[MessageHandler]
|
||||||
[EnumState<UserState>(UserState.WaitingForAge)]
|
[State<UserState>(UserState.WaitingForAge)]
|
||||||
private static async Task<Result> HandleAge(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
private static async Task<Result> HandleAge(IHandlerContainer<Message> container, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (int.TryParse(container.Input.Text, out int age))
|
if (int.TryParse(container.Input.Text, out int age))
|
||||||
|
|||||||
Reference in New Issue
Block a user