skip to Main Content

I’ve just started to go away from tasks of the university and do my own projects.
I want to program a Java Telegram Bot to interact with further classes. Unfortunately I’m not able to add the dependency right or it just cant import all of the functions. I tried to follow multiple tutorials but I got errors in either of them. One of the most promising tutorials was the following: https://github.com/rubenlagus/TelegramBots/wiki/Getting-Started

I followed the instructions (added the library with Maven) and put in the code. After this I imported the needed librarie. However the program isn’t able to call the method “execute” and I don’t know why.
I hope I specified the topic detailled enough.
Thank you in advance.

Main Class:

import org.telegram.telegrambots.ApiContextInitializer;
        import org.telegram.telegrambots.meta.TelegramBotsApi;
        import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
        import org.telegram.telegrambots.meta.generics.LongPollingBot;

public class Main {
    public static void main(String[] args) {

        ApiContextInitializer.init();

        TelegramBotsApi botsApi = new TelegramBotsApi();

        try {
            botsApi.registerBot((LongPollingBot) new Bot());
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}

Bot Class

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

public class Bot extends TelegramLongPollingBot {

    @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
                    .setChatId(update.getMessage().getChatId())
                    .setText(update.getMessage().getText());
            try {
                execute(message); // Call method to send the message
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String getBotUsername() {
        return null;
    }

    @Override
    public String getBotToken() {
        return null;
    }

    @Override
    public void onClosing() {

    }
}

Error

Error:(16, 17) java: cannot find symbol
  symbol:   method execute(org.telegram.telegrambots.meta.api.methods.send.SendMessage)
  location: class Bot

2

Answers


  1. in the POM.xml file I added the follow dependency:

    <!-- https://mvnrepository.com/artifact/org.telegram/telegrambots -->
    <dependency>
        <groupId>org.telegram</groupId>
        <artifactId>telegrambots</artifactId>
        <version>4.9.2</version>
    </dependency>
    

    You can also download directly the jar.
    You must create and define the bot with BotFather, use a username, take the token and create the class that extends TelegramLongPollingBot.

    Your problem is on the token and username that return always null. You must return the configuration created with BothFather.

    Login or Signup to reply.
  2. You must set in the getters botToken and botUsername the token and username of bot.

    return "<token>";
    
    return "<username_of_bot>";
    

    at least minimum the token.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search