Telegram bot in Java: where to start and how not to give up halfway
Have you ever wanted to write your own Telegram bot, but thought it was too difficult? I'm happy to tell you: creating a Telegram bot in Java is a task that even a beginner can handle. No magic, just a clear plan and a bit of code.
In this guide, we will go through the entire journey from registering a bot in Telegram to publishing a finished application. You will learn how to connect the TelegramBots library, handle commands, send messages, and avoid common mistakes. At the end — a ready-made template that you can adapt to your tasks.
Why Java? This language offers stability, performance, and a huge number of ready-made solutions. For a Telegram bot in Java, you don't need to install dozens of dependencies — everything fits in one Maven/Gradle project. Let's go!
Step 1: Registering the bot and getting a token
Before writing code, you need to "create" the bot. This is done through the main Telegram bot — BotFather.
Start BotFather in Telegram and send the command:
/newbotThen follow the instructions: come up with a name (it will be visible to users) and a username (must end with _bot). After successful creation, you will receive a token — a long string like:
1234567890:ABCdefGHIjklmNOPqrstUVwxyz-1234567Important: the token is the key to your bot. Do not show it to anyone, do not upload it to public repositories. It's better to store it in environment variables or configuration files.
Step 2: Creating the project and connecting dependencies
We will use Maven — it's convenient and standard for Java projects. If you prefer Gradle, the principle is the same, just rewrite the dependencies for your format.
Create a regular Java project in your IDE (IntelliJ IDEA, Eclipse, or NetBeans) and open the pom.xml file. Add the following dependency to it:
<dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots</artifactId> <version>6.9.7.1</version></dependency>This library — TelegramBots — is the most popular for creating bots in Java. It supports all the main features: sending messages, images, buttons, working with Inline mode and Webhooks.
After adding the dependency, run mvn clean install or update the project in the IDE to download the library.
Step 3: Writing the first bot — basic class
Create a class that inherits from TelegramLongPollingBot. This class will be responsible for receiving updates from Telegram servers. Here is the minimal structure:
import org.telegram.telegrambots.bots.TelegramLongPollingBot;import org.telegram.telegrambots.meta.api.objects.Update;import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
public class MySimpleBot extends TelegramLongPollingBot {
@Override public String getBotUsername() { return "YourBotUsername"; // without @ }
@Override public String getBotToken() { return "YOUR_TOKEN_HERE"; }
@Override public void onUpdateReceived(Update update) { // We will handle messages here if (update.hasMessage() && update.getMessage().hasText()) { String messageText = update.getMessage().getText(); long chatId = update.getMessage().getChatId(); System.out.println("Received message: " + messageText + " from chat: " + chatId); } }}For now, the bot only logs incoming messages. Let's add a response action.
Step 4: Sending a reply — the bot starts talking
To make the bot respond, use the SendMessage class. Let's add command handling