2025-07-25 01:49:43 +04:00

2025-07-24 23:19:59 +04:00
---
2025-07-24 23:23:07 +04:00
## 🚀 About Telegrator
2025-07-24 23:19:59 +04:00
2026-03-08 19:43:48 +04:00
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.
2025-07-24 23:19:59 +04:00
---
2026-04-29 23:38:38 +04:00
## Learn and Docs
Learn Telegrator on [Official documentation site ](https://rikitav.github.io/telegrator.docs ).
2026-03-15 17:56:05 +04:00
The documentation may not be completely transparent, informative or even actual to latest version, so if you have any questions or problems, please write them in the [Telegram.Bot ](https://t.me/joinchat/B35YY0QbLfd034CFnvCtCA ) group. I am a member of this group and will notice you! If you have any suggestions or want to participate in building documentation, make push requests and open issues on this repository!
---
2025-07-24 23:19:59 +04:00
## ✨ Key Features
- **Aspect-oriented approach**: Handlers and filters are "aspects" of the bot, easily composable and extendable.
- **Decentralized logic**: Each handler is an independent module—no more giant switch/case blocks.
- **Mediator-based dispatching**: All updates are routed through a powerful mediator-dispatcher.
- **Flexible filtering**: Filters for commands, text, sender, chat, regex, and much more.
- **Execution order and priorities**: Easily control handler priorities and execution order.
- **Thread safety and concurrency control**: Limit the number of concurrent handlers, await other updates inside a handler.
- **Extensibility via attributes and providers**: Easily add your own filters, handlers, and state keepers.
- **Minimal boilerplate—maximum declarativity!**
---
## 🧩 Architecture & Approach
- **Decentralization**: Bot logic is split into independent handlers (aspects), each responsible for its own part.
- **Mediator**: All Telegram updates go through a mediator, which decides which handlers should process them and in what order.
- **Filters**: Describe handler trigger conditions in a flexible, declarative way.
- **State**: Built-in mechanisms for user/chat state without manual state machines.
---
## 🛠️ Quick Start
### 1. Installation
``` shell
2025-07-25 01:26:32 +04:00
# .NET CLI
dotnet add package Telegrator
# NuGet CLI
NuGet\I nstall-Package Telegrator
2025-07-24 23:19:59 +04:00
```
### 2. Minimal Bot Example
``` csharp
2025-07-24 23:23:07 +04:00
using Telegrator.Handlers ;
using Telegrator.Annotations ;
2025-07-24 23:19:59 +04:00
[MessageHandler]
public class HelloHandler : MessageHandler
{
2026-03-07 20:46:04 +04:00
public override async Task < Result > Execute ( IHandlerContainer < Message > container , CancellationToken cancellation )
2025-07-24 23:19:59 +04:00
{
await Reply ( "Hello, world!" , cancellationToken : cancellation ) ;
2025-08-02 03:36:48 +04:00
return Result . Ok ( ) ;
2025-07-24 23:19:59 +04:00
}
}
// Registration and launch:
2025-07-25 01:26:32 +04:00
var bot = new TelegratorClient ( "<YOUR_BOT_TOKEN>" ) ;
2025-07-24 23:19:59 +04:00
bot . Handlers . AddHandler < HelloHandler > ( ) ;
bot . StartReceiving ( ) ;
```
### 3. Adding Filtering and Commands
``` csharp
using Telegram.Bot.Types.Enums ;
2025-07-24 23:23:07 +04:00
using Telegrator.Handlers ;
using Telegrator.Annotations ;
2025-07-24 23:19:59 +04:00
2026-03-07 20:46:04 +04:00
[CommandHandler, CommandAlias("start", "hello"), ChatType(ChatType.Private)]
2025-07-24 23:19:59 +04:00
public class StartCommandHandler : CommandHandler
{
2026-03-07 20:46:04 +04:00
public override async Task < Result > Execute ( IHandlerContainer < Message > container , CancellationToken cancellation )
2025-07-24 23:19:59 +04:00
{
await Responce ( "Welcome!" , cancellationToken : cancellation ) ;
2025-08-02 03:36:48 +04:00
return Result . Ok ( ) ;
2025-07-24 23:19:59 +04:00
}
}
// Registration:
bot . Handlers . AddHandler < StartCommandHandler > ( ) ;
```
### 4. State Management Example
``` csharp
2025-07-24 23:23:07 +04:00
using Telegrator.Handlers ;
using Telegrator.Annotations ;
2025-07-24 23:19:59 +04:00
2026-03-09 03:31:45 +04:00
[CommandHandler, CommandAlias("first"), State<SetupWizard>(null)]
2025-07-24 23:19:59 +04:00
public class StateKeepFirst : CommandHandler
{
2026-03-07 20:46:04 +04:00
public override async Task < Result > Execute ( IHandlerContainer < Message > container , CancellationToken cancellation )
2025-07-24 23:19:59 +04:00
{
2026-03-09 03:31:45 +04:00
StateStorage . GetStateMachine < SetupWizard > ( ) . BysenderId ( ) . Advance ( ) ;
2025-07-24 23:19:59 +04:00
await Reply ( "first state moved (1)" , cancellationToken : cancellation ) ;
2025-08-02 03:36:48 +04:00
return Result . Ok ( ) ;
2025-07-24 23:19:59 +04:00
}
}
// Registration:
bot . Handlers . AddHandler < StateKeepFirst > ( ) ;
```
---
2025-07-24 23:23:07 +04:00
## 🏆 Why Telegrator over state machines?
2025-07-24 23:19:59 +04:00
- **No tangled switch/case**—logic is split into independent handlers.
- **Flexible dispatching**—the mediator decides who and when processes an event.
- **Simple state management**—no need to implement state machines manually.
- **Easy scaling**—add new handlers without rewriting old ones.
- **High code readability and maintainability.**
---
## 📚 Documentation & Examples
2026-05-01 21:20:19 +04:00
- [Documentation ](https://rikitav.github.io/telegrator.docs )
2026-03-07 20:46:04 +04:00
- [Usage examples (WIP) ](https://github.com/Rikitav/Telegrator/tree/master/examples )
2025-07-24 23:19:59 +04:00
---
## 🤝 Contribution & Feedback
We welcome your questions, suggestions, and pull requests! Open issues or contact us directly.
---
## ⚡ License
GPLv3