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

This commit is contained in:
Struchkov Mark 2023-01-25 11:30:31 +03:00
parent d6054cbd89
commit 790264ee96
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
15 changed files with 106 additions and 89 deletions

View File

@ -37,7 +37,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.core.ver>0.0.50</godfather.core.ver>
<godfather.core.ver>0.0.51-SNAPSHOT</godfather.core.ver>
<telegrambots.ver>6.3.0</telegrambots.ver>
<haiti.version>2.0.0</haiti.version>

View File

@ -18,6 +18,7 @@ public class CallbackQueryConvert {
final String callbackData = callbackQuery.getData();
final Mail mail = new Mail();
mail.setId(callbackQuery.getMessage().getMessageId().toString());
mail.setCreateDate(LocalDateTime.now());
mail.setText(callbackData);
mail.addAttachment(convertToButtonClick(callbackData, callbackQuery.getMessage().getMessageId()));
@ -34,7 +35,7 @@ public class CallbackQueryConvert {
private static ButtonClickAttachment convertToButtonClick(String callbackData, Integer messageId) {
final ButtonClickAttachment buttonClickAttachment = new ButtonClickAttachment();
buttonClickAttachment.setRawCallBackData(callbackData);
buttonClickAttachment.setMessageId(messageId);
buttonClickAttachment.setMessageId(messageId.toString());
if (callbackData.charAt(0) == '[' && callbackData.charAt(callbackData.length() - 1) == ']') {
final String[] args = callbackData.substring(1, callbackData.length() - 1).split(";");
for (String arg : args) {

View File

@ -39,6 +39,7 @@ public final class MessageMailConvert {
public static Mail apply(Message message) {
final Mail mail = new Mail();
final Long chatId = message.getChatId();
mail.setId(message.getMessageId().toString());
mail.setPersonId(chatId != null ? chatId.toString() : null);
mail.setText(message.getText());
mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), ZoneId.systemDefault()));

View File

@ -45,8 +45,9 @@ public class EventDistributorService implements EventDistributor {
return Uni.createFrom().voidItem()
.onItem().transformToUni(
v -> {
if (checkNotNull(update.getMessage())) {
final Message message = update.getMessage();
final Message message = update.getMessage();
final CallbackQuery callbackQuery = update.getCallbackQuery();
if (checkNotNull(message)) {
if (!isEvent(message)) {
final Optional<List<EventHandler>> optHandlers = getHandler(Mail.TYPE);
if (optHandlers.isPresent()) {
@ -58,8 +59,7 @@ public class EventDistributorService implements EventDistributor {
return Uni.createFrom().voidItem();
}
}
if (checkNotNull(update.getCallbackQuery())) {
final CallbackQuery callbackQuery = update.getCallbackQuery();
if (checkNotNull(callbackQuery)) {
final Optional<List<EventHandler>> optHandlers = getHandler(Mail.TYPE);
if (optHandlers.isPresent()) {
return Multi.createFrom().iterable(optHandlers.get())
@ -100,15 +100,15 @@ public class EventDistributorService implements EventDistributor {
private boolean isEvent(Message message) {
return message.getChannelChatCreated() != null
|| message.getDeleteChatPhoto() != null
|| isNewChatMember(message.getNewChatMembers())
|| message.getNewChatTitle() != null
|| message.getNewChatPhoto() != null
|| message.getVideoChatEnded() != null
|| message.getVideoChatParticipantsInvited() != null
|| message.getVideoChatScheduled() != null
|| message.getVideoNote() != null
|| message.getVideoChatStarted() != null;
|| message.getDeleteChatPhoto() != null
|| isNewChatMember(message.getNewChatMembers())
|| message.getNewChatTitle() != null
|| message.getNewChatPhoto() != null
|| message.getVideoChatEnded() != null
|| message.getVideoChatParticipantsInvited() != null
|| message.getVideoChatScheduled() != null
|| message.getVideoNote() != null
|| message.getVideoChatStarted() != null;
}
private boolean isNewChatMember(List<User> newChatMembers) {

View File

@ -38,16 +38,16 @@ public class EventDistributorService implements EventDistributor {
@Override
public void processing(@NotNull Update update) {
if (update.getMessage() != null) {
final Message message = update.getMessage();
final Message message = update.getMessage();
final CallbackQuery callbackQuery = update.getCallbackQuery();
if (message != null) {
if (!isEvent(message)) {
getHandler(Mail.TYPE)
.ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.handle(MessageMailConvert.apply(message))));
return;
}
}
if (update.getCallbackQuery() != null) {
final CallbackQuery callbackQuery = update.getCallbackQuery();
if (callbackQuery != null) {
getHandler(Mail.TYPE)
.ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.handle(CallbackQueryConvert.apply(callbackQuery))));
return;
@ -69,15 +69,15 @@ public class EventDistributorService implements EventDistributor {
private boolean isEvent(Message message) {
return message.getChannelChatCreated() != null
|| message.getDeleteChatPhoto() != null
|| isNewChatMember(message.getNewChatMembers())
|| message.getNewChatTitle() != null
|| message.getNewChatPhoto() != null
|| message.getVideoChatEnded() != null
|| message.getVideoChatParticipantsInvited() != null
|| message.getVideoChatScheduled() != null
|| message.getVideoNote() != null
|| message.getVideoChatStarted() != null;
|| message.getDeleteChatPhoto() != null
|| isNewChatMember(message.getNewChatMembers())
|| message.getNewChatTitle() != null
|| message.getNewChatPhoto() != null
|| message.getVideoChatEnded() != null
|| message.getVideoChatParticipantsInvited() != null
|| message.getVideoChatScheduled() != null
|| message.getVideoNote() != null
|| message.getVideoChatStarted() != null;
}
private boolean isNewChatMember(List<User> newChatMembers) {

View File

@ -4,9 +4,9 @@ import io.smallrye.mutiny.Uni;
public interface SenderRepository {
Uni<Integer> getLastSendMessage(String telegramId);
Uni<String> getLastSendMessage(String telegramId);
Uni<Void> saveLastSendMessage(String telegramId, Integer messageId);
Uni<Void> saveLastSendMessage(String telegramId, String messageId);
Uni<Void> removeLastSendMessage(String telegramId);

View File

@ -6,8 +6,8 @@ import dev.struchkov.godfather.quarkus.context.service.SendingService;
import io.smallrye.mutiny.Uni;
import org.jetbrains.annotations.NotNull;
public interface TelegramSending extends SendingService<Integer> {
public interface TelegramSending extends SendingService {
Uni<SentBox<Integer>> sendNotSave(@NotNull BoxAnswer boxAnswer);
Uni<SentBox> sendNotSave(@NotNull BoxAnswer boxAnswer);
}

View File

@ -4,9 +4,9 @@ import java.util.Optional;
public interface SenderRepository {
Optional<Integer> getLastSendMessage(String telegramId);
Optional<String> getLastSendMessage(String telegramId);
void saveLastSendMessage(String telegramId, Integer messageId);
void saveLastSendMessage(String telegramId, String messageId);
void removeLastSendMessage(String telegramId);

View File

@ -7,8 +7,8 @@ import org.jetbrains.annotations.NotNull;
import java.util.Optional;
public interface TelegramSending extends SendingService<Integer> {
public interface TelegramSending extends SendingService {
Optional<SentBox<Integer>> sendNotSave(@NotNull BoxAnswer boxAnswer);
Optional<SentBox> sendNotSave(@NotNull BoxAnswer boxAnswer);
}

View File

@ -11,15 +11,15 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull;
public class SenderMapRepository implements SenderRepository {
private final Map<String, Integer> lastMessageId = new HashMap<>();
private final Map<String, String> lastMessageId = new HashMap<>();
@Override
public Uni<Integer> getLastSendMessage(String telegramId) {
public Uni<String> getLastSendMessage(String telegramId) {
return Uni.createFrom().item(lastMessageId.get(telegramId));
}
@Override
public Uni<Void> saveLastSendMessage(@NotNull String telegramId, @NotNull Integer messageId) {
public Uni<Void> saveLastSendMessage(@NotNull String telegramId, @NotNull String messageId) {
isNotNull(telegramId);
lastMessageId.put(telegramId, messageId);
return Uni.createFrom().voidItem();

View File

@ -11,15 +11,15 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull;
public class SenderMapRepository implements SenderRepository {
private final Map<String, Integer> lastMessageId = new HashMap<>();
private final Map<String, String> lastMessageId = new HashMap<>();
@Override
public Optional<Integer> getLastSendMessage(String telegramId) {
public Optional<String> getLastSendMessage(String telegramId) {
return Optional.ofNullable(lastMessageId.get(telegramId));
}
@Override
public void saveLastSendMessage(@NotNull String telegramId, @NotNull Integer messageId) {
public void saveLastSendMessage(@NotNull String telegramId, @NotNull String messageId) {
isNotNull(telegramId);
lastMessageId.put(telegramId, messageId);
}

View File

@ -27,4 +27,8 @@ public class TriggerChecks {
};
}
public static Predicate<Mail> isClickButton() {
return mail -> Attachments.findFirstButtonClick(mail.getAttachments()).isPresent();
}
}

View File

@ -15,7 +15,7 @@ public class ButtonClickAttachment extends Attachment {
/**
* Идентификатор сообщения, под которым пользователь нажал кнопку.
*/
private Integer messageId;
private String messageId;
private String rawCallBackData;
private final Map<String, Arg> args = new HashMap<>();
@ -46,11 +46,11 @@ public class ButtonClickAttachment extends Attachment {
return args.values();
}
public Integer getMessageId() {
public String getMessageId() {
return messageId;
}
public void setMessageId(Integer messageId) {
public void setMessageId(String messageId) {
this.messageId = messageId;
}
@ -64,6 +64,7 @@ public class ButtonClickAttachment extends Attachment {
}
public static class Arg {
private final String type;
private final String value;

View File

@ -51,7 +51,7 @@ public class TelegramSender implements TelegramSending {
}
@Override
public Uni<SentBox<Integer>> send(@NotNull BoxAnswer boxAnswer) {
public Uni<SentBox> send(@NotNull BoxAnswer boxAnswer) {
return sendBoxAnswer(boxAnswer, true);
}
@ -61,10 +61,10 @@ public class TelegramSender implements TelegramSending {
}
@Override
public Uni<Void> deleteMessage(@NotNull String personId, @NotNull Integer messageId) {
public Uni<Void> deleteMessage(@NotNull String personId, @NotNull String messageId) {
final DeleteMessage deleteMessage = new DeleteMessage();
deleteMessage.setChatId(personId);
deleteMessage.setMessageId(messageId);
deleteMessage.setMessageId(Integer.parseInt(messageId));
try {
absSender.execute(deleteMessage);
} catch (TelegramApiException e) {
@ -74,51 +74,57 @@ public class TelegramSender implements TelegramSending {
}
@Override
public Uni<SentBox<Integer>> replaceMessage(@NotNull String personId, @NotNull Integer messageId, @NotNull BoxAnswer newAnswer) {
public Uni<SentBox> replaceMessage(@NotNull String personId, @NotNull String messageId, @NotNull BoxAnswer newAnswer) {
return replace(personId, messageId, newAnswer, true);
}
@Override
public Uni<SentBox<Integer>> sendNotSave(@NotNull BoxAnswer boxAnswer) {
public Uni<SentBox> sendNotSave(@NotNull BoxAnswer boxAnswer) {
return sendBoxAnswer(boxAnswer, false);
}
private Uni<SentBox<Integer>> sendBoxAnswer(@NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
private Uni<SentBox> sendBoxAnswer(@NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
return Uni.createFrom().voidItem()
.onItem().transformToUni(
v -> {
final String recipientTelegramId = boxAnswer.getRecipientPersonId();
if (boxAnswer.isReplace() && checkNotNull(senderRepository)) {
return senderRepository.getLastSendMessage(recipientTelegramId)
.onItem().transformToUni(
lastId -> {
if (checkNotNull(lastId)) {
return replace(recipientTelegramId, lastId, boxAnswer, saveMessageId);
} else {
return sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
}
}
);
} else {
return sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
if (boxAnswer.isReplace()) {
final String replaceMessageId = boxAnswer.getReplaceMessageId();
if (checkNotNull(replaceMessageId)) {
return replace(recipientTelegramId, replaceMessageId, boxAnswer, saveMessageId);
} else {
if (checkNotNull(senderRepository)) {
return senderRepository.getLastSendMessage(recipientTelegramId)
.onItem().transformToUni(
lastId -> {
if (checkNotNull(lastId)) {
return replace(recipientTelegramId, lastId, boxAnswer, saveMessageId);
} else {
return sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
}
}
);
}
}
}
return sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
}
);
}
private Uni<SentBox<Integer>> replace(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
private Uni<SentBox> replace(@NotNull String telegramId, @NotNull String lastMessageId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
return Uni.createFrom().voidItem()
.onItem().transformToUni(
v -> {
final EditMessageText editMessageText = new EditMessageText();
editMessageText.setChatId(telegramId);
editMessageText.setMessageId(lastMessageId);
editMessageText.setMessageId(Integer.parseInt(lastMessageId));
editMessageText.enableMarkdown(true);
editMessageText.setText(boxAnswer.getMessage());
editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard()));
try {
absSender.execute(editMessageText);
return Uni.createFrom().optional(SentBox.optional(lastMessageId, boxAnswer, boxAnswer));
return Uni.createFrom().optional(SentBox.optional(telegramId, lastMessageId, boxAnswer, boxAnswer));
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) {
@ -132,7 +138,7 @@ public class TelegramSender implements TelegramSending {
);
}
private Uni<SentBox<Integer>> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
private Uni<SentBox> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
return Uni.createFrom().voidItem()
.onItem().transform(
v -> {
@ -153,12 +159,12 @@ public class TelegramSender implements TelegramSending {
}
).onItem().ifNotNull().call(answerMessage -> {
if (checkNotNull(senderRepository) && saveMessageId) {
return senderRepository.saveLastSendMessage(telegramId, answerMessage.getMessageId());
return senderRepository.saveLastSendMessage(telegramId, answerMessage.getMessageId().toString());
}
return null;
})
.onItem().ifNotNull().transformToUni(
answerMessage -> Uni.createFrom().optional(SentBox.optional(answerMessage.getMessageId(), boxAnswer, boxAnswer))
answerMessage -> Uni.createFrom().optional(SentBox.optional(telegramId, answerMessage.getMessageId().toString(), boxAnswer, boxAnswer))
);
}

View File

@ -57,10 +57,10 @@ public class TelegramSender implements TelegramSending {
}
@Override
public void deleteMessage(@NotNull String personId, @NotNull Integer messageId) {
public void deleteMessage(@NotNull String personId, @NotNull String messageId) {
final DeleteMessage deleteMessage = new DeleteMessage();
deleteMessage.setChatId(personId);
deleteMessage.setMessageId(messageId);
deleteMessage.setMessageId(Integer.parseInt(messageId));
try {
absSender.execute(deleteMessage);
} catch (TelegramApiException e) {
@ -69,22 +69,22 @@ public class TelegramSender implements TelegramSending {
}
@Override
public Optional<SentBox<Integer>> replaceMessage(@NotNull String personId, @NotNull Integer messageId, @NotNull BoxAnswer newAnswer) {
public Optional<SentBox> replaceMessage(@NotNull String personId, @NotNull String messageId, @NotNull BoxAnswer newAnswer) {
return replace(personId, messageId, newAnswer, newAnswer, true);
}
@Override
public Optional<SentBox<Integer>> send(@NotNull BoxAnswer boxAnswer) {
public Optional<SentBox> send(@NotNull BoxAnswer boxAnswer) {
isNotNull(boxAnswer.getRecipientPersonId());
return sendBoxAnswer(boxAnswer, true);
}
@Override
public Optional<SentBox<Integer>> sendNotSave(@NotNull BoxAnswer boxAnswer) {
public Optional<SentBox> sendNotSave(@NotNull BoxAnswer boxAnswer) {
return sendBoxAnswer(boxAnswer, false);
}
private Optional<SentBox<Integer>> sendBoxAnswer(BoxAnswer boxAnswer, boolean saveMessageId) {
private Optional<SentBox> sendBoxAnswer(BoxAnswer boxAnswer, boolean saveMessageId) {
final String recipientTelegramId = boxAnswer.getRecipientPersonId();
isNotNull(recipientTelegramId);
@ -93,28 +93,32 @@ public class TelegramSender implements TelegramSending {
preparedAnswer = preSendProcessor.pretreatment(boxAnswer);
}
if (preparedAnswer.isReplace() && checkNotNull(senderRepository)) {
final Optional<Integer> optLastId = senderRepository.getLastSendMessage(recipientTelegramId);
if (optLastId.isPresent()) {
return replace(recipientTelegramId, optLastId.get(), boxAnswer, preparedAnswer, saveMessageId);
if (preparedAnswer.isReplace()) {
final String replaceMessageId = preparedAnswer.getReplaceMessageId();
if (checkNotNull(replaceMessageId)) {
return replace(recipientTelegramId, replaceMessageId, boxAnswer, preparedAnswer, saveMessageId);
} else {
return sendMessage(recipientTelegramId, boxAnswer, preparedAnswer, saveMessageId);
if (checkNotNull(senderRepository)) {
final Optional<String> optLastId = senderRepository.getLastSendMessage(recipientTelegramId);
if (optLastId.isPresent()) {
return replace(recipientTelegramId, optLastId.get(), boxAnswer, preparedAnswer, saveMessageId);
}
}
}
} else {
return sendMessage(recipientTelegramId, boxAnswer, preparedAnswer, saveMessageId);
}
return sendMessage(recipientTelegramId, boxAnswer, preparedAnswer, saveMessageId);
}
private Optional<SentBox<Integer>> replace(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer, BoxAnswer preparedAnswer, boolean saveMessageId) {
private Optional<SentBox> replace(@NotNull String telegramId, @NotNull String replaceMessageId, @NotNull BoxAnswer boxAnswer, BoxAnswer preparedAnswer, boolean saveMessageId) {
final EditMessageText editMessageText = new EditMessageText();
editMessageText.setChatId(telegramId);
editMessageText.setMessageId(lastMessageId);
editMessageText.setMessageId(Integer.parseInt(replaceMessageId));
editMessageText.enableMarkdown(true);
editMessageText.setText(boxAnswer.getMessage());
editMessageText.setReplyMarkup(KeyBoardConvert.convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard()));
try {
absSender.execute(editMessageText);
return SentBox.optional(lastMessageId, preparedAnswer, boxAnswer);
return SentBox.optional(telegramId, replaceMessageId, preparedAnswer, boxAnswer);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) {
@ -126,7 +130,7 @@ public class TelegramSender implements TelegramSending {
return Optional.empty();
}
private Optional<SentBox<Integer>> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, BoxAnswer preparedAnswer, boolean saveMessageId) {
private Optional<SentBox> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, BoxAnswer preparedAnswer, boolean saveMessageId) {
final SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true);
sendMessage.setChatId(telegramId);
@ -135,9 +139,9 @@ public class TelegramSender implements TelegramSending {
try {
final Message execute = absSender.execute(sendMessage);
if (checkNotNull(senderRepository) && saveMessageId) {
senderRepository.saveLastSendMessage(telegramId, execute.getMessageId());
senderRepository.saveLastSendMessage(telegramId, execute.getMessageId().toString());
}
return SentBox.optional(execute.getMessageId(), preparedAnswer, boxAnswer);
return SentBox.optional(telegramId, execute.getMessageId().toString(), preparedAnswer, boxAnswer);
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
} catch (TelegramApiException e) {