Telegram Bots Help

Getting Started

So, you’d like to create your own Telegram bot with Telegram Bots? Then Let's get You started quickly.

Receive Updates

First you need to acquire the corresponding library and add it to your project, in this tutorial (for the sake of simplicity), we are going to build a Long Polling Bot:. There are several ways to do this:

<dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots-longpolling</artifactId> <version>7.10.0</version> </dependency>
implementation 'org.telegram:telegrambots-longpolling:7.10.0'

If you don´t like standard Maven Central Repository, see Jitpack steps here

Import the library .jar directly to your project. You can find it here, don't forget to fetch the latest version, it is usually a good idea.

Depending on the IDE you are using, the process to add a library is different, here is a video that may help with Intellij

Create your bot

Now that you have the library, you can start coding. There are few steps to follow:

  1. Creating your updates consumer: The class must implement LongPollingUpdateConsumer, for simplicity, you can use LongPollingSingleThreadUpdateConsumer instead:

    public class MyAmazingBot implements LongPollingSingleThreadUpdateConsumer { @Override public void consume(Update update) { // TODO } }
    • consume: This method will be called when an Update is received by your bot. In this example, this method will just read text messages and print the text received:

      @Override public void consume(Update update) { // We check if the update has a message and the message has text if (update.hasMessage() && update.getMessage().hasText()) { System.out.println(update.getMessage().getText()); } }

Create the Telegram Application and register your bot

Now let's start our bot

  1. Instantiate TelegramBotsLongPollingApplication and register our new bot:

    For this part, we need to actually perform 2 steps: Instantiate Telegram Api and Register our Bot. In this tutorial, we are going to do it in our main method:

    public class Main { public static void main(String[] args) { // TODO Instantiate Telegram Bots API // TODO Register our bot } }
  2. Instantiate Telegram Bots Application:

    Easy as well, just create a new instance. Remember that a single instance can handle different bots but each bot can run only once (Telegram doesn't support concurrent calls to GetUpdates):

    public class Main { public static void main(String[] args) { // Instantiate Telegram Bots API TelegramBotsLongPollingApplication botsApplication = new TelegramBotsLongPollingApplication(); // TODO Register our bot } }
  3. Register our bot:

    Now we need to register a new instance of our previously created bot class in the api:

    public class Main { public static void main(String[] args) { try { String botToken = "12345:YOUR_TOKEN"; TelegramBotsLongPollingApplication botsApplication = new TelegramBotsLongPollingApplication(); botsApplication.registerBot(botToken, new MyAmazingBot()); } catch (TelegramApiException e) { e.printStackTrace(); } } }
  4. Play with your bot:

    Done, now you just need to run this main method and your Bot should start working.

Send Messages

Let's continue with our previously created bot, now we are going to do it echo the received text back. First, you'll need to add a library to send messages, you can use the default implementation or create your own. There are several ways to do this:

<dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots-client</artifactId> <version>7.10.0</version> </dependency>
implementation 'org.telegram:telegrambots-client:7.10.0'

If you don´t like standard Maven Central Repository, see Jitpack steps here

Import the library .jar directly to your project. You can find it here, don't forget to fetch the latest version, it is usually a good idea.

Depending on the IDE you are using, the process to add a library is different, here is a video that may help with Intellij

Create an instance of the client

  1. Create your TelegramClient, you can use the default implementation as OkHttpTelegramClient:

    TelegramClient telegramClient = new OkHttpTelegramClient("12345:YOUR_TOKEN");
  2. Let's add it to our previous bot

    public class MyAmazingBot implements LongPollingSingleThreadUpdateConsumer { private TelegramClient telegramClient = new OkHttpTelegramClient("12345:YOUR_TOKEN"); @Override public void consume(Update update) { // We check if the update has a message and the message has text if (update.hasMessage() && update.getMessage().hasText()) { System.out.println(update.getMessage().getText()); } } }

Execute SendMessage method to echo the text received

Let's complete our amazing Bot that we created earlier:

public class MyAmazingBot implements LongPollingSingleThreadUpdateConsumer { private TelegramClient telegramClient = new OkHttpTelegramClient("12345:YOUR_TOKEN"); @Override public void consume(Update update) { // We check if the update has a message and the message has text if (update.hasMessage() && update.getMessage().hasText()) { // Create your send message object SendMessage sendMessage = new SendMessage(update.getMessage().getChatId(), update.getMessage().getText()); try { // Execute it telegramClient.execute(method); } catch (TelegramApiException e) { e.printStackTrace(); } } } }
Last modified: 23 November 2024