Программисты JAVA общаются здесь
Anonymous
TelegramBot не работает (Spring Boot, телеграммботы)
Сообщение
Anonymous » 12 янв 2025, 21:10
Недавно я начал писать своего первого бота для Telegram, используя Java 17, Spring boot и telegrambots-spring-boot-starter. Мой бот был зарегистрирован через BotFather в Telegram корректно. Теперь моя функция «но ничего не делать» и точки останова вообще не работают (похоже, что моя Java-программа не получает никаких команд от бота).
Мой класс BotController
Код: Выделить всё
package com.menkov.klim.weatherforecasttelegrambot.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
@Component
public class BotController extends TelegramLongPollingBot {
@lombok.Getter
@Value("${telegram.bot.username}")
private String botUsername;
@lombok.Getter
@Value("${telegram.bot.token}")
private String botToken;
@Override
public void onUpdateReceived(Update update) {
try {
String chatId = update.getMessage().getChatId().toString();
SendMessage message = new SendMessage();
message.setChatId(chatId);
message.setText("Hi!");
execute(message);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
Мой POM-файл
Код: Выделить всё
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.1.0
com.menkov.klim
WeatherForecastTelegramBot
0.0.1-SNAPSHOT
WeatherForecastTelegramBot
WeatherForecastTelegramBot
17
org.springframework.boot
spring-boot-starter-data-jpa
com.mysql
mysql-connector-j
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.telegram
telegrambots-spring-boot-starter
5.6.0
com.fasterxml.jackson.core
jackson-databind
org.flywaydb
flyway-core
org.flywaydb
flyway-mysql
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
Мой основной класс
Код: Выделить всё
package com.menkov.klim.weatherforecasttelegrambot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {
"com.menkov.klim.weatherforecasttelegrambot.controller"
})
public class WeatherForecastTelegramBotApplication {
public static void main(String[] args) {
SpringApplication.run(WeatherForecastTelegramBotApplication.class, args);
}
}
Я изменил версии зависимостей POM, но это не сработало.
Подробнее здесь:
https://stackoverflow.com/questions/765 ... legrambots
1736705421
Anonymous
Недавно я начал писать своего первого бота для Telegram, используя Java 17, Spring boot и telegrambots-spring-boot-starter. Мой бот был зарегистрирован через BotFather в Telegram корректно. Теперь моя функция «но ничего не делать» и точки останова вообще не работают (похоже, что моя Java-программа не получает никаких команд от бота). Мой класс BotController [code]package com.menkov.klim.weatherforecasttelegrambot.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; @Component public class BotController extends TelegramLongPollingBot { @lombok.Getter @Value("${telegram.bot.username}") private String botUsername; @lombok.Getter @Value("${telegram.bot.token}") private String botToken; @Override public void onUpdateReceived(Update update) { try { String chatId = update.getMessage().getChatId().toString(); SendMessage message = new SendMessage(); message.setChatId(chatId); message.setText("Hi!"); execute(message); } catch (TelegramApiException e) { e.printStackTrace(); } } } [/code] Мой POM-файл [code] xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 3.1.0 com.menkov.klim WeatherForecastTelegramBot 0.0.1-SNAPSHOT WeatherForecastTelegramBot WeatherForecastTelegramBot 17 org.springframework.boot spring-boot-starter-data-jpa com.mysql mysql-connector-j runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.telegram telegrambots-spring-boot-starter 5.6.0 com.fasterxml.jackson.core jackson-databind org.flywaydb flyway-core org.flywaydb flyway-mysql org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok [/code] Мой основной класс [code]package com.menkov.klim.weatherforecasttelegrambot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = { "com.menkov.klim.weatherforecasttelegrambot.controller" }) public class WeatherForecastTelegramBotApplication { public static void main(String[] args) { SpringApplication.run(WeatherForecastTelegramBotApplication.class, args); } } [/code] Я изменил версии зависимостей POM, но это не сработало. Подробнее здесь: [url]https://stackoverflow.com/questions/76514510/telegrambot-is-not-working-spring-boot-telegrambots[/url]