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

This commit is contained in:
Struchkov Mark 2023-03-05 18:10:41 +03:00
parent ce16b35cd7
commit 4d41a2375d
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
5 changed files with 155 additions and 0 deletions

View File

@ -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<Void> executeAction(@NotNull String personId, ChatAction chatAction);
@ -12,4 +15,6 @@ public interface TelegramService {
Uni<Void> unPinMessage(@NotNull String personId, @NotNull String messageId);
Uni<Void> addCommand(@NotNull Collection<ClientBotCommand> botCommands);
}

View File

@ -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<ClientBotCommand> botCommands);
}

View File

@ -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<Void> addCommand(@NotNull Collection<ClientBotCommand> 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<BotCommand> noLangCommands = t.getItem2();
if (checkNotEmpty(noLangCommands)) {
return Uni.createFrom().completionStage(
getExecuteAsync(
SetMyCommands.builder().commands(noLangCommands).build()
)
);
}
return Uni.createFrom().voidItem();
})
.call(t -> {
final Map<String, List<BotCommand>> 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<Boolean> 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<Boolean> getExecuteAsync(UnpinChatMessage unpinChatMessage) {
try {
return absSender.executeAsync(unpinChatMessage);

View File

@ -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<ClientBotCommand> botCommands) {
final Map<String, List<BotCommand>> 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<String, List<BotCommand>> 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());
}
}
}

View File

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