Добавил новый тип вложения - ссылка

This commit is contained in:
Struchkov Mark 2022-07-26 09:31:39 +03:00
parent 83b9de8b8b
commit 54e1fad49b
7 changed files with 114 additions and 48 deletions

View File

@ -5,7 +5,7 @@
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.22</version> <version>0.0.23</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
@ -33,7 +33,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.core.ver>0.0.17</godfather.core.ver> <godfather.core.ver>0.0.18</godfather.core.ver>
<telegrambots.ver>6.1.0</telegrambots.ver> <telegrambots.ver>6.1.0</telegrambots.ver>
<plugin.maven.compiler.ver>3.10.1</plugin.maven.compiler.ver> <plugin.maven.compiler.ver>3.10.1</plugin.maven.compiler.ver>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.22</version> <version>0.0.23</version>
</parent> </parent>
<artifactId>telegram-core</artifactId> <artifactId>telegram-core</artifactId>

View File

@ -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.context.domain.content.attachment.Attachment;
import dev.struchkov.godfather.telegram.domain.attachment.ContactAttachment; import dev.struchkov.godfather.telegram.domain.attachment.ContactAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; 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.Picture;
import dev.struchkov.godfather.telegram.domain.attachment.PictureGroupAttachment; import dev.struchkov.godfather.telegram.domain.attachment.PictureGroupAttachment;
import org.telegram.telegrambots.meta.api.objects.Contact; 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.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional; 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.Exceptions.utilityClass;
/** /**
@ -29,7 +30,7 @@ import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
*/ */
public final class MessageMailConvert { public final class MessageMailConvert {
public MessageMailConvert() { private MessageMailConvert() {
utilityClass(); utilityClass();
} }
@ -111,16 +112,23 @@ public final class MessageMailConvert {
} }
private static List<Attachment> convertAttachments(List<MessageEntity> entities) { private static List<Attachment> convertAttachments(List<MessageEntity> entities) {
final List<Attachment> attachments = new ArrayList<>(); if (checkNotEmpty(entities)) {
// for (MessageEntity entity : entities) { return entities.stream()
// String type = entity.getType(); .map(MessageMailConvert::convertEntity)
// if ("text_link".equals(type)) { .filter(Optional::isPresent)
// Link link = new Link(); .map(Optional::get)
// link.setUrl(entity.getUrl()); .toList();
// attachments.add(link); }
// } return Collections.emptyList();
// } }
return attachments;
private static Optional<Attachment> convertEntity(MessageEntity entity) {
switch (entity.getType()) {
case "url" -> {
return Optional.of(new LinkAttachment(entity.getText()));
}
}
return Optional.empty();
} }
} }

View File

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

View File

@ -4,6 +4,7 @@ public enum TelegramAttachmentType {
DOCUMENT, DOCUMENT,
CONTACT, CONTACT,
PICTURE PICTURE,
LINK
} }

View File

