Telegram bots are powerful tools for automation, interaction, and integration. While Python is a popular choice for bot development, C# offers a robust, type-safe, and performant alternative—especially for developers already working in the .NET ecosystem. This guide walks through building a simple Telegram bot using C# and the official Telegram.Bot library.
What is a Telegram Bot?
A Telegram bot is an automated account that can interact with users, send messages, receive commands, and integrate with various services. They run on servers and perform tasks without human intervention—smart assistants inside your Telegram chats.
Why build with C#?
- Performance: C# and .NET are well suited for high-throughput bot applications.
- Type safety: Strong typing catches many errors at compile time rather than runtime.
- Ecosystem: Use the full .NET library and tooling stack.
- Scalability: .NET applications scale cleanly as your bot grows in usage.
- Familiarity: If you already write C#, you can ship faster in your preferred language.
Prerequisites
Before you start, make sure you have:
- .NET SDK — .NET 8.0 or later recommended
- An IDE — Visual Studio, VS Code with the C# extension, or JetBrains Rider
- A Telegram account — to create and test your bot
Step 1: Create your bot with BotFather
Register your bot with Telegram's official @BotFather bot to get a unique API token.
- Open Telegram — search for
@BotFatherand start a chat. Use the official account with the blue checkmark. - Start a new bot — send
/newbot. - Choose a display name — e.g. "My C# Helper".
- Choose a username — must be unique and end with
bot, e.g.MyCSharpHelperBot. - Save your token — BotFather returns a long API token string. Keep it secret and never commit it to source control. In real projects, load it from environment variables or .NET user secrets.
Step 2: Set up the C# project
Create a console application and install Telegram.Bot:
dotnet new console -n MyTelegramBotCSharp
cd MyTelegramBotCSharp
dotnet add package Telegram.Bot
Step 3: Write your first bot code
Open Program.cs and replace its contents with:
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
class Program
{
private static string BotToken = "YOUR_BOT_TOKEN_HERE";
static async Task Main()
{
var botClient = new TelegramBotClient(BotToken);
using var cts = new CancellationTokenSource();
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = Array.Empty<UpdateType>() // receive all update types
};
botClient.StartReceiving(
HandleUpdateAsync,
HandleErrorAsync,
receiverOptions,
cancellationToken: cts.Token);
var me = await botClient.GetMe();
Console.WriteLine($"Bot started: @{me.Username}");
Console.ReadLine(); // Keep app running
cts.Cancel(); // Gracefully stop
}
static async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken ct)
{
if (update.Message is not { } message) return;
if (message.Text is not { } messageText) return;
Console.WriteLine($"Received message from {message.Chat.Id}: {messageText}");
await botClient.SendMessage(
chatId: message.Chat.Id,
text: $"You said: {messageText}",
cancellationToken: ct);
}
static Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken ct)
{
Console.WriteLine($"Bot error: {exception.Message}");
return Task.CompletedTask;
}
}
Replace YOUR_BOT_TOKEN_HERE with the token from BotFather, or read it from an environment variable before creating TelegramBotClient.
Step 4: Run your bot
From the project directory:
dotnet run
When the console shows your bot username, open Telegram, search for your bot (e.g. @MyCSharpHelperBot), and start a chat. Send any text message—the bot echoes it back.
Understanding the code
BotToken— your bot's unique credential for the Telegram Bot API.TelegramBotClient— main client fromTelegram.Botfor API calls.StartReceiving— starts long polling: continuously fetches updates and dispatches them toHandleUpdateAsyncand errors toHandleErrorAsync.HandleUpdateAsync— your bot logic. Filters for text messages and replies (you can branch on/start,/help, etc.).HandleErrorAsync— logs polling errors; extend with structured logging in production.CancellationTokenSource— lets you stop receiving gracefully when the process shuts down.
For development, polling is the simplest path. In production, many teams switch to HTTPS webhooks behind a valid certificate and store tokens in environment variables.
Next steps
This echo bot is a starting point. Common extensions:
- Inline keyboards — interactive buttons on messages
- Callback queries — handle button presses
- Conversations — multi-step flows (forms, wizards)
- External APIs — weather, news, CRM, or your own backend
- Databases — persist user state (SQLite, PostgreSQL, etc.)
- Deployment — run 24/7 on Azure, AWS, or a VPS; use HTTPS for webhooks when you outgrow polling
- AI and tools — pipe messages to an LLM with guardrails, or expose capabilities via an MCP server (see Building MCP Server Using .NET)
If you use AI assistants while iterating, keep review and tests in the loop—see What is vibe coding? for a disciplined take on AI-assisted development.
Conclusion
Building a Telegram bot with C# gives you a robust, scalable base for automation and user interaction. The Telegram.Bot library handles the API surface so you can focus on your bot's behavior. Start small, test in Telegram, then add keyboards, integrations, and deployment as requirements grow.