From 46cdc7c584f31690e78877584f189a7095184020 Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Wed, 8 Nov 2023 17:07:02 +0200 Subject: [PATCH] removed AbstractRedTelBot.java --- .../bots/telegram/AbstractRedTelBot.java | 44 ------------------- .../yvasyliev/bots/telegram/RedTelBot.java | 40 +++++++++++++---- .../telegram/posts/PhotoGroupPostService.java | 2 +- 3 files changed, 33 insertions(+), 53 deletions(-) delete mode 100644 src/main/java/com/github/yvasyliev/bots/telegram/AbstractRedTelBot.java diff --git a/src/main/java/com/github/yvasyliev/bots/telegram/AbstractRedTelBot.java b/src/main/java/com/github/yvasyliev/bots/telegram/AbstractRedTelBot.java deleted file mode 100644 index eade0a9..0000000 --- a/src/main/java/com/github/yvasyliev/bots/telegram/AbstractRedTelBot.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.github.yvasyliev.bots.telegram; - -import org.springframework.beans.factory.annotation.Value; -import org.telegram.telegrambots.bots.TelegramLongPollingBot; -import org.telegram.telegrambots.meta.api.objects.Update; - -public abstract class AbstractRedTelBot extends TelegramLongPollingBot { - @Value("${BOT_USERNAME}") - private String botUsername; - - @Value("${telegram.admin.id}") - private String adminId; - - @Value("${telegram.channel.id}") - private String channelId; - - @Value("${GROUP_ID}") - private String groupId; - - public AbstractRedTelBot(String botToken) { - super(botToken); - } - - @Override - public void onUpdateReceived(Update update) { - } - - @Override - public String getBotUsername() { - return botUsername; - } - - public String getAdminId() { - return adminId; - } - - public String getChannelId() { - return channelId; - } - - public String getGroupId() { - return groupId; - } -} diff --git a/src/main/java/com/github/yvasyliev/bots/telegram/RedTelBot.java b/src/main/java/com/github/yvasyliev/bots/telegram/RedTelBot.java index ee35bb2..d9d5b7b 100644 --- a/src/main/java/com/github/yvasyliev/bots/telegram/RedTelBot.java +++ b/src/main/java/com/github/yvasyliev/bots/telegram/RedTelBot.java @@ -17,14 +17,13 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; -import org.telegram.telegrambots.meta.TelegramBotsApi; +import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.meta.api.methods.send.SendMediaGroup; import org.telegram.telegrambots.meta.api.methods.send.SendPhoto; import org.telegram.telegrambots.meta.api.objects.CallbackQuery; import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.User; -import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.generics.BotSession; import org.telegram.telegrambots.starter.AfterBotRegistration; @@ -34,7 +33,7 @@ import java.util.concurrent.CompletableFuture; @Component -public class RedTelBot extends AbstractRedTelBot { +public class RedTelBot extends TelegramLongPollingBot { private static final Logger LOGGER = LoggerFactory.getLogger(RedTelBot.class); @Autowired @@ -57,6 +56,18 @@ public class RedTelBot extends AbstractRedTelBot { @Autowired private DelayedBlockingExecutor delayedBlockingExecutor; + @Value("${telegram.chat.id}") + private String chatId; + + @Value("${telegram.channel.id}") + private String channelId; + + @Value("${telegram.admin.id}") + private String adminId; + + @Value("${telegram.bot.username}") + private String botUsername; + public RedTelBot(@Value("${telegram.bot.token}") String botToken) { super(botToken); } @@ -67,11 +78,6 @@ public void setBotSession(BotSession botSession) { LOGGER.info("{} started long polling.", getBotUsername()); } - public void startPolling() throws TelegramApiException { - botSession = context.getBean(TelegramBotsApi.class).registerBot(this); - LOGGER.info("{} started long polling.", getBotUsername()); - } - @PreDestroy public void stopPolling() { botSession.stop(); @@ -156,6 +162,7 @@ public CompletableFuture executeDelayed(SendPhoto sendPhoto) { return delayedBlockingExecutor.submit(() -> execute(sendPhoto)); } + // TODO: 11/8/2023 message.isCommand private Optional getCommand(Message message) { long userId = message.getFrom().getId(); if (message.hasText()) { @@ -172,4 +179,21 @@ private Optional getCommand(Message message) { private boolean looksLikeCommand(String text) { return text.matches("/\\w+"); } + + public String getChatId() { + return chatId; + } + + public String getChannelId() { + return channelId; + } + + public String getAdminId() { + return adminId; + } + + @Override + public String getBotUsername() { + return botUsername; + } } diff --git a/src/main/java/com/github/yvasyliev/service/telegram/posts/PhotoGroupPostService.java b/src/main/java/com/github/yvasyliev/service/telegram/posts/PhotoGroupPostService.java index 863a18e..4e0071c 100644 --- a/src/main/java/com/github/yvasyliev/service/telegram/posts/PhotoGroupPostService.java +++ b/src/main/java/com/github/yvasyliev/service/telegram/posts/PhotoGroupPostService.java @@ -68,7 +68,7 @@ public Optional> applyWithException(@NonNull String chatId, PhotoG public List sendExtraPhotos(int replyToMessageId, int forwardMessageId) throws ExecutionException, InterruptedException { var post = extraPhotos.remove(forwardMessageId); return post != null - ? sendDelayed(redTelBot.getGroupId(), replyToMessageId, post) + ? sendDelayed(redTelBot.getChatId(), replyToMessageId, post) : List.of(); }