@ -29,7 +29,7 @@ public class TelegramSender implements Sending {
private static final String ERROR_REPLACE_MESSAGE = "Bad Request: message to edit not found"; private static final String ERROR_REPLACE_MESSAGE = "Bad Request: message to edit not found";
private final AbsSender absSender; private final AbsSender absSender;
private Map<Long, Integer> map = new HashMap<>(); private final Map<Long, Integer> lastMessageId = new HashMap<>();
private SendPreProcessing sendPreProcessing; private SendPreProcessing sendPreProcessing;
@ -43,30 +43,26 @@ public class TelegramSender implements Sending {
public void send(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) { public void send(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) {
isNotNull(telegramId, boxAnswer); isNotNull(telegramId, boxAnswer);
if (boxAnswer.getMessage() != null && !boxAnswer.getMessage().isBlank()) { try {
try { if (boxAnswer.isReplace() && lastMessageId.containsKey(telegramId)) {
if (boxAnswer.isReplace() && map.containsKey(telegramId)) { replaceMessage(telegramId, boxAnswer);
replaceMessage(telegramId, boxAnswer); } else {
} else { sendMessage(telegramId, boxAnswer);
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());
} }
} else { } catch (TelegramApiRequestException e) {
log.warn("Сообщение не было отправлено, так как текст сообщения был пустым"); 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 { private void replaceMessage(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) throws TelegramApiException {
final EditMessageText editMessageText = new EditMessageText(); final EditMessageText editMessageText = new EditMessageText();
editMessageText.setChatId(String.valueOf(telegramId)); editMessageText.setChatId(String.valueOf(telegramId));
editMessageText.setMessageId(map.get(telegramId)); editMessageText.setMessageId(lastMessageId.get(telegramId));
editMessageText.enableMarkdown(true); editMessageText.enableMarkdown(true);
editMessageText.setText(boxAnswer.getMessage()); editMessageText.setText(boxAnswer.getMessage());
editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard())); editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard()));
@ -85,8 +81,7 @@ public class TelegramSender implements Sending {
sendMessage.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard())); sendMessage.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard()));
try { try {
final Message execute = absSender.execute(sendMessage); final Message execute = absSender.execute(sendMessage);
lastMessageId.put(telegramId, execute.getMessageId());
map.put(telegramId, execute.getMessageId());
} catch (TelegramApiRequestException e) { } catch (TelegramApiRequestException e) {
log.error(e.getApiResponse()); log.error(e.getApiResponse());
} catch (TelegramApiException e) { } catch (TelegramApiException e) {

View File

@ -3,15 +3,19 @@ package dev.struchkov.godfather.telegram.utils;
import dev.struchkov.godfather.context.domain.content.attachment.Attachment; import dev.struchkov.godfather.context.domain.content.attachment.Attachment;
import dev.struchkov.godfather.telegram.domain.attachment.ContactAttachment; import dev.struchkov.godfather.telegram.domain.attachment.ContactAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment; 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.Picture;
import dev.struchkov.godfather.telegram.domain.attachment.PictureGroupAttachment; import dev.struchkov.godfather.telegram.domain.attachment.PictureGroupAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.TelegramAttachmentType; import dev.struchkov.godfather.telegram.domain.attachment.TelegramAttachmentType;
import dev.struchkov.haiti.utils.Inspector;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional; 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.Exceptions.utilityClass;
import static dev.struchkov.haiti.utils.Inspector.isNotNull;
public final class Attachments { public final class Attachments {
@ -19,11 +23,32 @@ public final class Attachments {
utilityClass(); utilityClass();
} }
public static List<LinkAttachment> findAllLinks(Collection<Attachment> attachments) {
if (checkNotEmpty(attachments)) {
return attachments.stream()
.filter(Attachments::isLink)
.map(LinkAttachment.class::cast)
.toList();
}
return Collections.emptyList();
}
public static Optional<LinkAttachment> findFirstLink(Collection<Attachment> attachments) {
if (checkNotEmpty(attachments)) {
for (Attachment attachment : attachments) {
if (isLink(attachment)) {
return Optional.of((LinkAttachment) attachment);
}
}
}
return Optional.empty();
}
public static Optional<PictureGroupAttachment> findFirstPictureGroup(Collection<Attachment> attachments) { public static Optional<PictureGroupAttachment> findFirstPictureGroup(Collection<Attachment> attachments) {
if (attachments != null) { if (checkNotEmpty(attachments)) {
for (Attachment attachment : attachments) { for (Attachment attachment : attachments) {
if (isPictureGroup(attachment)) { if (isPictureGroup(attachment)) {
return Optional.ofNullable((PictureGroupAttachment) attachment); return Optional.of((PictureGroupAttachment) attachment);
} }
} }
} }
@ -31,7 +56,7 @@ public final class Attachments {
} }
public static Optional<Picture> findFirstLargePicture(Collection<Attachment> attachments) { public static Optional<Picture> findFirstLargePicture(Collection<Attachment> attachments) {
if (attachments != null) { if (checkNotEmpty(attachments)) {
for (Attachment attachment : attachments) { for (Attachment attachment : attachments) {
if (isPictureGroup(attachment)) { if (isPictureGroup(attachment)) {
final PictureGroupAttachment pictureGroup = (PictureGroupAttachment) attachment; final PictureGroupAttachment pictureGroup = (PictureGroupAttachment) attachment;
@ -43,10 +68,10 @@ public final class Attachments {
} }
public static Optional<DocumentAttachment> findFirstDocument(Collection<Attachment> attachments) { public static Optional<DocumentAttachment> findFirstDocument(Collection<Attachment> attachments) {
if (attachments != null) { if (checkNotEmpty(attachments)) {
for (Attachment attachment : attachments) { for (Attachment attachment : attachments) {
if (isDocument(attachment)) { if (isDocument(attachment)) {
return Optional.ofNullable((DocumentAttachment) attachment); return Optional.of((DocumentAttachment) attachment);
} }
} }
} }
@ -54,10 +79,10 @@ public final class Attachments {
} }
public static Optional<ContactAttachment> findFirstContact(Collection<Attachment> attachments) { public static Optional<ContactAttachment> findFirstContact(Collection<Attachment> attachments) {
if (attachments != null) { if (checkNotEmpty(attachments)) {
for (Attachment attachment : attachments) { for (Attachment attachment : attachments) {
if (isContact(attachment)) { 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<Attachment> attachments) { public static boolean hasDocument(Collection<Attachment> attachments) {
Inspector.isNotNull(attachments); isNotNull(attachments);
for (Attachment attachment : attachments) { for (Attachment attachment : attachments) {
if (isDocument(attachment)) { if (isDocument(attachment)) {
return true; return true;
@ -75,18 +100,23 @@ public final class Attachments {
} }
public static boolean isDocument(Attachment attachment) { public static boolean isDocument(Attachment attachment) {
Inspector.isNotNull(attachment); isNotNull(attachment);
return TelegramAttachmentType.DOCUMENT.name().equals(attachment.getType()); return TelegramAttachmentType.DOCUMENT.name().equals(attachment.getType());
} }
private static boolean isContact(Attachment attachment) { private static boolean isContact(Attachment attachment) {
Inspector.isNotNull(attachment); isNotNull(attachment);
return TelegramAttachmentType.CONTACT.name().equals(attachment.getType()); return TelegramAttachmentType.CONTACT.name().equals(attachment.getType());
} }
private static boolean isPictureGroup(Attachment attachment) { private static boolean isPictureGroup(Attachment attachment) {
Inspector.isNotNull(attachment); isNotNull(attachment);
return TelegramAttachmentType.PICTURE.name().equals(attachment.getType()); return TelegramAttachmentType.PICTURE.name().equals(attachment.getType());
} }
private static boolean isLink(Attachment attachment) {
isNotNull(attachment);
return TelegramAttachmentType.LINK.name().equals(attachment.getType());
}
} }