This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to spring boot 2.6.2 and telegram bots 5.6.0
- Loading branch information
1 parent
6d5580a
commit debea73
Showing
3 changed files
with
66 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/com/github/xabgesagtx/bots/TelegramBotStarter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.github.xabgesagtx.bots; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.telegram.telegrambots.bots.TelegramWebhookBot; | ||
import org.telegram.telegrambots.meta.TelegramBotsApi; | ||
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
import org.telegram.telegrambots.meta.generics.BotSession; | ||
import org.telegram.telegrambots.meta.generics.LongPollingBot; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.annotation.PreDestroy; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class TelegramBotStarter { | ||
|
||
private final List<BotSession> sessions = new ArrayList<>(); | ||
private final List<LongPollingBot> pollingBots; | ||
private final List<TelegramWebhookBot> webHookBots; | ||
private final TelegramBotsApi api; | ||
private final SetWebhookBuilderFactory setWebhookFactory; | ||
|
||
@PostConstruct | ||
public void start() { | ||
log.info("Starting auto config for telegram bots"); | ||
pollingBots.forEach(bot -> { | ||
try { | ||
log.info("Registering polling bot: {}", bot.getBotUsername()); | ||
sessions.add(api.registerBot(bot)); | ||
} catch (TelegramApiException e) { | ||
log.error("Failed to register bot {} due to error", bot.getBotUsername(), e); | ||
} | ||
}); | ||
webHookBots.forEach(bot -> { | ||
try { | ||
log.info("Registering web hook bot: {}", bot.getBotUsername()); | ||
SetWebhook.SetWebhookBuilder webhookBuilder = setWebhookFactory.create(); | ||
if (bot instanceof CustomizableTelegramWebhookBot) { | ||
((CustomizableTelegramWebhookBot) bot).customizeWebHook(webhookBuilder); | ||
} | ||
api.registerBot(bot, webhookBuilder.build()); | ||
} catch (TelegramApiException e) { | ||
log.error("Failed to register bot {} due to error", bot.getBotUsername(), e); | ||
} | ||
}); | ||
} | ||
|
||
@PreDestroy | ||
public void stop() { | ||
sessions.forEach(session -> { | ||
if (session != null) { | ||
session.stop(); | ||
} | ||
}); | ||
} | ||
|
||
} |