From dcc4f29ddd54ed497e15770135fde6ba8324e45f Mon Sep 17 00:00:00 2001 From: zhaoyi Date: Sat, 12 May 2018 14:52:57 +0800 Subject: [PATCH] Make longPollingBots or webHookBots in spring boot configuration --- .../TelegramBotStarterConfiguration.java | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java index ec851dc0..0d7058e9 100644 --- a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java +++ b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java @@ -1,23 +1,24 @@ 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.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.generics.LongPollingBot; import org.telegram.telegrambots.generics.WebhookBot; +import javax.annotation.PostConstruct; import java.util.List; +import java.util.Optional; /** * 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 { +public class TelegramBotStarterConfiguration { private final List longPollingBots; @@ -26,24 +27,34 @@ public class TelegramBotStarterConfiguration implements CommandLineRunner { @Autowired private TelegramBotsApi telegramBotsApi; - public TelegramBotStarterConfiguration(List longPollingBots, - List webHookBots) { + public TelegramBotStarterConfiguration(@Autowired(required = false) List longPollingBots, + @Autowired(required = false) List 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(); - } + @PostConstruct + public void registerBots() { + Optional.ofNullable(longPollingBots).ifPresent(bots -> { + bots.forEach(bot -> { + try { + telegramBotsApi.registerBot(bot); + } catch (TelegramApiRequestException e) { + throw new RuntimeException(e); + } + }); + }); + + Optional.ofNullable(webHookBots).ifPresent(bots -> { + bots.forEach(bot -> { + try { + telegramBotsApi.registerBot(bot); + } catch (TelegramApiRequestException e) { + throw new RuntimeException(e); + } + }); + }); }