From bef5f42eb4a1af810929f3b26a03947fabaad79b Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Wed, 22 Mar 2023 04:16:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=BA=D0=B8=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BA=D0=B0=D0=BD=D0=B0=D0=BB/=D0=B3=D1=80=D1=83=D0=BF?= =?UTF-8?q?=D0=BF=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../context/service/TelegramService.java | 2 ++ .../context/service/TelegramService.java | 4 +++- .../core/service/TelegramServiceImpl.java | 24 +++++++++++++++++++ .../core/service/TelegramServiceImpl.java | 23 ++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramService.java b/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramService.java index d905ac6..3125a8e 100644 --- a/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramService.java +++ b/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramService.java @@ -17,4 +17,6 @@ public interface TelegramService { Uni addCommand(@NotNull Collection botCommands); + Uni checkChatMember(@NotNull String personId, @NotNull String chatIdOrChannelId); + } diff --git a/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramService.java b/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramService.java index 80f363b..125d0b4 100644 --- a/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramService.java +++ b/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramService.java @@ -1,7 +1,7 @@ package dev.struchkov.godfather.telegram.simple.context.service; -import dev.struchkov.godfather.telegram.domain.ClientBotCommand; import dev.struchkov.godfather.telegram.domain.ChatAction; +import dev.struchkov.godfather.telegram.domain.ClientBotCommand; import org.jetbrains.annotations.NotNull; import java.util.Collection; @@ -16,4 +16,6 @@ public interface TelegramService { void addCommand(@NotNull Collection botCommands); + boolean checkChatMember(@NotNull String personId, @NotNull String chatIdOrChannelId); + } diff --git a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/TelegramServiceImpl.java b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/TelegramServiceImpl.java index fc1460a..1db5ca3 100644 --- a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/TelegramServiceImpl.java +++ b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/TelegramServiceImpl.java @@ -11,9 +11,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.telegram.telegrambots.meta.api.methods.ActionType; import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands; +import org.telegram.telegrambots.meta.api.methods.groupadministration.GetChatMember; import org.telegram.telegrambots.meta.api.methods.pinnedmessages.PinChatMessage; import org.telegram.telegrambots.meta.api.methods.pinnedmessages.UnpinChatMessage; import org.telegram.telegrambots.meta.api.methods.send.SendChatAction; +import org.telegram.telegrambots.meta.api.objects.chatmember.ChatMember; import org.telegram.telegrambots.meta.api.objects.commands.BotCommand; import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; @@ -113,6 +115,28 @@ public class TelegramServiceImpl implements TelegramService { .replaceWithVoid(); } + @Override + public Uni checkChatMember(@NotNull String personId, @NotNull String chatIdOrChannelId) { + final GetChatMember getChatMember = GetChatMember.builder() + .userId(Long.parseLong(personId)) + .chatId(chatIdOrChannelId) + .build(); + return Uni.createFrom().completionStage(getExecuteAsync(getChatMember)) + .onItem().ifNotNull().transform(chatMember -> "member".equals(chatMember.getStatus())) + .onItem().ifNull().continueWith(false); + } + + private CompletableFuture getExecuteAsync(GetChatMember myCommands) { + try { + return absSender.executeAsync(myCommands); + } catch (TelegramApiRequestException e) { + log.error(e.getApiResponse()); + } catch (TelegramApiException e) { + log.error(e.getMessage()); + } + return CompletableFuture.completedFuture(null); + } + private CompletableFuture getExecuteAsync(SetMyCommands myCommands) { try { return absSender.executeAsync(myCommands); diff --git a/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/service/TelegramServiceImpl.java b/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/service/TelegramServiceImpl.java index 5886061..6c14b8c 100644 --- a/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/service/TelegramServiceImpl.java +++ b/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/service/TelegramServiceImpl.java @@ -9,9 +9,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.telegram.telegrambots.meta.api.methods.ActionType; import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands; +import org.telegram.telegrambots.meta.api.methods.groupadministration.GetChatMember; import org.telegram.telegrambots.meta.api.methods.pinnedmessages.PinChatMessage; import org.telegram.telegrambots.meta.api.methods.pinnedmessages.UnpinChatMessage; import org.telegram.telegrambots.meta.api.methods.send.SendChatAction; +import org.telegram.telegrambots.meta.api.objects.chatmember.ChatMember; import org.telegram.telegrambots.meta.api.objects.commands.BotCommand; import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; @@ -115,4 +117,25 @@ public class TelegramServiceImpl implements TelegramService { } } + @Override + public boolean checkChatMember(@NotNull String personId, @NotNull String chatIdOrChannelId) { + final GetChatMember chatMember = GetChatMember.builder() + .userId(Long.parseLong(personId)) + .chatId(chatIdOrChannelId) + .build(); + try { + final ChatMember member = absSender.execute(chatMember); + if (checkNotNull(member)) { + final String status = member.getStatus(); + return "member".equals(status); + } + return false; + } catch (TelegramApiRequestException e) { + log.error(e.getApiResponse()); + } catch (TelegramApiException e) { + log.error(e.getMessage()); + } + return false; + } + }