Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tipalol/telegram.bot.framework
This is a simple and lightweight framework that helps you create Telegram bots faster.
https://github.com/tipalol/telegram.bot.framework
csharp dotnet dotnet8 nuget nuget-package telegram telegram-bot telegram-bot-api telegrambot
Last synced: 3 months ago
JSON representation
This is a simple and lightweight framework that helps you create Telegram bots faster.
- Host: GitHub
- URL: https://github.com/tipalol/telegram.bot.framework
- Owner: tipalol
- Created: 2024-06-20T13:10:39.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-07-13T16:47:53.000Z (6 months ago)
- Last Synced: 2024-10-14T03:02:45.910Z (3 months ago)
- Topics: csharp, dotnet, dotnet8, nuget, nuget-package, telegram, telegram-bot, telegram-bot-api, telegrambot
- Language: C#
- Homepage:
- Size: 81.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Telegram Bot API Framework
This is a simple and lightweight framework that helps you create Telegram bots faster. It uses the [Telegram Bot API](https://core.telegram.org/bots)
## Usage
Example projects can be found at:
- [Console Application example](https://github.com/tipalol/Telegram.Bot.Framework/tree/main/Telegram.Bot.Framework.TestClient)
- [Web Application example](https://github.com/tipalol/Telegram.Bot.Framework/tree/main/Telegram.Bot.Framework.WebClient)Here's an example of how to use the framework to create a simple bot that replies the same text when a user sends a text message:
```c#
// Program.csvar settings = new TelegramSettings
{
Token = "your_api_token"
};var handlers = new PipelineBuilder()
.WithMiddleware()
.WithMessageHandler()
.WithMessageHandler()
.Build();// initialize telegram client
ITelegramClient telegramClient = new TelegramClient(settings)
.ConfigureBasePipelines(handlers);await telegramClient.Start();
``````c#
// TextHandler.cs.cs///
/// Handles all text messages
///
public class TextHandler : MessageHandler
{
public override bool CanHandle(Message message)
{
return true;
}public override async Task HandleAsync(Message message, ITelegramBotClient botClient, CancellationToken cancellationToken)
{
await botClient.SendTextMessageAsync(message.ChatId, $"You said: {message.Data}",
cancellationToken: cancellationToken);
}
}
```### Chat commands
You can add more commands by adding new handlers like this one:
```c#
// StartHandler.cs///
/// Handles /start message
///
public class StartHandler : MessageHandler
{
///
public override bool CanHandle(Message message)
{
return message.Data is "/start";
}public override async Task HandleAsync(Message message, ITelegramBotClient botClient, CancellationToken cancellationToken)
{
await botClient.SendTextMessageAsync(message.ChatId, "Hello, I'm working!", cancellationToken: cancellationToken);
}
}
```### Callback data
Callback data is used to pass information between different functions in your bot. For example, you could have a `some_request` callback called and then handle it in another function like this:
```c#
// SomeCallbackHandler.cspublic class SomeCallbackHandler : MessageHandler
{
public override bool CanHandle(Message message)
{
return message.Data is "some_request";
}public override async Task HandleAsync(Message message, ITelegramBotClient botClient, CancellationToken cancellationToken)
{
await botClient.SendTextMessageAsync(message.ChatId, "Alright, you?", cancellationToken: cancellationToken);
// we must say OK to Telegram API, so it knows callback is handled
await botClient.CallbackOk(message, cancellationToken);
}
}
```### Reply markups
Reply markups are used to provide additional options or buttons for users to interact with your bot. You can create as many reply markups as you need, and they will be displayed below the message you send.
```c#
//MenuHandler.cspublic class MenuHandler : MessageHandler
{
///
public override bool CanHandle(Message message)
{
return message.Data is "/menu";
}///
public override async Task HandleAsync(Message message, ITelegramBotClient botClient, CancellationToken cancellationToken)
{
await botClient.SendTextMessageAsync(message.ChatId, "Main menu", replyMarkup: MenuProvider.MainMenu,
cancellationToken: cancellationToken);
}
}// MenuProvider.cs
public static class MenuProvider
{
public static InlineKeyboardMarkup MainMenu { get; } = new InlineMenuBuilder()
.WithButton("What's up?", "some_request")
.Build();
}
```### Middlewares
```c#
//SubscriptionMiddleware.cspublic class SubscriptionMiddleware(IServiceProvider serviceProvider) : Middleware
{
///
public override bool CanHandle(Message message)
{
return message.Data is not "/start";
}///
public override async Task HandleAsync(Message message, ITelegramBotClient botClient, CancellationToken cancellationToken)
{
var subscriptionService = serviceProvider.GetRequiredService();
var subscribed = subscriptionService.CheckSubscription(message.ChatId);if (subscribed)
{
Successful = true;
return;
}await botClient.SendTextMessageAsync(message.ChatId,
"We can't find your subscription. Access is denied", cancellationToken: cancellationToken);
}
}
```