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:
If you don´t like standard Maven Central Repository, see Jitpack steps here
Create your bot
Now that you have the library, you can start coding. There are few steps to follow:
Creating your updates consumer: The class must implement
LongPollingUpdateConsumer
, for simplicity, you can useLongPollingSingleThreadUpdateConsumer
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
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 } }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 } }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(); } } }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:
If you don´t like standard Maven Central Repository, see Jitpack steps here
Create an instance of the client
Create your
TelegramClient
, you can use the default implementation asOkHttpTelegramClient
:TelegramClient telegramClient = new OkHttpTelegramClient("12345:YOUR_TOKEN");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: