Telegram Bots Help

How To Update 7

Library dependencies changes

The library has been divided in small pieces, so you only take what you need:

Before

<dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots</artifactId> <version>6.9.7.1</version> </dependency>

After

<!-- Long polling bot --> <dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots-longpolling</artifactId> <version>7.0.0</version> </dependency> <!-- Webhook bot --> <dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots-webhook</artifactId> <version>7.0.0</version> </dependency> <!-- OkHttp client implementation --> <dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots-client</artifactId> <version>7.0.0</version> </dependency>

Migrating your existing LongPolling bots

TelegramLongPollingBot has been removed and LongPollingUpdateConsumer is its replacement.

Migrate Long Polling Bot

For convenience, a default extension of LongPollingUpdateConsumer is added as LongPollingSingleThreadUpdateConsumer.

  1. Instead of extend TelegramLongPollingBot, implement LongPollingSingleThreadUpdateConsumer.

  2. Instead of overriding void onUpdateReceived(Update update), override void consume(Update update).

  3. Remove any call to super constructor.

  4. Remove any other overrides from previous versions.

Migrate Long Polling Bot

  1. Instead of extend TelegramLongPollingBot, implement LongPollingUpdateConsumer.

  2. Instead of overriding void onUpdatesReceived(List<Update> updates), override void consume(List<Update> updates).

  3. Remove any call to super constructor.

  4. Remove any other overrides from previous versions.

Migrating your existing Webhook bots

TelegramWebhookBot has become an interface instead of an abstract class.

Migrate Webhook Bot

  1. Instead of extend TelegramWebhookBot, implement it.

  2. Instead of overriding BotApiMethod onWebhookUpdateReceived(Update update), override BotApiMethod<?> consumeUpdate(Update update).

  3. Implement your own version of void runDeleteWebhook() and void runSetWebhook().

  4. In getBotPath(), make sure your path starts with /.

Migrate your TelegramBotsApi

Long Polling

For long polling bots, you can start the bot application and start registering your bots:

TelegramBotsLongPollingApplication botsApplication = new TelegramBotsLongPollingApplication(); botsApplication.registerBot("TOKEN", new AmazingBot());

You have available multiple constructors and implementations of registerBot to allow further customisation.

Webhook

For webhook bots, you can start the bot application and start registering your bots:

TelegramBotsWebhookApplication webhookApplication = new TelegramBotsWebhookApplication(); botsApplication.registerBot(new AmazingBot());

You have available multiple constructors and implementations of registerBot to allow further customisation.

Sending API requests

A new class TelegramClient will allow you to perform Telegram API requests independent on the updates consumption.

For convenience, an implementation using okHttp is provided as OkHttpTelegramClient.

TelegramClient telegramClient = new OkHttpTelegramClient("TOKEN"); telegramClient.execute(new SendMessage("chatId", "text"));

Classes that have moved packages

1BotApiMethod and PartialBotApiMethod have been moved to package org.telegram.telegrambots.meta.api.methods.botapimethods 2Message and InaccessibleMessage classes have been moved to org.telegram.telegrambots.meta.api.objects.message.Message

New way to provide Telegram URL

You can use class TelegramUrl to provide your custom URL for your Telegram Bots API.

For convenience, use TelegramUrl.DEFAULT_URL to use the default Telegram-hosted bots API.

You can build your own like:

TelegramUrl .builder() .schema("https") .host("api.telegram.org") .port(443) .build();

Mandatory parameters must be always included when creating new objects

All methods with mandatory parameters have lost their no-arguments constructor and enforce to provide them in the constructor.

To show with an example, let's use SendMessage method that requires chatId and text as mandatory:

SendMessage sendMessage = new SendMessage(); sendMessage.setChatId(); sendMessage.setText("Text");
SendMessage sendMessage = new SendMessage("chatId", "text");

Alternatively, you can use the provided builders for them:

SendMessage sendMessage = SendMessage .builder() .chatId("chatId") .text("text) .build();

Inline keyboard now use a new row object

When creating inline keyboards, you now have available a InlineKeyboardRow so a keyboard would be a List<InlineKeyboardRow>:

Here are a couple of example of how the code would change using builders:

InlineKeyboardMarkup keyboard = InlineKeyboardMarkup .builder() .keyboardRow(Arrays.asList(InlineKeyboardButton .builder() .text("Row 1 Column 1") .callbackData("Row 1 Column 1") .build(), InlineKeyboardButton .builder() .text("Row 1 Column 2") .callbackData("Row 1 Column 2") .build()) ) .keyboardRow(Arrays.asList(InlineKeyboardButton .builder() .text("Row 2 Column 1") .callbackData("Row 2 Column 1") .build(), InlineKeyboardButton .builder() .text("Row 2 Column 2") .callbackData("Row 2 Column 2") .build()) ) .build();
InlineKeyboardMarkup keyboard = InlineKeyboardMarkup .builder() .keyboardRow(new InlineKeyboardRow(InlineKeyboardButton .builder() .text("Row 1 Column 1") .callbackData("Row 1 Column 1") .build(), InlineKeyboardButton .builder() .text("Row 1 Column 2") .callbackData("Row 1 Column 2") .build()) ) .keyboardRow(new InlineKeyboardRow(InlineKeyboardButton .builder() .text("Row 2 Column 1") .callbackData("Row 2 Column 1") .build(), InlineKeyboardButton .builder() .text("Row 2 Column 2") .callbackData("Row 2 Column 2") .build()) ) .build();

or

InlineKeyboardMarkup keyboard = InlineKeyboardMarkup .builder() .keyboard(Arrays.asList( Arrays.asList(InlineKeyboardButton .builder() .text("Row 1 Column 1") .callbackData("Row 1 Column 1") .build(), InlineKeyboardButton .builder() .text("Row 1 Column 2") .callbackData("Row 1 Column 2") .build()), Arrays.asList(InlineKeyboardButton .builder() .text("Row 2 Column 1") .callbackData("Row 2 Column 1") .build(), InlineKeyboardButton .builder() .text("Row 2 Column 2") .callbackData("Row 2 Column 2") .build()) )) .build();
InlineKeyboardMarkup keyboard = InlineKeyboardMarkup .builder() .keyboard(List.of( new InlineKeyboardRow(InlineKeyboardButton .builder() .text("Row 1 Column 1") .callbackData("Row 1 Column 1") .build(), InlineKeyboardButton .builder() .text("Row 1 Column 2") .callbackData("Row 1 Column 2") .build()), new InlineKeyboardRow(InlineKeyboardButton .builder() .text("Row 2 Column 1") .callbackData("Row 2 Column 1") .build(), InlineKeyboardButton .builder() .text("Row 2 Column 2") .callbackData("Row 2 Column 2") .build()) )) .build();
Last modified: 18 November 2024