Building Telegram Bots with C#, .NET Framework, and the Telegram.Bot Library

Cuneyt Kuscu
adessoTurkey
Published in
3 min readJul 18, 2023

--

In our age, highly interactive and fast communication is essential for both companies and individual users. There are many applications that meet this description, and one of them is Telegram. The capabilities of this popular messaging application go beyond facilitating communication; they also include API and bot support to streamline our daily tasks.

In this article, we will explore together how to develop a Telegram bot using C#, .NET Framework, and the Telegram.Bot library, also you will learn how to enhance the bot’s interactivity.

Introduction to Telegram Bots

I will provide an overview of the Telegram platform, explaining how bots work and the benefits they offer.

  1. Telegram is an instant messaging application launched in 2013. While Telegram has many features, I will only focus on its bot support. Telegram bots are software programs developed by third-party developers that automate tasks on the Telegram platform. Some of the functions of bots include providing various services to users, delivering content, playing games, facilitating payments, and performing many types of tasks.
  2. Speaking of benefits, we can easily mention the general advantages such as completing automated tasks, producing content, offering ordering services, providing fun games, and performing data analysis with the ability to send notifications.

Getting Started with Bot Development in C#

I will guide you through the steps of developing a Telegram bot using the C# programming language. By the end of this section, you will have created a simple “Hello World” bot.

  1. To create a new Telegram bot, you need to send a message to a service called “BotFather,” which is another bot within the Telegram application. The visual below shows a newly created Telegram bot named “okr_2023_bot” and how it is created.
  2. After creating your bot, it is important not to lose the “token” value that we will use in the code. In our example, you can easily see this token value in the visual below
How to Create a new TelegramBot using BotFather

3. Now that we have covered the Telegram side, we can start developing the bot using C#. It might seem a bit complex at first glance, but I will explain everything step by step. Promise! (:

How to Code a Telegram Bot in C# language
  1. Inside the “Main” method, we create a new instance of the “TelegramBotClient” class from the Telegram.Bot library, and we pass the "token" value given to us by BotFather.
  2. We pass the “UpdateHandler” and “ErrorHandler” methods as parameters to the “StartReceiving” method of the created object. These methods will be triggered when there is any update or an error.
  3. In the “UpdateHandler” method, we capture the incoming message from the “update.Message.Text” property and send a response back.
  4. In the “ErrorHandler” method, we can perform actions such as logging or any other desired operation to handle errors.

Enhancing the Bot’s Abilities

To take the bot’s functionality a step further, we will leverage the flexibility provided by the C# language. In this step, you will be able to add features to your bot, such as sending images, audio, and location-based content. Additionally, you can customize your bot’s responses with new commands and triggers.

Note : You can review the “Upgrades” project within the application for the features I mentioned in step 3.

For more Information : https://github.com/cnytkuscu/TelegramBot

--

--