Telegram bot in Kotlin: create from scratch in an hour

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Kotlin is the ideal language for a Telegram bot?

\n

Have you ever wondered why Kotlin has won the hearts of developers so quickly? The thing is, this language combines the conciseness of Python and the power of Java, but without their drawbacks. When it comes to creating a Telegram bot in Kotlin, you get not just a working solution, but elegant, high-performance, and easily maintainable code.

\n

Imagine: you write a bot that processes thousands of requests per second, runs without failures, and the code takes up half as many lines as in Java. Sounds like a dream? In reality, it's a fact. A Telegram bot in Kotlin is not only trendy but also practical. Especially if you are already familiar with the JVM ecosystem or want to try something new after Python.

\n

In this guide, we will create a full-fledged bot step by step: from registration in BotFather to deployment on a server. You will learn how to handle commands, work with keyboards, and even send files. Let's go!

\n\n

Step 1: Setting up the environment and registering the bot

\n

Before writing code, we need to get a token — an access key to the Telegram API. This is done through the official bot BotFather. The process will take less than a minute.

\n
    \n
  • Open Telegram and find @BotFather.
  • \n
  • Send the command /newbot.
  • \n
  • Come up with a name (e.g., MyKotlinBot) and a username ending in bot (e.g., MyKotlinBot).
  • \n
  • Save the received token — you will need it for connection.
  • \n
\n

Now let's set up the project. I recommend using IntelliJ IDEA Community Edition (it's free and works great with Kotlin). Create a new Gradle project with Kotlin DSL.

\n\n

Adding dependencies

\n

To work with the Telegram API, we will use the TelegramBots library — this is the de facto standard in the Kotlin and Java world. Open the build.gradle.kts file and add:

\n
dependencies {\n    implementation("org.telegram:telegrambots:6.9.7.1")\n    implementation("org.telegram:telegrambotsextensions:6.9.7.1")\n    implementation("ch.qos.logback:logback-classic:1.5.6")\n}
\n

The first library is the core, the second is useful extensions (e.g., for command handling), the third is logging to see errors.

\n\n

Step 2: Writing your first bot in Kotlin

\n

Create a class MyBot that inherits from TelegramLongPollingBot. This is the simplest way: the bot will poll the Telegram server for new messages on its own.

\n
import org.telegram.telegrambots.bots.TelegramLongPollingBot\nimport org.telegram.telegrambots.meta.api.objects.Update\n\nclass MyBot : TelegramLongPollingBot() {\n\n    override fun getBotUsername(): String = "MyKotlinBot" // your username\n\n    override fun getBotToken(): String = "YOUR_TOKEN_HERE" // token from BotFather\n\n    override fun onUpdateReceived(update: Update?) {\n        // Just a stub for now\n        if (update?.hasMessage() == true && update.message.hasText()) {\n            println("Received message: ${update.message.text}")\n        }\n    }\n}
\n

Now you need to start the bot. Create a file Main.kt with the entry point:

\n
import org.telegram.telegrambots.meta.TelegramBotsApi\nimport org.telegram.telegrambots.updatesreceivers.DefaultBotSession\n\nfun main() {\n    val botsApi = TelegramBotsApi(DefaultBotSession::class.java)\n    botsApi.registerBot(MyBot())\n    println("Bot started!")\n}
\n

Run it — and your first Telegram bot in Kotlin is already alive! For now, it only outputs messages to the console, but this is just the beginning.

\n\n

Step 3: Handling commands and sending replies

\n

Just reading messages is boring. Let's teach the bot to respond to commands. To do this, we'll use the @BotCommand annotation from the extensions, but first, let's rewrite the class so that it inherits

Blogs

Book Recommendations