Не работает телеграмм бот
Ошибка целиком:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/cache/RemovalListener
at com.google.inject.internal.InheritingState.<init>(InheritingState.java:63)
at com.google.inject.internal.InjectorShell$Builder.getState(InjectorShell.java:208)
at com.google.inject.internal.InjectorShell$Builder.lock(InjectorShell.java:114)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:102)
at com.google.inject.Guice.createInjector(Guice.java:87)
at com.google.inject.Guice.createInjector(Guice.java:69)
at com.google.inject.Guice.createInjector(Guice.java:59)
at org.telegram.telegrambots.meta.ApiContext.getInjector(ApiContext.java:48)
at org.telegram.telegrambots.meta.ApiContext.getInstance(ApiContext.java:27)
at org.telegram.telegrambots.bots.TelegramLongPollingBot.<init>(TelegramLongPollingBot.java:16)
at Bot.<init>(Bot.java:8)
at Bot.main(Bot.java:13)
Caused by: java.lang.ClassNotFoundException: com.google.common.cache.RemovalListener
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 12 more
класс бот
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
public class Bot extends TelegramLongPollingBot {
public static void main(String[] args) {
ApiContextInitializer.init();
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
TelegramBotsApi telegramBotsApi =new TelegramBotsApi();
try{
telegramBotsApi.registerBot(new Bot());
}catch (TelegramApiRequestException e){
e.printStackTrace();
}
}
public void onUpdateReceived(Update update) {
}
public String getBotUsername() {
return "FrilanceExampleBot";
}
public String getBotToken() {
return "1009780375:AAE5zUAX8BtUOd7cFrvJSWyLXzBzkbcP3ts";
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>WeatherBot</groupId>
<artifactId>Bot</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
</project>
Ответы (1 шт):
Автор решения: LG03
→ Ссылка
День убил, но нашел нужное. https://www.surgebook.com/open/book/pishem-telegram-bota-na-java/predoslovie_g5 следуйте и все получится. Кроме того, обязательно включайте VPN
Исходники: pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled1</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Main:
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
public class Main {
public static void main(String[] args) {
ApiContextInitializer.init();
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
try {
telegramBotsApi.registerBot(new Bot());
} catch (TelegramApiRequestException e) {
e.printStackTrace();
}
}
}
Bot:
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMediaGroup;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
public class Bot extends TelegramLongPollingBot {
public void onUpdateReceived(Update update) {
update.getUpdateId();
String chat_id = String.valueOf(update.getMessage().getChatId());
SendMessage sendMessage = new SendMessage().setChatId(chat_id);
if(update.getMessage().getText().equals("Привет")){
sendMessage.setText("Привет дружище");
try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}