Telegram.Bot.Maker 1.0.9

dotnet add package Telegram.Bot.Maker --version 1.0.9                
NuGet\Install-Package Telegram.Bot.Maker -Version 1.0.9                
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="Telegram.Bot.Maker" Version="1.0.9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Telegram.Bot.Maker --version 1.0.9                
#r "nuget: Telegram.Bot.Maker, 1.0.9"                
#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.
// Install Telegram.Bot.Maker as a Cake Addin
#addin nuget:?package=Telegram.Bot.Maker&version=1.0.9

// Install Telegram.Bot.Maker as a Cake Tool
#tool nuget:?package=Telegram.Bot.Maker&version=1.0.9                

Telegram Bot API Framework

This is a simple and lightweight framework that helps you create Telegram bots faster. It uses the Telegram Bot API

Usage

Example projects can be found at:

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:

// Program.cs

var settings = new TelegramSettings
{
    Token = "your_api_token"
};

var handlers = new PipelineBuilder()
    .WithMiddleware<SubscriptionMiddleware>()
    .WithMessageHandler<StartHandler>()
    .WithMessageHandler<TextHandler>()
    .Build();

// initialize telegram client
ITelegramClient telegramClient = new TelegramClient(settings)
    .ConfigureBasePipelines(handlers);

await telegramClient.Start();
// TextHandler.cs.cs

/// <summary>
/// Handles all text messages
/// </summary>
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:

// StartHandler.cs

/// <summary>
/// Handles /start message
/// </summary>
public class StartHandler : MessageHandler
{
    /// <inheritdoc/>
    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:

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

//MenuHandler.cs

public class MenuHandler : MessageHandler
{
    /// <inheritdoc/>
    public override bool CanHandle(Message message)
    {
        return message.Data is "/menu";
    }

    /// <inheritdoc/>
    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

//SubscriptionMiddleware.cs

public class SubscriptionMiddleware(IServiceProvider serviceProvider) : Middleware
{
    /// <inheritdoc />
    public override bool CanHandle(Message message)
    {
        return message.Data is not "/start";
    }

    /// <inheritdoc/>
    public override async Task HandleAsync(Message message, ITelegramBotClient botClient, CancellationToken cancellationToken)
    {
        var subscriptionService = serviceProvider.GetRequiredService<ISubscriptionService>();
        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);
    }
}
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. 
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
1.0.9 89 7/13/2024
1.0.8 84 7/13/2024
1.0.7 82 7/5/2024
1.0.6 103 6/24/2024
1.0.5 101 6/24/2024
1.0.2 103 6/20/2024

Reworked pipelines and configuration, also reworked handlers abstractions