TgBot.Infrastructure
0.0.4
dotnet add package TgBot.Infrastructure --version 0.0.4
NuGet\Install-Package TgBot.Infrastructure -Version 0.0.4
<PackageReference Include="TgBot.Infrastructure" Version="0.0.4" />
<PackageVersion Include="TgBot.Infrastructure" Version="0.0.4" />
<PackageReference Include="TgBot.Infrastructure" />
paket add TgBot.Infrastructure --version 0.0.4
#r "nuget: TgBot.Infrastructure, 0.0.4"
#:package TgBot.Infrastructure@0.0.4
#addin nuget:?package=TgBot.Infrastructure&version=0.0.4
#tool nuget:?package=TgBot.Infrastructure&version=0.0.4
TgBot Infrastructure framework
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 | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Telegram.Bot (>= 19.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.