Recently I started writing my first telegram bot using Java 17, Spring boot and telegrambots-spring-boot-starter. My bot was registered via BotFather in Telegram correctly. Now, my but do nothing and breakpoints doesn’t work at all (it seems that my java program is not receiving any command from bot).
My BotController class
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();
}
}
}
My POM file
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.menkov.klim</groupId>
<artifactId>WeatherForecastTelegramBot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WeatherForecastTelegramBot</name>
<description>WeatherForecastTelegramBot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-spring-boot-starter</artifactId>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
My main class
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);
}
}
I`ve changed POM dependencies versions but it didnt work.
2
Answers
I`ve found the issue. The problem was in package-scanning.
you should start your code with this: