Добавил новый тип вложения - ссылка
This commit is contained in:
parent
83b9de8b8b
commit
54e1fad49b
4
pom.xml
4
pom.xml
@ -5,7 +5,7 @@
|
||||
|
||||
<groupId>dev.struchkov.godfather</groupId>
|
||||
<artifactId>telegram-bot</artifactId>
|
||||
<version>0.0.22</version>
|
||||
<version>0.0.23</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
@ -33,7 +33,7 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<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>
|
||||
|
||||
<plugin.maven.compiler.ver>3.10.1</plugin.maven.compiler.ver>
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>dev.struchkov.godfather</groupId>
|
||||
<artifactId>telegram-bot</artifactId>
|
||||
<version>0.0.22</version>
|
||||
<version>0.0.23</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>telegram-core</artifactId>
|
||||
|
@ -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<Attachment> convertAttachments(List<MessageEntity> entities) {
|
||||
final List<Attachment> 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<Attachment> convertEntity(MessageEntity entity) {
|
||||
switch (entity.getType()) {
|
||||
case "url" -> {
|
||||
return Optional.of(new LinkAttachment(entity.getText()));
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -4,6 +4,7 @@ public enum TelegramAttachmentType {
|
||||
|
||||
DOCUMENT,
|
||||
CONTACT,
|
||||
PICTURE
|
||||
PICTURE,
|
||||
LINK
|
||||
|
||||
}
|
||||
|
@ -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<Long, Integer> map = new HashMap<>();
|
||||
private final Map<Long, Integer> 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) {
|
||||
|
@ -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<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) {
|
||||
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<Picture> findFirstLargePicture(Collection<Attachment> 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<DocumentAttachment> findFirstDocument(Collection<Attachment> 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<ContactAttachment> findFirstContact(Collection<Attachment> 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<Attachment> 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());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user