TgBotCommandHandler 0.1.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package TgBotCommandHandler --version 0.1.0
NuGet\Install-Package TgBotCommandHandler -Version 0.1.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="TgBotCommandHandler" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TgBotCommandHandler --version 0.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TgBotCommandHandler, 0.1.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 TgBotCommandHandler as a Cake Addin #addin nuget:?package=TgBotCommandHandler&version=0.1.0 // Install TgBotCommandHandler as a Cake Tool #tool nuget:?package=TgBotCommandHandler&version=0.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
TgBotCommandHandler
is a library that provides easy-to-use command handlers for Telegram bots commands from User
If you like this project please give a star and a cup of coffee =)
Installation
To install TgBotCommandHandler, you can use the NuGet package manager in Visual Studio. Simply search for "TgBotCommandHandler" and click "Install".
Alternatively, you can install TgBotCommandHandler using the command line:
Install-Package TgBotCommandHandler
Getting Started
- Register your command handler classes using
botClient.RegisterCommand<MyCommandHandler>
whereMyCommandHandler
is a subclass ofCommandHandler
andbotClient
is an instance ofITelegramBotClient
- initialize the
botClient.HandleCommands(update)
whereupdate
is an instance ofTelegram.Bot.Types.Update
Example
var botClient = new TelegramBotClient("YOUR_BOT_TOKEN");
botClient.RegisterCommand<MyCommandHandler>();
botClient.InitializeCommands(commandHandler);
botClient..StartReceiving(updateHandler: HandleUpdateAsync,
pollingErrorHandler: HandlePollingErrorAsync,
receiverOptions: receiverOptions,
cancellationToken: cancellationToken);
⚠️ NOTE: do NOT put your Token directly to your source code.
async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
// Only process Message updates: https://core.telegram.org/bots/api#message
if (update.Message is not { } message)
return;
// Only process text messages
if (message.Text is not { } messageText)
return;
await botClient.HandleCommands(update);
}
Command Handlers
public class MyCommandHandler : CommandHandler
{
// All command methods MUST be async, have a return type of Task, have only a Command as a parameter,
// and have the [Command] attribute.
// The parameter for the [Command] attribute indicates what invokes this method. DO NOT specify a prefix here.
// When a user invokes the /hello command, the bot will respond with "Hello".
[Command("hello")]
public async Task HandleHelloCommand(CommandContext context)
{
Console.WriteLine("hello command request.");
await context.RespondAsync("Hello");
}
// When a user invokes the /world command, the bot will respond with "World".
[Command("world")]
public async Task HandleWorldCommand(CommandContext context)
{
Console.WriteLine("world command request.");
await context.RespondAsync("World");
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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.
-
net6.0
- Telegram.Bot (>= 18.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.