5.3 KiB
So, you’d like to create your own Telegram bot with TelegramBots? Then Let's get You started quickly.
Grab the library
First you need to acquire the library and add it to your project. There are several ways to do this:
-
If you use Maven, Gradle, etc; you should be able to import the dependency directly from Maven Central Repository. For example:
-
With Maven:
<dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots</artifactId> <version>6.5.0</version> </dependency>
-
With Gradle:
implementation 'org.telegram:telegrambots:6.5.0'
-
-
Don't like the Maven Central Repository? It can also be grabbed from Jitpack.
-
Import the library .jar direclty to your project. You can find it here, don't forget to fetch the latest version, it usually is 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 or Eclipse
Building your first bot
Now that you have the library, you can start coding. There are few steps to follow, in this tutorial (for the sake of simplicity), we are going to build a Long Polling Bot:
-
Creating your actual bot: The class must extends
TelegramLongPollingBot
and implement necessary methods:public class MyAmazingBot extends TelegramLongPollingBot { @Override public void onUpdateReceived(Update update) { // TODO } @Override public String getBotUsername() { // TODO return null; } @Override public String getBotToken() { // TODO return null; } }
-
getBotUsername()
: This method must always return your Bot username. May look like:@Override public String getBotUsername() { return "myamazingbot"; }
-
getBotToken()
: This method must always return your Bot Token (If you don't know it, you may want to talk with @BotFather). May look like:@Override public String getBotToken() { return "123456789:qwertyuioplkjhgfdsazxcvbnm"; }
-
onUpdateReceived
: This method will be called when an Update is received by your bot. In this example, this method will just read messages and echo the same text:@Override public void onUpdateReceived(Update update) { // We check if the update has a message and the message has text if (update.hasMessage() && update.getMessage().hasText()) { SendMessage message = new SendMessage(); // Create a SendMessage object with mandatory fields message.setChatId(update.getMessage().getChatId().toString()); message.setText(update.getMessage().getText()); try { execute(message); // Call method to send the message } catch (TelegramApiException e) { e.printStackTrace(); } } }
-
-
Instantiate
TelegramBotsApi
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 ourmain
method:public class Main { public static void main(String[] args) { // TODO Instantiate Telegram Bots API // TODO Register our bot } }
-
Instantiate Telegram Bots API: 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) { // You can use your own BotSession implementation if needed. TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class); // TODO Register our bot } }
-
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 { TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class); botsApi.registerBot(new MyAmazingBot()); } catch (TelegramApiException e) { e.printStackTrace(); } } }
-
-
Play with your bot: Done, now you just need to run this
main
method and your Bot should start working.