【发布时间】:2020-06-27 07:56:33
【问题描述】:
我刚刚开始摆脱大学的任务并开始自己的项目。 我想编写一个 Java Telegram Bot 来与更多的类进行交互。不幸的是,我无法添加依赖权,或者它只是无法导入所有功能。我尝试遵循多个教程,但其中任何一个都有错误。最有前途的教程之一如下:https://github.com/rubenlagus/TelegramBots/wiki/Getting-Started
我按照说明(使用 Maven 添加了库)并输入了代码。在此之后,我导入了所需的库。但是程序无法调用方法“执行”,我不知道为什么。 我希望我指定的主题足够详细。 提前谢谢你。
主类:
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();
}
}
}
机器人类
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:(16, 17) java: cannot find symbol
symbol: method execute(org.telegram.telegrambots.meta.api.methods.send.SendMessage)
location: class Bot
【问题讨论】:
-
你能发送 pom.xml 文件吗?还是你用了jar?
-
我猜我用了一个罐子。因为我刚刚开始编程并且无法解决问题,所以项目不再进行。我不知道我什么时候会回到它。