Добавил возможность проверки подписки на канал/группу
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-03-22 04:16:35 +03:00
parent fd7c53060a
commit bef5f42eb4
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
4 changed files with 52 additions and 1 deletions

View File

@ -17,4 +17,6 @@ public interface TelegramService {
Uni<Void> addCommand(@NotNull Collection<ClientBotCommand> botCommands);
Uni<Boolean> checkChatMember(@NotNull String personId, @NotNull String chatIdOrChannelId);
}

View File

@ -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<ClientBotCommand> botCommands);
boolean checkChatMember(@NotNull String personId, @NotNull String chatIdOrChannelId);
}

View File

@ -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<Boolean> 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<ChatMember> 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<Boolean> getExecuteAsync(SetMyCommands myCommands) {
try {
return absSender.executeAsync(myCommands);

View File

@ -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;
}
}