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

This commit is contained in:
Struchkov Mark 2023-02-25 11:27:20 +03:00
parent 13f4d8c368
commit 97122552e8
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
5 changed files with 83 additions and 0 deletions

View File

View File

@ -8,4 +8,8 @@ public interface TelegramService {
Uni<Void> executeAction(@NotNull String personId, ChatAction chatAction);
Uni<Void> pinMessage(@NotNull String personId, @NotNull String messageId);
Uni<Void> unPinMessage(@NotNull String personId, @NotNull String messageId);
}

View File

@ -7,4 +7,8 @@ public interface TelegramService {
void executeAction(@NotNull String personId, ChatAction chatAction);
void pinMessage(@NotNull String personId, @NotNull String messageId);
void unPinMessage(@NotNull String personId, @NotNull String messageId);
}

View File

@ -8,12 +8,15 @@ 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.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.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
public class TelegramServiceImpl implements TelegramService {
@ -35,6 +38,48 @@ public class TelegramServiceImpl implements TelegramService {
.replaceWithVoid();
}
@Override
public Uni<Void> pinMessage(@NotNull String personId, @NotNull String messageId) {
final PinChatMessage pinChatMessage = new PinChatMessage();
pinChatMessage.setChatId(personId);
pinChatMessage.setMessageId(Integer.parseInt(messageId));
return Uni.createFrom().completionStage(getExecuteAsync(pinChatMessage))
.replaceWithVoid();
}
@Override
public Uni<Void> unPinMessage(@NotNull String personId, @NotNull String messageId) {
final UnpinChatMessage unpinChatMessage = new UnpinChatMessage();
unpinChatMessage.setChatId(personId);
unpinChatMessage.setMessageId(Integer.parseInt(messageId));
return Uni.createFrom().completionStage(getExecuteAsync(unpinChatMessage))
.replaceWithVoid();
}
private CompletableFuture<Boolean> getExecuteAsync(UnpinChatMessage unpinChatMessage) {
try {
return absSender.executeAsync(unpinChatMessage);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
return CompletableFuture.completedFuture(null);
}
private CompletionStage<Boolean> getExecuteAsync(PinChatMessage pinChatMessage) {
try {
return absSender.executeAsync(pinChatMessage);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
return CompletableFuture.completedFuture(null);
}
private CompletableFuture<Boolean> getExecuteAsync(SendChatAction sendChatAction) {
try {
return absSender.executeAsync(sendChatAction);

View File

@ -7,6 +7,8 @@ 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.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.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
@ -36,4 +38,32 @@ public class TelegramServiceImpl implements TelegramService {
}
}
@Override
public void pinMessage(@NotNull String personId, @NotNull String messageId) {
final PinChatMessage pinChatMessage = new PinChatMessage();
pinChatMessage.setChatId(personId);
pinChatMessage.setMessageId(Integer.parseInt(messageId));
try {
absSender.execute(pinChatMessage);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
}
@Override
public void unPinMessage(@NotNull String personId, @NotNull String messageId) {
final UnpinChatMessage unpinChatMessage = new UnpinChatMessage();
unpinChatMessage.setChatId(personId);
unpinChatMessage.setMessageId(Integer.parseInt(messageId));
try {
absSender.execute(unpinChatMessage);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
}
}