skip to Main Content

I try to start telegram bot, but when I start the application I get this:

org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty

I want to set this velues using org.springframework.beans.factory.annotation.Value but it doesn’t work

pom.xml:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.coffeOrderBot</groupId>
    <artifactId>CoffeBot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CoffeeBot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <telegram.version>5.6.0</telegram.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.telegram/telegrambots -->
        <dependency>
            <groupId>org.telegram</groupId>
            <artifactId>telegrambots</artifactId>
            <version>${telegram.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
@SpringBootApplication
public class CoffeeBotApplication {

    public static void main(String[] args) {
        SpringApplication.run(CoffeeBotApplication.class, args);
    }
}
@Component
public class Bot extends TelegramLongPollingBot {

    private final static BotConfig CONFIG = new BotConfig();
/// Some code

@Component
public class BotInitializer {

@Autowired
private Bot bot;

@EventListener({ContextRefreshedEvent.class})
public void init() throws TelegramApiException {
    TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
    try {
        telegramBotsApi.registerBot(bot);
    } catch (TelegramApiException e){
        e.printStackTrace();
    }
}

}

@Configuration
@Data
@PropertySource("application.properties")
public class BotConfig {

    @Value("${bot.name}")
    String name;

    @Value("${bot.token}")
    String token;
}

I tryed to write:
@PropertySource(value = "application.properties")
@PropertySource(value = "classpath:application.properties")

2

Answers


  1. Maybe the values for the bot name and token are not being properly loaded from the application.properties file using the @PropertySource. The property names in your application.properties file should match the field names in your BotConfig class exactly. Then the BotConfig class should have exactly the same field names.

    application.properties:

    bot.name=your_bot_name
    bot.token=your_bot_token
    

    BotConfig:

    @Data
    @PropertySource("application.properties")
    public class BotConfig {
        @Value("${bot.name}")
        String name;
    
        @Value("${bot.token}")
        String token;
    }
    
    
    Login or Signup to reply.
  2. If application.properties is on the classpath which is in the default src/main/resources directory, Spring Boot will detect it automatically.
    There is no need to explicitly register a PropertySource.

    If you want to read values of @Value fields elsewhere, you can register these @Value fields as beans in the same config class.

    The code would look like this:

    @Configuration
    public class BotConfig {
    
        @Value("${bot.name}")
        String name;
    
        @Value("${bot.token}")
        String token;
    
        @Bean(name = "botName")
        public String getBotName() {
            return name;
        }
    
        @Bean(name = "botToken")
        public String getBotToken() {
            return token;
        }
    }
    
    @AllArgsConstructor
    @Component
    public class Bot extends TelegramLongPollingBot {
    
        private final String botName;
        private final String botToken;
        
    

    Finally, make sure that you have properties defined in application.properties.

    bot.name=<BOT_NAME>
    bot.token=<BOT_TOKEN>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search