Make longPollingBots or webHookBots in spring boot configuration

This commit is contained in:
zhaoyi 2018-05-12 14:52:57 +08:00
parent 431d945486
commit dcc4f29ddd

View File

@ -1,23 +1,24 @@
package org.telegram.telegrambots.starter; package org.telegram.telegrambots.starter;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.telegram.telegrambots.TelegramBotsApi; 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.LongPollingBot;
import org.telegram.telegrambots.generics.WebhookBot; import org.telegram.telegrambots.generics.WebhookBot;
import javax.annotation.PostConstruct;
import java.util.List; import java.util.List;
import java.util.Optional;
/** /**
* Receives all beand which are #LongPollingBot and #WebhookBot and register them in #TelegramBotsApi. * Receives all beand which are #LongPollingBot and #WebhookBot and register them in #TelegramBotsApi.
* #TelegramBotsApi added to spring context as well * #TelegramBotsApi added to spring context as well
*/ */
@Configuration @Configuration
public class TelegramBotStarterConfiguration implements CommandLineRunner { public class TelegramBotStarterConfiguration {
private final List<LongPollingBot> longPollingBots; private final List<LongPollingBot> longPollingBots;
@ -26,24 +27,34 @@ public class TelegramBotStarterConfiguration implements CommandLineRunner {
@Autowired @Autowired
private TelegramBotsApi telegramBotsApi; private TelegramBotsApi telegramBotsApi;
public TelegramBotStarterConfiguration(List<LongPollingBot> longPollingBots, public TelegramBotStarterConfiguration(@Autowired(required = false) List<LongPollingBot> longPollingBots,
List<WebhookBot> webHookBots) { @Autowired(required = false) List<WebhookBot> webHookBots) {
this.longPollingBots = longPollingBots; this.longPollingBots = longPollingBots;
this.webHookBots = webHookBots; this.webHookBots = webHookBots;
} }
@Override @PostConstruct
public void run(String... args) { public void registerBots() {
Optional.ofNullable(longPollingBots).ifPresent(bots -> {
bots.forEach(bot -> {
try { try {
for (LongPollingBot bot : longPollingBots) {
telegramBotsApi.registerBot(bot); telegramBotsApi.registerBot(bot);
} catch (TelegramApiRequestException e) {
throw new RuntimeException(e);
} }
for (WebhookBot bot : webHookBots) { });
});
Optional.ofNullable(webHookBots).ifPresent(bots -> {
bots.forEach(bot -> {
try {
telegramBotsApi.registerBot(bot); telegramBotsApi.registerBot(bot);
} catch (TelegramApiRequestException e) {
throw new RuntimeException(e);
} }
} catch (TelegramApiException e) { });
e.printStackTrace(); });
}
} }