TgBot.Infrastructure 0.0.4

dotnet add package TgBot.Infrastructure --version 0.0.4
                    
NuGet\Install-Package TgBot.Infrastructure -Version 0.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TgBot.Infrastructure" Version="0.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TgBot.Infrastructure" Version="0.0.4" />
                    
Directory.Packages.props
<PackageReference Include="TgBot.Infrastructure" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TgBot.Infrastructure --version 0.0.4
                    
#r "nuget: TgBot.Infrastructure, 0.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TgBot.Infrastructure@0.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TgBot.Infrastructure&version=0.0.4
                    
Install as a Cake Addin
#tool nuget:?package=TgBot.Infrastructure&version=0.0.4
                    
Install as a Cake Tool

TgBot Infrastructure framework

TgBot.Infrastructure

About

TgBot.Infrastructure is a .NET Core (net8.0) framework that help easier launch a telegram bots.
Features:

  • Command handlers with auto registration;
  • Callback handlers from inline buttons with auto registration;
  • Shared context of a handler with a simple management system;
  • DI that support registration of application specific services;
  • Environments;
  • Application settings that stored in json by environments;
  • Background worker that allow run tasks by time interval.

Quick start

Create Settings class/record that implements ISettings interface:

internal record MySettings : ISettings
{
    public string BotToken { get; init; } = "";  // Token from @BotFather
    public string CallbackDataPrefixDelimiter { get; init; } = ":";  // Using for callback data handlers: [handler name][CallbackDataPrefixDelimiter from settings][data]
    public bool FailUpdateOnContextUpdateFailed { get; init; } = true;  // Stop bot if handler fail update context
}

Create Application class that extend TgBotApplication abstract class and realize everything necessary. Also, mark your class with ApplicationSettings attribute with type of created settings implementation:

[ApplicationSettings(typeof(MySettings))]
internal class MyTgBotApplication : TgBotApplication
{
    protected override void RegisterServices(IServiceCollection serviceCollection) 
    {
        // Register your services
    }

    protected override async Task OnHandleResultAsync(
        ITelegramBotClient botClient,
        Update update,
        HandleResult handleResult,
        CancellationToken cancellationToken)
    {
        // Handler return result
        
        if (handleResult.IsSuccess || update.Message?.Chat.Id is null)
        {
            return;
        }

        // Handle result is error
    }

    protected override async Task OnUnexpectedExceptionAsync(
        ITelegramBotClient botClient,
        Update update,
        Exception exception,
        CancellationToken cancellationToken)
    {
        // Exception was thrown
    }

    protected override async Task OnPollingErrorAsync(
        ITelegramBotClient botClient,
        Exception exception,
        CancellationToken cancellationToken)
    {
        // Telegram API can't refresh updates 
    }
}

Add your command handlers that user can execute by sending your bot messages like /command.
For example:

internal class MyHandler : IHandler
{
    public string Command => "my_command";  // name of command from message: /my_command

    public async Task<HandleResult> HandleSetHandlerToActiveAsync(
        ITelegramBotClient botClient,
        BotUpdate update,
        object? context,
        CancellationToken cancellationToken)
    {
        // Executes when it's first time user has sent command of this handler
    }

    public async Task<HandleResult> HandleNextMessageAsync(
        ITelegramBotClient botClient,
        BotUpdate update,
        object? context,
        CancellationToken cancellationToken)
    {
        // Executes on every update from user if handler is active 
        // (must be set in HandleResult by StayHandlerAsActive prop)
    }

    public Task<HandleResult> ExecuteOnHandlerLeaveAsync(
        ITelegramBotClient botClient,
        object? context,
        CancellationToken cancellationToken)
    {
        // Executes when user has sent an other command 
    }
}

What's next?

You can check the runnable Example application of telegram bot. Just add your bot token to settings

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.4 58 8/31/2025
0.0.3 57 8/31/2025
0.0.2 245 3/6/2025
0.0.1 142 10/7/2024