Добавил возможность проверки подписки на канал/группу
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
fd7c53060a
commit
bef5f42eb4
@ -17,4 +17,6 @@ public interface TelegramService {
|
|||||||
|
|
||||||
Uni<Void> addCommand(@NotNull Collection<ClientBotCommand> botCommands);
|
Uni<Void> addCommand(@NotNull Collection<ClientBotCommand> botCommands);
|
||||||
|
|
||||||
|
Uni<Boolean> checkChatMember(@NotNull String personId, @NotNull String chatIdOrChannelId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package dev.struchkov.godfather.telegram.simple.context.service;
|
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.ChatAction;
|
||||||
|
import dev.struchkov.godfather.telegram.domain.ClientBotCommand;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -16,4 +16,6 @@ public interface TelegramService {
|
|||||||
|
|
||||||
void addCommand(@NotNull Collection<ClientBotCommand> botCommands);
|
void addCommand(@NotNull Collection<ClientBotCommand> botCommands);
|
||||||
|
|
||||||
|
boolean checkChatMember(@NotNull String personId, @NotNull String chatIdOrChannelId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,11 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.telegram.telegrambots.meta.api.methods.ActionType;
|
import org.telegram.telegrambots.meta.api.methods.ActionType;
|
||||||
import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands;
|
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.PinChatMessage;
|
||||||
import org.telegram.telegrambots.meta.api.methods.pinnedmessages.UnpinChatMessage;
|
import org.telegram.telegrambots.meta.api.methods.pinnedmessages.UnpinChatMessage;
|
||||||
import org.telegram.telegrambots.meta.api.methods.send.SendChatAction;
|
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.api.objects.commands.BotCommand;
|
||||||
import org.telegram.telegrambots.meta.bots.AbsSender;
|
import org.telegram.telegrambots.meta.bots.AbsSender;
|
||||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||||
@ -113,6 +115,28 @@ public class TelegramServiceImpl implements TelegramService {
|
|||||||
.replaceWithVoid();
|
.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) {
|
private CompletableFuture<Boolean> getExecuteAsync(SetMyCommands myCommands) {
|
||||||
try {
|
try {
|
||||||
return absSender.executeAsync(myCommands);
|
return absSender.executeAsync(myCommands);
|
||||||
|
@ -9,9 +9,11 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.telegram.telegrambots.meta.api.methods.ActionType;
|
import org.telegram.telegrambots.meta.api.methods.ActionType;
|
||||||
import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands;
|
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.PinChatMessage;
|
||||||
import org.telegram.telegrambots.meta.api.methods.pinnedmessages.UnpinChatMessage;
|
import org.telegram.telegrambots.meta.api.methods.pinnedmessages.UnpinChatMessage;
|
||||||
import org.telegram.telegrambots.meta.api.methods.send.SendChatAction;
|
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.api.objects.commands.BotCommand;
|
||||||
import org.telegram.telegrambots.meta.bots.AbsSender;
|
import org.telegram.telegrambots.meta.bots.AbsSender;
|
||||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user