Telegram.Bot.Extensions.Roles 1.0.0

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

// Install Telegram.Bot.Extensions.Roles as a Cake Tool
#tool nuget:?package=Telegram.Bot.Extensions.Roles&version=1.0.0

Telegram bot extensions

This project aims to make interactions with Telegram.Bot easier.

Getting started

Install Telegram.Bot.Extensions.Hosting

Add telegram bot to services.

builder.Services.AddTelegramBot(new TelegramBotClientOptions(token), x =>
{
    // handle exact text or command
    x.AddHandler<StartHandler>(x => x.HandleText("/start")); // handler ITelegramHandler
    // handle if specified condition is satisfied
    x.AddHandler<SecretHandler>(x => x.HandleIf((update, cancellationToken) => ValueTask.FromResult(true)));
    

    x.AddMiddleware<EchoMiddleware>(); // also you can add middleware ITelegramMiddleware
});
builder.Services.AddTelegramBotMiddleware<EchoMiddleware>() // another way to add middleware

Remember to use the handler.

app.UseDefaultTelegramHandler();

Now your application handles telegram events.

Middleware is executed in the order it was added.

User context and authentication

Install Telegram.Bot.Extensions.Roles

Add storage and role services.

builder.Services.AddTelegramBotStorage<TestBotDbContext>();
builder.Services.AddTelegramBotRoles();

// context should inherit TelegramDbContext
public class TestBotDbContext : TelegramDbContext
{
    // your models here
}

Specify required roles with AllowActiveRoleAttribute

public class AdminHandler : ITelegramHandler
{
    [AllowActiveRole("admin")]
    public async ValueTask Handle(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
    {
        // your code here
    }
}

This package also adds IChatAccessor and IUserAccessor services to your handlers.

There are 2 types of users: global and chat local. Global user is the same for any chat. Chat local user is different for each chat. User only appears in the database once the bot receives the event from them in any chat. For each user, there is only one global user and there may be multiple chat local users.

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.0 86 5/18/2024