Skip to content

yawaflua/Telegram.Net

Repository files navigation

Telegram Bot Attribute Handlers

This project provides a set of C# attributes to facilitate the handling of different types of Telegram bot updates using reflection.

Features

  • Inline Query Handling (InlineAttribute)
  • Edited Message Handling (EditMessageAttribute)
  • Command Handling (CommandHandlerAttribute)
  • Callback Query Handling (CallbackAttribute)
  • PreCheckout Query Handling (PreCheckoutAttribute)
  • General Update Handling (UpdateAttribute)
  • Auto-generate telegram client

Installation

Ensure you have the required dependencies installed:

 dotnet add package yawaflua.Telegram.Net

Usage

Provide dependencies in class

You can provide dependencies in class from constructor, and after it use it like static.

public class Example : IUpdatePollingService
{
    private static MyCoolService _service; // It should to be static!
    
    public Example(MyCoolService service)
    {
        _service = service;
    }
}

Inline Query Handling

Use the InlineAttribute to register a method as an inline query handler.

[Inline("example_query")]
public static async Task HandleInlineQuery(ITelegramBotClient bot, InlineQuery query, CancellationToken cancellationToken)
{
    // Handle inline query
}

Edited Message Handling

Use the EditMessageAttribute to register a method as a handler for edited messages.

[EditMessage]
public static async Task HandleEditedMessage(ITelegramBotClient bot, Message message, CancellationToken cancellationToken)
{
    // Handle edited message
}

Command Handling

Use the CommandHandlerAttribute to register a method as a command handler. You can provide only begin of command text. Like, /start act-

[CommandHandler("/start")]
public static async Task StartCommand(ITelegramBotClient bot, Message message, CancellationToken cancellationToken)
{
    // Handle start command
}

Callback Query Handling

Use the CallbackAttribute to register a method as a callback query handler. You can provide only begin of callback data text

[Callback("button_click")]
public static async Task HandleCallbackQuery(ITelegramBotClient bot, CallbackQuery query, CancellationToken cancellationToken)
{
    // Handle callback query
}

PreCheckout Query Handling

Use the PreCheckoutAttribute to register a method as a pre-checkout query handler.

[PreCheckout]
public static async Task HandlePreCheckoutQuery(ITelegramBotClient bot, PreCheckoutQuery query, CancellationToken cancellationToken)
{
    // Handle pre-checkout query
}

General Update Handling

Use the UpdateAttribute to register a method as a generic update handler.

[Update]
public static async Task HandleUpdate(ITelegramBotClient bot, Update update, CancellationToken cancellationToken)
{
    // Handle general update
}

We know about this bugs:

  • [ X ] Not working providing dependencies in class constructor and this gives an error. Try to not use provided dependencies in class. We are should to fix it in v1.0.2. Now just use static variables

License

This project is open-source and available under the Apache 2.0 License.