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 d2c39d5..d905ac6 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 @@ -1,9 +1,12 @@ package dev.struchkov.godfather.telegram.quarkus.context.service; import dev.struchkov.godfather.telegram.domain.ChatAction; +import dev.struchkov.godfather.telegram.domain.ClientBotCommand; import io.smallrye.mutiny.Uni; import org.jetbrains.annotations.NotNull; +import java.util.Collection; + public interface TelegramService { Uni executeAction(@NotNull String personId, ChatAction chatAction); @@ -12,4 +15,6 @@ public interface TelegramService { Uni unPinMessage(@NotNull String personId, @NotNull String messageId); + Uni addCommand(@NotNull Collection botCommands); + } 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 cdc376c..80f363b 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,8 +1,11 @@ package dev.struchkov.godfather.telegram.simple.context.service; +import dev.struchkov.godfather.telegram.domain.ClientBotCommand; import dev.struchkov.godfather.telegram.domain.ChatAction; import org.jetbrains.annotations.NotNull; +import java.util.Collection; + public interface TelegramService { void executeAction(@NotNull String personId, ChatAction chatAction); @@ -11,4 +14,6 @@ public interface TelegramService { void unPinMessage(@NotNull String personId, @NotNull String messageId); + void addCommand(@NotNull Collection botCommands); + } 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 701c2e0..afaab45 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 @@ -1,22 +1,36 @@ package dev.struchkov.godfather.telegram.quarkus.core.service; import dev.struchkov.godfather.telegram.domain.ChatAction; +import dev.struchkov.godfather.telegram.domain.ClientBotCommand; import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramService; +import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.Uni; import org.jetbrains.annotations.NotNull; 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.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.commands.BotCommand; import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; +import java.util.Collection; +import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; +import java.util.stream.Collectors; + +import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; +import static dev.struchkov.haiti.utils.Checker.checkNotNull; +import static dev.struchkov.haiti.utils.Checker.checkNull; +import static java.util.stream.Collectors.mapping; +import static java.util.stream.Collectors.toList; public class TelegramServiceImpl implements TelegramService { @@ -58,6 +72,58 @@ public class TelegramServiceImpl implements TelegramService { .replaceWithVoid(); } + @Override + public Uni addCommand(@NotNull Collection botCommands) { + return Uni.combine().all() + .unis( + Uni.createFrom().item( + botCommands.stream() + .filter(command -> checkNotNull(command.getLang())) + .collect( + Collectors.groupingBy( + ClientBotCommand::getLang, + mapping(clientCommand -> BotCommand.builder().command(clientCommand.getKey()).description(clientCommand.getDescription()).build(), toList()) + ) + ) + ), + Uni.createFrom().item( + botCommands.stream() + .filter(command -> checkNull(command.getLang())) + .map(clientCommand -> BotCommand.builder().command(clientCommand.getKey()).description(clientCommand.getDescription()).build()) + .toList() + ) + ).asTuple() + .call(t -> { + final List noLangCommands = t.getItem2(); + if (checkNotEmpty(noLangCommands)) { + return Uni.createFrom().completionStage( + getExecuteAsync( + SetMyCommands.builder().commands(noLangCommands).build() + ) + ); + } + return Uni.createFrom().voidItem(); + }) + .call(t -> { + final Map> commandMap = t.getItem1(); + return Multi.createFrom().iterable(commandMap.entrySet()) + .call(entry -> Uni.createFrom().completionStage(getExecuteAsync(SetMyCommands.builder().languageCode(entry.getKey()).commands(entry.getValue()).build()))) + .collect().asList().replaceWithVoid(); + }) + .replaceWithVoid(); + } + + private CompletableFuture getExecuteAsync(SetMyCommands 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(UnpinChatMessage unpinChatMessage) { try { return absSender.executeAsync(unpinChatMessage); 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 a78c756..3408a60 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 @@ -1,19 +1,33 @@ package dev.struchkov.godfather.telegram.simple.core.service; import dev.struchkov.godfather.telegram.domain.ChatAction; +import dev.struchkov.godfather.telegram.domain.ClientBotCommand; import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.simple.context.service.TelegramService; import org.jetbrains.annotations.NotNull; 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.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.commands.BotCommand; import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; +import static dev.struchkov.haiti.utils.Checker.checkNotNull; +import static dev.struchkov.haiti.utils.Checker.checkNull; +import static java.util.stream.Collectors.mapping; +import static java.util.stream.Collectors.toList; + public class TelegramServiceImpl implements TelegramService { private static final Logger log = LoggerFactory.getLogger(TelegramServiceImpl.class); @@ -29,6 +43,7 @@ public class TelegramServiceImpl implements TelegramService { final SendChatAction sendChatAction = new SendChatAction(); sendChatAction.setChatId(personId); sendChatAction.setAction(ActionType.valueOf(chatAction.name())); + try { absSender.execute(sendChatAction); } catch (TelegramApiRequestException e) { @@ -66,4 +81,35 @@ public class TelegramServiceImpl implements TelegramService { } } + @Override + public void addCommand(@NotNull Collection botCommands) { + final Map> commandMap = botCommands.stream() + .filter(command -> checkNotNull(command.getLang())) + .collect( + Collectors.groupingBy( + ClientBotCommand::getLang, + mapping(clientCommand -> BotCommand.builder().command(clientCommand.getKey()).description(clientCommand.getDescription()).build(), toList()) + ) + ); + + final List<@NotNull BotCommand> noLangCommands = botCommands.stream() + .filter(command -> checkNull(command.getLang())) + .map(clientCommand -> BotCommand.builder().command(clientCommand.getKey()).description(clientCommand.getDescription()).build()) + .toList(); + + try { + if (checkNotEmpty(noLangCommands)) { + absSender.execute(SetMyCommands.builder().commands(noLangCommands).build()); + } + + for (Map.Entry> entry : commandMap.entrySet()) { + absSender.execute(SetMyCommands.builder().languageCode(entry.getKey()).commands(entry.getValue()).build()); + } + } catch (TelegramApiRequestException e) { + log.error(e.getApiResponse()); + } catch (TelegramApiException e) { + log.error(e.getMessage()); + } + } + } diff --git a/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/ClientBotCommand.java b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/ClientBotCommand.java new file mode 100644 index 0000000..0430056 --- /dev/null +++ b/telegram-domain/telegram-domain-main/src/main/java/dev/struchkov/godfather/telegram/domain/ClientBotCommand.java @@ -0,0 +1,33 @@ +package dev.struchkov.godfather.telegram.domain; + +public class ClientBotCommand { + + private String key; + private String description; + private String lang; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getLang() { + return lang; + } + + public void setLang(String lang) { + this.lang = lang; + } + +}