package org.telegram.telegrambots.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.generics.LongPollingBot;
import org.telegram.telegrambots.generics.WebhookBot;

import java.util.List;

/**
 * Receives all beand which are #LongPollingBot and #WebhookBot and register them in #TelegramBotsApi.
 * #TelegramBotsApi added to spring context as well
 */
@Configuration
public class TelegramBotStarterConfiguration implements CommandLineRunner {


    private final List<LongPollingBot> longPollingBots;
    private final List<WebhookBot> webHookBots;

    @Autowired
    private TelegramBotsApi telegramBotsApi;

    public TelegramBotStarterConfiguration(List<LongPollingBot> longPollingBots,
                                           List<WebhookBot> webHookBots) {
        this.longPollingBots = longPollingBots;
        this.webHookBots = webHookBots;
    }

    @Override
    public void run(String... args) {
        try {
            for (LongPollingBot bot : longPollingBots) {
                telegramBotsApi.registerBot(bot);
            }
            for (WebhookBot bot : webHookBots) {
                telegramBotsApi.registerBot(bot);
            }
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }


    @Bean
    @ConditionalOnMissingBean(TelegramBotsApi.class)
    public TelegramBotsApi telegramBotsApi() {
        return new TelegramBotsApi();
    }
}

@@ -104,59 +104,8 @@
 ed;%0A
-import org.springframework.boot.CommandLineRunner;%0A
 impo
@@ -392,32 +392,39 @@
 ions.TelegramApi
+Request
 Exception;%0Aimpor
@@ -546,18 +546,84 @@
 java
-.util.List
+x.annotation.PostConstruct;%0Aimport java.util.List;%0Aimport java.util.Optional
 ;%0A%0A/
@@ -848,37 +848,8 @@
 ion 
-implements CommandLineRunner 
 %7B%0A%0A%0A
@@ -1057,16 +1057,45 @@
 uration(
+@Autowired(required = false) 
 List%3CLon
@@ -1166,16 +1166,45 @@
         
+ @Autowired(required = false)
  List%3CWe
@@ -1220,32 +1220,33 @@
  webHookBots) %7B%0A
+%0A
         this.lon
@@ -1333,16 +1333,21 @@
    @
-Override
+PostConstruct
 %0A   
@@ -1364,213 +1364,476 @@
 id r
-un(String... args) %7B%0A        try %7B%0A            for (LongPollingBot bot : longPollingBots) %7B%0A                telegramBotsApi.registerBot(bot);%0A            %7D%0A            for (Webh
+egisterBots() %7B%0A        Optional.ofNullable(longPollingBots).ifPresent(bots -%3E %7B%0A            bots.forEach(bot -%3E %7B%0A                try %7B%0A                    telegramBotsApi.registerBot(bot);%0A                %7D catch (TelegramApiRequestException e) %7B%0A                    throw new RuntimeException(e);%0A                %7D%0A            %7D);%0A        %7D);%0A%0A        Optional.ofNullable(webH
 ookBot
- bot : webHookBots) %7B%0A
+s).ifPresent(bots -%3E %7B%0A            bots.forEach(bot -%3E %7B%0A                try %7B%0A    
     
@@ -1887,29 +1887,23 @@
         
-   %7D%0A
        
- 
 %7D catch 
@@ -1914,16 +1914,23 @@
 egramApi
+Request
 Exceptio
@@ -1952,26 +1952,44 @@
     
-e.printStackTrace(
+        throw new RuntimeException(e
 );%0A 
@@ -1991,25 +1991,61 @@
 e);%0A        
-%7D
+        %7D%0A            %7D);%0A        %7D);
 %0A    %7D%0A%0A%0A   

Make longPollingBots or webHookBots in spring boot configuration