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
in the POM.xml file I added the follow 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.
You must set in the getters botToken and botUsername the token and username of bot.
at least minimum the token.