Telegram.Bot.Maker
1.0.2
See the version list below for details.
dotnet add package Telegram.Bot.Maker --version 1.0.2
NuGet\Install-Package Telegram.Bot.Maker -Version 1.0.2
<PackageReference Include="Telegram.Bot.Maker" Version="1.0.2" />
paket add Telegram.Bot.Maker --version 1.0.2
#r "nuget: Telegram.Bot.Maker, 1.0.2"
// Install Telegram.Bot.Maker as a Cake Addin #addin nuget:?package=Telegram.Bot.Maker&version=1.0.2 // Install Telegram.Bot.Maker as a Cake Tool #tool nuget:?package=Telegram.Bot.Maker&version=1.0.2
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
// initialize telegram client
ITelegramClient telegramClient = new TelegramClient(configuration);
// define your handlers (note that order is important)
List<IMessageHandler<Message>> handlers =
[
new TextHandler()
];
HandlersConfiguration.Configure(handlers);
await telegramClient.Start();
// TextHandler.cs.cs
/// <summary>
/// Handles all text messages
/// </summary>
public class TextHandler : IMessageHandler<Message>
{
public bool CanHandle(Message message)
{
return true;
}
public async Task HandleAsync(Message message, ITelegramBotClient botClient, CancellationToken cancellationToken)
{
await botClient.SendTextMessageAsync(message.ChatId, $"You said: {message.Data}",
cancellationToken: cancellationToken);
}
public HandlerType Type => HandlerType.MessageHandler;
public bool Successful { get; }
}
Chat commands
You can add more commands by adding new handlers like this one:
// StartHandler.cs
/// <summary>
/// Handles /start message
/// </summary>
public class StartHandler : IMessageHandler<Message>
{
/// <inheritdoc/>
public bool CanHandle(Message message)
{
return message.Data is "/start";
}
public async Task HandleAsync(Message message, ITelegramBotClient botClient, CancellationToken cancellationToken)
{
await botClient.SendTextMessageAsync(message.ChatId, "Hello, I'm working!", cancellationToken: cancellationToken);
}
public HandlerType Type => HandlerType.MessageHandler;
public bool Successful { get; }
}
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 : IMessageHandler<Message>
{
public bool CanHandle(Message message)
{
return message.Data is "some_request";
}
public 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);
}
public HandlerType Type => HandlerType.MessageHandler;
public bool Successful { get; }
}
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 : IMessageHandler<Message>
{
public bool CanHandle(Message message)
{
return message.Data is "/menu";
}
public async Task HandleAsync(Message message, ITelegramBotClient botClient, CancellationToken cancellationToken)
{
await botClient.SendTextMessageAsync(message.ChatId, "Main menu", replyMarkup: MenuProvider.MainMenu,
cancellationToken: cancellationToken);
}
public HandlerType Type => HandlerType.MessageHandler;
public bool Successful { get; }
}
// MenuProvider.cs
public static class MenuProvider
{
public static InlineKeyboardMarkup MainMenu { get; } = new InlineMenuBuilder()
.WithButton("What's up?", "some_request")
.Build();
}
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. |
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 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.
Initial