Integrate Telegram Bot using Java

Hi Guys,
A short and really useful blog on Telegram Bot.

Before writing ahead i want to say a big thank you to Telegram for providing such a rich API and flexibility to create our own bot.

Use Case

Official telegram site shows many use cases where bot is really useful.

Here i used telegram bot to test my services which is deployed in raspberry pi board and bot will work as adhoc between a raspberry pi and Mobile Device using which i am sending a different commands to device.


The sample java code which is used to register a bot with telegram server and echo to mobile device whatever you have sent to bot.


Use below snippet to download required dependency.


import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;

public class App extends TelegramLongPollingBot {

    public String getBotToken() {
        return "REPLACE_YOUR_KEY";

    public String getBotUsername() {
        return "Kode12Bot";

    public void onUpdateReceived(Update update) {
        if (update.hasMessage() && update.getMessage().hasText()) {
            // Create a SendMessage object with mandatory fields
            SendMessage message = new SendMessage().setChatId(update.getMessage().getChatId())
                    .setText("You Said: " + update.getMessage().getText());
            try {
                sendMessage(message); // Call method to send the message
            } catch (TelegramApiException e) {


    public static void main(String[] args) {

        TelegramBotsApi botsApi = new TelegramBotsApi();

        try {
            botsApi.registerBot(new App());
        } catch (TelegramApiException e) {

You just need to execute main method, that it and Your bot will start reacting.
Live Telegram Boot Screen

You need to create your bot and need to get token by talking with BotFather, you can talk to BotFather via TeleGram App by searching @BotFather. Refer for more.

Thanks Guys for reading.
Happy Learning.

Share current post by copy:

Yogesh P