Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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.cs

var 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.cs

public 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.cs

public 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.cs

public 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);
}
}
```