C# client library for building Telegram bots (https://core.telegram.org/bots/api).
Contains strongly-typed request and response classes, and transport class for sending requests and receiving results. Uses System.Text.Json
only.
var bot = new TelegramBot(accessToken, new HttpClient());
var me = await bot.GetMe();
if (me != null)
{
Console.WriteLine("Me: {0} (@{1})", me.FirstName, me.Username);
}
See TelegramBotDemo
project for more samples.
Use with AddHttpClient<TelegramBot>(...)
in web projects, use Polly
(or any other you like) to handle transient faults and improve the resilience.
Just describe new request (or data) class:
public class PinChatMessage() : RequestBase<bool>("pinChatMessage")
{
public string? BusinessConnectionId { get; set; }
public required IntegerOrString ChatId { get; set; }
public required long MessageId { get; set; }
public bool? DisableNotification { get; set; }
}
This works out-of-the-box:
- snake_case property naming (
BusinessConnectionId
->business_connection_id
); DateTimeOffset
[de]serialization of unix-time fields;- use
IntegerOrString
type for number-or-string fields (likechat_id
); - use
InputFile
orInputFileOrString
types for requests with files (don't forget to passwithFiles: true
toRequestBase
, checkSetWebhook
for example);
Use NuGet package NetTelegramBotApi.
Only System.Text.Json
.