diff --git a/pom.xml b/pom.xml index bda6bce..f881ef1 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.22 + 0.0.23 pom @@ -33,7 +33,7 @@ UTF-8 UTF-8 - 0.0.17 + 0.0.18 6.1.0 3.10.1 diff --git a/telegram-core/pom.xml b/telegram-core/pom.xml index 3ab9ad3..98c4f54 100644 --- a/telegram-core/pom.xml +++ b/telegram-core/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.22 + 0.0.23 telegram-core diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/MessageMailConvert.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/MessageMailConvert.java index 36b9e6d..6389403 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/MessageMailConvert.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/MessageMailConvert.java @@ -4,6 +4,7 @@ import dev.struchkov.godfather.context.domain.content.Mail; import dev.struchkov.godfather.context.domain.content.attachment.Attachment; import dev.struchkov.godfather.telegram.domain.attachment.ContactAttachment; import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment; import dev.struchkov.godfather.telegram.domain.attachment.Picture; import dev.struchkov.godfather.telegram.domain.attachment.PictureGroupAttachment; import org.telegram.telegrambots.meta.api.objects.Contact; @@ -15,11 +16,11 @@ import org.telegram.telegrambots.meta.api.objects.PhotoSize; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; +import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; import static dev.struchkov.haiti.utils.Exceptions.utilityClass; /** @@ -29,7 +30,7 @@ import static dev.struchkov.haiti.utils.Exceptions.utilityClass; */ public final class MessageMailConvert { - public MessageMailConvert() { + private MessageMailConvert() { utilityClass(); } @@ -111,16 +112,23 @@ public final class MessageMailConvert { } private static List convertAttachments(List entities) { - final List attachments = new ArrayList<>(); -// for (MessageEntity entity : entities) { -// String type = entity.getType(); -// if ("text_link".equals(type)) { -// Link link = new Link(); -// link.setUrl(entity.getUrl()); -// attachments.add(link); -// } -// } - return attachments; + if (checkNotEmpty(entities)) { + return entities.stream() + .map(MessageMailConvert::convertEntity) + .filter(Optional::isPresent) + .map(Optional::get) + .toList(); + } + return Collections.emptyList(); + } + + private static Optional convertEntity(MessageEntity entity) { + switch (entity.getType()) { + case "url" -> { + return Optional.of(new LinkAttachment(entity.getText())); + } + } + return Optional.empty(); } } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/LinkAttachment.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/LinkAttachment.java new file mode 100644 index 0000000..7545568 --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/LinkAttachment.java @@ -0,0 +1,32 @@ +package dev.struchkov.godfather.telegram.domain.attachment; + +import dev.struchkov.godfather.context.domain.content.attachment.Attachment; +import dev.struchkov.haiti.utils.Parser; +import dev.struchkov.haiti.utils.domain.CompositeUrl; + +public class LinkAttachment extends Attachment { + + private String url; + + public LinkAttachment(String url) { + this.url = url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getUrl() { + return url; + } + + @Override + public String getType() { + return TelegramAttachmentType.LINK.name(); + } + + public CompositeUrl split() { + return Parser.url(url); + } + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java index 71a4b66..39e1784 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/attachment/TelegramAttachmentType.java @@ -4,6 +4,7 @@ public enum TelegramAttachmentType { DOCUMENT, CONTACT, - PICTURE + PICTURE, + LINK } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/TelegramSender.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/TelegramSender.java index f1fc936..e1d5e74 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/TelegramSender.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/TelegramSender.java @@ -29,7 +29,7 @@ public class TelegramSender implements Sending { private static final String ERROR_REPLACE_MESSAGE = "Bad Request: message to edit not found"; private final AbsSender absSender; - private Map map = new HashMap<>(); + private final Map lastMessageId = new HashMap<>(); private SendPreProcessing sendPreProcessing; @@ -43,30 +43,26 @@ public class TelegramSender implements Sending { public void send(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) { isNotNull(telegramId, boxAnswer); - if (boxAnswer.getMessage() != null && !boxAnswer.getMessage().isBlank()) { - try { - if (boxAnswer.isReplace() && map.containsKey(telegramId)) { - replaceMessage(telegramId, boxAnswer); - } else { - sendMessage(telegramId, boxAnswer); - } - } catch (TelegramApiRequestException e) { - log.error(e.getApiResponse()); - if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) { - sendMessage(telegramId, boxAnswer); - } - } catch (TelegramApiException e) { - log.error(e.getMessage()); + try { + if (boxAnswer.isReplace() && lastMessageId.containsKey(telegramId)) { + replaceMessage(telegramId, boxAnswer); + } else { + sendMessage(telegramId, boxAnswer); } - } else { - log.warn("Сообщение не было отправлено, так как текст сообщения был пустым"); + } catch (TelegramApiRequestException e) { + log.error(e.getApiResponse()); + if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) { + sendMessage(telegramId, boxAnswer); + } + } catch (TelegramApiException e) { + log.error(e.getMessage()); } } private void replaceMessage(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) throws TelegramApiException { final EditMessageText editMessageText = new EditMessageText(); editMessageText.setChatId(String.valueOf(telegramId)); - editMessageText.setMessageId(map.get(telegramId)); + editMessageText.setMessageId(lastMessageId.get(telegramId)); editMessageText.enableMarkdown(true); editMessageText.setText(boxAnswer.getMessage()); editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard())); @@ -85,8 +81,7 @@ public class TelegramSender implements Sending { sendMessage.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard())); try { final Message execute = absSender.execute(sendMessage); - - map.put(telegramId, execute.getMessageId()); + lastMessageId.put(telegramId, execute.getMessageId()); } catch (TelegramApiRequestException e) { log.error(e.getApiResponse()); } catch (TelegramApiException e) { diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/Attachments.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/Attachments.java index 2431693..7bc161b 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/Attachments.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/utils/Attachments.java @@ -3,15 +3,19 @@ package dev.struchkov.godfather.telegram.utils; import dev.struchkov.godfather.context.domain.content.attachment.Attachment; import dev.struchkov.godfather.telegram.domain.attachment.ContactAttachment; import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; +import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment; import dev.struchkov.godfather.telegram.domain.attachment.Picture; import dev.struchkov.godfather.telegram.domain.attachment.PictureGroupAttachment; import dev.struchkov.godfather.telegram.domain.attachment.TelegramAttachmentType; -import dev.struchkov.haiti.utils.Inspector; import java.util.Collection; +import java.util.Collections; +import java.util.List; import java.util.Optional; +import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; import static dev.struchkov.haiti.utils.Exceptions.utilityClass; +import static dev.struchkov.haiti.utils.Inspector.isNotNull; public final class Attachments { @@ -19,11 +23,32 @@ public final class Attachments { utilityClass(); } + public static List findAllLinks(Collection attachments) { + if (checkNotEmpty(attachments)) { + return attachments.stream() + .filter(Attachments::isLink) + .map(LinkAttachment.class::cast) + .toList(); + } + return Collections.emptyList(); + } + + public static Optional findFirstLink(Collection attachments) { + if (checkNotEmpty(attachments)) { + for (Attachment attachment : attachments) { + if (isLink(attachment)) { + return Optional.of((LinkAttachment) attachment); + } + } + } + return Optional.empty(); + } + public static Optional findFirstPictureGroup(Collection attachments) { - if (attachments != null) { + if (checkNotEmpty(attachments)) { for (Attachment attachment : attachments) { if (isPictureGroup(attachment)) { - return Optional.ofNullable((PictureGroupAttachment) attachment); + return Optional.of((PictureGroupAttachment) attachment); } } } @@ -31,7 +56,7 @@ public final class Attachments { } public static Optional findFirstLargePicture(Collection attachments) { - if (attachments != null) { + if (checkNotEmpty(attachments)) { for (Attachment attachment : attachments) { if (isPictureGroup(attachment)) { final PictureGroupAttachment pictureGroup = (PictureGroupAttachment) attachment; @@ -43,10 +68,10 @@ public final class Attachments { } public static Optional findFirstDocument(Collection attachments) { - if (attachments != null) { + if (checkNotEmpty(attachments)) { for (Attachment attachment : attachments) { if (isDocument(attachment)) { - return Optional.ofNullable((DocumentAttachment) attachment); + return Optional.of((DocumentAttachment) attachment); } } } @@ -54,10 +79,10 @@ public final class Attachments { } public static Optional findFirstContact(Collection attachments) { - if (attachments != null) { + if (checkNotEmpty(attachments)) { for (Attachment attachment : attachments) { if (isContact(attachment)) { - return Optional.ofNullable((ContactAttachment) attachment); + return Optional.of((ContactAttachment) attachment); } } } @@ -65,7 +90,7 @@ public final class Attachments { } public static boolean hasDocument(Collection attachments) { - Inspector.isNotNull(attachments); + isNotNull(attachments); for (Attachment attachment : attachments) { if (isDocument(attachment)) { return true; @@ -75,18 +100,23 @@ public final class Attachments { } public static boolean isDocument(Attachment attachment) { - Inspector.isNotNull(attachment); + isNotNull(attachment); return TelegramAttachmentType.DOCUMENT.name().equals(attachment.getType()); } private static boolean isContact(Attachment attachment) { - Inspector.isNotNull(attachment); + isNotNull(attachment); return TelegramAttachmentType.CONTACT.name().equals(attachment.getType()); } private static boolean isPictureGroup(Attachment attachment) { - Inspector.isNotNull(attachment); + isNotNull(attachment); return TelegramAttachmentType.PICTURE.name().equals(attachment.getType()); } + private static boolean isLink(Attachment attachment) { + isNotNull(attachment); + return TelegramAttachmentType.LINK.name().equals(attachment.getType()); + } + }