Why is C# an excellent choice for a Telegram bot?
\n\nIf you still thought that Telegram bots are only written in Python or JavaScript, get ready to be surprised. C# with its strong typing, huge ecosystem, and speed is a real "heavyweight" for creating bots. Imagine: you write the code once, and the bot works for years without failures, processing thousands of messages per second. Sounds tempting, right?
\n\nIn this guide, we will create a Telegram bot in C# from scratch. You will learn how to register a bot with BotFather, connect the Telegram.Bot library, and write working code with command handling. No fluff — only practice and real examples.
What do we need?
\n\nBefore starting, make sure you have:
\n- \n
- .NET SDK (version 6.0 or newer) — download from the official website; \n
- Visual Studio or VS Code (any IDE where you feel comfortable); \n
- A Telegram account — you can't do without it; \n
- 5 minutes of free time — C# will do the rest. \n
Ready? Let's go!
\n\nStep 1. Get a bot token via BotFather
\n\nBefore writing code, you need to "create" the bot itself. Open Telegram and find @BotFather — this is the main "bot creator" in Telegram.
\n\n- \n
- Send the command
/newbot. \n - Come up with a name for the bot (e.g., MyFirstCSharpBot). \n
- Come up with a username (must end with bot, e.g., MyFirstCSharpBot). \n
- Get the token — a long string like
123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11. \n
Important: The token is the access key to your bot. Do not show it to anyone or publish it in open code. It's best to store it in appsettings.json or environment variables.
Step 2. Create a project and install the library
\n\nOpen the terminal and run the commands:
\n\ndotnet new console -n TelegramBotCSharp\ncd TelegramBotCSharp\ndotnet add package Telegram.Bot\n\nWe are using the Telegram.Bot library — this is the most popular and maintained library for working with the Telegram API in C#. It handles all the "dirty work": connecting to the server, processing updates, sending messages.
\n\nAfter installation, open the Program.cs file — this is where we will write the magic.
Step 3. Write the basic bot code
\n\nLet's start with the simplest: the bot will respond to the /start and /help commands. Here is the minimal working code:
using Telegram.Bot;\nusing Telegram.Bot.Exceptions;\nusing Telegram.Bot.Polling;\nusing Telegram.Bot.Types;\nusing Telegram.Bot.Types.Enums;\n\nvar botClient = new TelegramBotClient("YOUR_TOKEN_HERE");\n\nusing var cts = new CancellationTokenSource();\n\nvar receiverOptions = new ReceiverOptions\n{\n AllowedUpdates = Array.Empty<UpdateType>() // receive all update types\n};\n\nbotClient.StartReceiving(\n updateHandler: HandleUpdateAsync,\n pollingErrorHandler: HandlePollingErrorAsync,\n receiverOptions: receiverOptions,\n cancellationToken: cts.Token\n);\n\nConsole.WriteLine("Bot started! Press Enter to stop.");\nConsole.ReadLine();\ncts.Cancel();\n\nasync Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)\n{\n if (update.Message is not { } message)\n return;\n if (message.Text is not { } messageText)\n return;\n\n var chatId = message.Chat.Id;\n Console.WriteLine($"Received message '{messageText}' from {chatId}");\n\n if (messageText == "/start")\n {\n await botClient.SendTextMessageAsync(\n chatId: chatId,\n text: "