Разделение на модули

This commit is contained in:
Struchkov Mark 2022-08-02 21:56:19 +03:00
parent 98bed43267
commit 95a8084cae
24 changed files with 416 additions and 203 deletions

28
pom.xml
View File

@ -5,7 +5,7 @@
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.26</version> <version>0.0.30</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
@ -38,10 +38,10 @@
<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.22</godfather.core.ver> <godfather.core.ver>0.0.24</godfather.core.ver>
<telegrambots.ver>6.1.0</telegrambots.ver> <telegrambots.ver>6.1.0</telegrambots.ver>
<haiti.version>1.3.0</haiti.version> <haiti.version>1.4.0</haiti.version>
<plugin.maven.compiler.ver>3.10.1</plugin.maven.compiler.ver> <plugin.maven.compiler.ver>3.10.1</plugin.maven.compiler.ver>
<plugin.nexus.staging.ver>1.6.13</plugin.nexus.staging.ver> <plugin.nexus.staging.ver>1.6.13</plugin.nexus.staging.ver>
@ -68,6 +68,28 @@
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-consumer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-sender-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-quarkus-reactive-sender</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-simple-sender</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>bot-context</artifactId> <artifactId>bot-context</artifactId>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<version>0.0.26</version> <version>0.0.30</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,18 +5,15 @@ import dev.struchkov.godfather.context.service.EventHandler;
import dev.struchkov.godfather.telegram.TelegramConnectBot; import dev.struchkov.godfather.telegram.TelegramConnectBot;
import dev.struchkov.godfather.telegram.context.EventDistributor; import dev.struchkov.godfather.telegram.context.EventDistributor;
import dev.struchkov.godfather.telegram.convert.CallbackQueryConvert; import dev.struchkov.godfather.telegram.convert.CallbackQueryConvert;
import dev.struchkov.godfather.telegram.convert.CommandConvert;
import dev.struchkov.godfather.telegram.convert.MessageMailConvert; import dev.struchkov.godfather.telegram.convert.MessageMailConvert;
import dev.struchkov.godfather.telegram.convert.SubscribeConvert; import dev.struchkov.godfather.telegram.convert.SubscribeConvert;
import dev.struchkov.godfather.telegram.convert.UnsubscribeConvert; import dev.struchkov.godfather.telegram.convert.UnsubscribeConvert;
import dev.struchkov.godfather.telegram.domain.event.Command;
import dev.struchkov.godfather.telegram.domain.event.Subscribe; import dev.struchkov.godfather.telegram.domain.event.Subscribe;
import dev.struchkov.godfather.telegram.domain.event.Unsubscribe; import dev.struchkov.godfather.telegram.domain.event.Unsubscribe;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery; import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated; import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.MessageEntity;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User; import org.telegram.telegrambots.meta.api.objects.User;
@ -25,8 +22,6 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static dev.struchkov.haiti.utils.Checker.checkNotEmpty;
/** /**
* TODO: Добавить описание класса. * TODO: Добавить описание класса.
* *
@ -46,17 +41,11 @@ public class EventDistributorService implements EventDistributor {
if (update.getMessage() != null) { if (update.getMessage() != null) {
final Message message = update.getMessage(); final Message message = update.getMessage();
if (!isEvent(message)) { if (!isEvent(message)) {
if (isCommand(message)) {
getHandler(Command.TYPE)
.ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.handle(CommandConvert.apply(message))));
return;
} else {
getHandler(Mail.TYPE) getHandler(Mail.TYPE)
.ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.handle(MessageMailConvert.apply(message)))); .ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.handle(MessageMailConvert.apply(message))));
return; return;
} }
} }
}
if (update.getCallbackQuery() != null) { if (update.getCallbackQuery() != null) {
final CallbackQuery callbackQuery = update.getCallbackQuery(); final CallbackQuery callbackQuery = update.getCallbackQuery();
getHandler(Mail.TYPE) getHandler(Mail.TYPE)
@ -99,14 +88,6 @@ public class EventDistributorService implements EventDistributor {
} }
} }
private boolean isCommand(Message message) {
final List<MessageEntity> entities = message.getEntities();
if (checkNotEmpty(entities)) {
return "bot_command".equals(entities.get(0).getType());
}
return false;
}
private Optional<List<EventHandler>> getHandler(String type) { private Optional<List<EventHandler>> getHandler(String type) {
return Optional.ofNullable(eventProviderMap.get(type)); return Optional.ofNullable(eventProviderMap.get(type));
} }

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<version>0.0.26</version> <version>0.0.30</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

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

View File

@ -1,50 +0,0 @@
package dev.struchkov.godfather.telegram.convert;
import dev.struchkov.godfather.telegram.domain.event.Command;
import dev.struchkov.haiti.utils.Strings;
import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.MessageEntity;
import java.util.List;
import static dev.struchkov.haiti.context.exception.ConvertException.convertException;
import static dev.struchkov.haiti.utils.Checker.checkNotEmpty;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
import static dev.struchkov.haiti.utils.Inspector.isNotEmpty;
import static dev.struchkov.haiti.utils.Inspector.isTrue;
public class CommandConvert {
private CommandConvert() {
utilityClass();
}
public static Command apply(Message message) {
final List<MessageEntity> entities = message.getEntities();
isNotEmpty(entities, convertException("Ошибка преобразования сообщения в команду. В сообщении не обнаружена команда."));
final MessageEntity messageEntity = entities.get(0);
isTrue("bot_command".equals(messageEntity.getType()), convertException("Ошибка преобразования сообщения в команду. В сообщении не обнаружена команда."));
final String commandValue = messageEntity.getText();
String commandArg = message.getText().replace(commandValue, "");
if (checkNotEmpty(commandArg)) {
commandArg = commandArg.substring(1);
}
final Command command = new Command();
command.setValue(commandValue);
command.setCommandType(commandValue.replace("/", ""));
command.setArg(Strings.EMPTY.equals(commandArg) ? null : commandArg);
command.setRawValue(message.getText());
final Chat chat = message.getChat();
command.setFirstName(chat.getFirstName());
command.setLastName(chat.getLastName());
command.setPersonId(chat.getId());
return command;
}
}

View File

@ -2,11 +2,13 @@ package dev.struchkov.godfather.telegram.convert;
import dev.struchkov.godfather.context.domain.content.Mail; 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.CommandAttachment;
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.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.haiti.utils.Strings;
import org.telegram.telegrambots.meta.api.objects.Contact; import org.telegram.telegrambots.meta.api.objects.Contact;
import org.telegram.telegrambots.meta.api.objects.Document; import org.telegram.telegrambots.meta.api.objects.Document;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
@ -49,7 +51,7 @@ public final class MessageMailConvert {
final List<MessageEntity> entities = message.getEntities(); final List<MessageEntity> entities = message.getEntities();
if (entities != null) { if (entities != null) {
mail.addAttachments(convertAttachments(entities)); mail.addAttachments(convertAttachments(message));
} }
if (message.getReplyToMessage() != null) { if (message.getReplyToMessage() != null) {
@ -111,10 +113,11 @@ public final class MessageMailConvert {
return Optional.empty(); return Optional.empty();
} }
private static List<Attachment> convertAttachments(List<MessageEntity> entities) { private static List<Attachment> convertAttachments(Message message) {
final List<MessageEntity> entities = message.getEntities();
if (checkNotEmpty(entities)) { if (checkNotEmpty(entities)) {
return entities.stream() return entities.stream()
.map(MessageMailConvert::convertEntity) .map(entity -> convertEntity(message, entity))
.filter(Optional::isPresent) .filter(Optional::isPresent)
.map(Optional::get) .map(Optional::get)
.toList(); .toList();
@ -122,11 +125,24 @@ public final class MessageMailConvert {
return Collections.emptyList(); return Collections.emptyList();
} }
private static Optional<Attachment> convertEntity(MessageEntity entity) { private static Optional<Attachment> convertEntity(Message message, MessageEntity entity) {
switch (entity.getType()) { switch (entity.getType()) {
case "url" -> { case "url" -> {
return Optional.of(new LinkAttachment(entity.getText())); return Optional.of(new LinkAttachment(entity.getText()));
} }
case "bot_command" -> {
final String commandValue = entity.getText();
String commandArg = message.getText().replace(commandValue, "");
if (checkNotEmpty(commandArg)) {
commandArg = commandArg.substring(1);
}
final CommandAttachment commandAttachment = new CommandAttachment();
commandAttachment.setValue(commandValue);
commandAttachment.setCommandType(commandValue.replace("/", ""));
commandAttachment.setArg(Strings.EMPTY.equals(commandArg) ? null : commandArg);
commandAttachment.setRawValue(message.getText());
return Optional.of(commandAttachment);
}
} }
return Optional.empty(); return Optional.empty();
} }

View File

@ -1,6 +1,7 @@
package dev.struchkov.godfather.telegram.utils; 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.CommandAttachment;
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.LinkAttachment;
@ -89,6 +90,17 @@ public final class Attachments {
return Optional.empty(); return Optional.empty();
} }
public static Optional<CommandAttachment> findFirstCommand(Collection<Attachment> attachments) {
if (checkNotEmpty(attachments)) {
for (Attachment attachment : attachments) {
if (isCommand(attachment)) {
return Optional.of((CommandAttachment) attachment);
}
}
}
return Optional.empty();
}
public static boolean hasDocument(Collection<Attachment> attachments) { public static boolean hasDocument(Collection<Attachment> attachments) {
isNotNull(attachments); isNotNull(attachments);
for (Attachment attachment : attachments) { for (Attachment attachment : attachments) {
@ -99,6 +111,11 @@ public final class Attachments {
return false; return false;
} }
public static boolean isCommand(Attachment attachment) {
isNotNull(attachment);
return TelegramAttachmentType.COMMAND.name().equals(attachment.getType());
}
public static boolean isDocument(Attachment attachment) { public static boolean isDocument(Attachment attachment) {
isNotNull(attachment); isNotNull(attachment);
return TelegramAttachmentType.DOCUMENT.name().equals(attachment.getType()); return TelegramAttachmentType.DOCUMENT.name().equals(attachment.getType());

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<version>0.0.26</version> <version>0.0.30</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -0,0 +1,50 @@
package dev.struchkov.godfather.telegram.domain.attachment;
import dev.struchkov.godfather.context.domain.content.attachment.Attachment;
import java.util.Optional;
public class CommandAttachment extends Attachment {
private String value;
private String commandType;
private String arg;
private String rawValue;
public void setValue(String value) {
this.value = value;
}
public void setCommandType(String commandType) {
this.commandType = commandType;
}
public void setArg(String arg) {
this.arg = arg;
}
public void setRawValue(String rawValue) {
this.rawValue = rawValue;
}
public String getValue() {
return value;
}
public Optional<String> getArg() {
return Optional.ofNullable(arg);
}
public String getCommandType() {
return commandType;
}
public String getRawValue() {
return rawValue;
}
@Override
public String getType() {
return TelegramAttachmentType.COMMAND.name();
}
}

View File

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

View File

@ -1,80 +0,0 @@
package dev.struchkov.godfather.telegram.domain.event;
import dev.struchkov.godfather.context.domain.event.Event;
import java.util.Optional;
public class Command implements Event {
public static final String TYPE = "CMD";
private String value;
private String commandType;
private String arg;
private String rawValue;
private String firstName;
private String lastName;
private Long personId;
public void setValue(String value) {
this.value = value;
}
public void setCommandType(String commandType) {
this.commandType = commandType;
}
public void setArg(String arg) {
this.arg = arg;
}
public void setRawValue(String rawValue) {
this.rawValue = rawValue;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setPersonId(Long personId) {
this.personId = personId;
}
public String getValue() {
return value;
}
public Optional<String> getArg() {
return Optional.ofNullable(arg);
}
public String getCommandType() {
return commandType;
}
public String getRawValue() {
return rawValue;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Long getPersonId() {
return personId;
}
@Override
public String getEventType() {
return TYPE;
}
}

View File

@ -1,5 +1,6 @@
package dev.struchkov.godfather.telegram.domain.keyboard; package dev.struchkov.godfather.telegram.domain.keyboard;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoardButton;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoardLine; import dev.struchkov.godfather.context.domain.keyboard.KeyBoardLine;
import dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoard; import dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoard;
@ -7,6 +8,8 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoardLine.simpleLine;
public class MarkupKeyBoard extends SimpleKeyBoard { public class MarkupKeyBoard extends SimpleKeyBoard {
public static final String TYPE = "MARKUP"; public static final String TYPE = "MARKUP";
@ -34,6 +37,22 @@ public class MarkupKeyBoard extends SimpleKeyBoard {
inputFieldPlaceholder = builder.inputFieldPlaceholder; inputFieldPlaceholder = builder.inputFieldPlaceholder;
} }
public static MarkupKeyBoard markupKeyBoard(KeyBoardLine... lines) {
final Builder builder = new Builder();
for (KeyBoardLine line : lines) {
builder.line(line);
}
return builder.build();
}
public static MarkupKeyBoard markupKeyBoard(KeyBoardButton... buttons) {
final Builder builder = new Builder();
for (KeyBoardButton button : buttons) {
builder.line(simpleLine(button));
}
return builder.build();
}
public static Builder markupBuilder() { public static Builder markupBuilder() {
return new Builder(); return new Builder();
} }

View File

@ -5,41 +5,21 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.26</version> <version>0.0.30</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>telegram-sender</artifactId> <artifactId>telegram-sender</artifactId>
<packaging>pom</packaging>
<modules>
<module>telegram-sender-core</module>
<module>telegram-quarkus-reactive-sender</module>
<module>telegram-simple-sender</module>
</modules>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
</properties> </properties>
<dependencies>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-domain</artifactId>
</dependency>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-core</artifactId>
</dependency>
<dependency>
<groupId>dev.struchkov.godfather</groupId>
<artifactId>bot-context</artifactId>
</dependency>
<dependency>
<groupId>dev.struchkov.haiti</groupId>
<artifactId>haiti-utils</artifactId>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
</dependency>
</dependencies>
</project> </project>

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-sender</artifactId>
<version>0.0.30</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>telegram-quarkus-reactive-sender</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-sender-core</artifactId>
</dependency>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-context</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-core</artifactId>
<version>2.24.1</version>
</dependency>
</dependencies>
</project>

View File

@ -1,4 +1,4 @@
package dev.struchkov.godfather.telegram.service; package dev.struchkov.godfather.telegram.quarkus.sender;
/** /**
* // TODO: 18.09.2020 Добавить описание. * // TODO: 18.09.2020 Добавить описание.

View File

@ -0,0 +1,13 @@
package dev.struchkov.godfather.telegram.quarkus.sender;
import io.smallrye.mutiny.Uni;
public interface SenderStorageService {
Uni<Integer> getLastSendMessage(Long telegramId);
Uni<Void> saveLastSendMessage(Long telegramId, Integer messageId);
Uni<Void> removeLastSendMessage(Long telegramId);
}

View File

@ -0,0 +1,129 @@
package dev.struchkov.godfather.telegram.quarkus.sender;
import dev.struchkov.godfather.context.domain.BoxAnswer;
import dev.struchkov.godfather.telegram.context.TelegramConnect;
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.sender.util.KeyBoardConvert;
import io.smallrye.mutiny.Uni;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import static dev.struchkov.godfather.telegram.sender.util.KeyBoardConvert.convertInlineKeyBoard;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
import static dev.struchkov.haiti.utils.Inspector.isNotNull;
public class TelegramSender {
private static final Logger log = LoggerFactory.getLogger(TelegramSender.class);
private static final String ERROR_REPLACE_MESSAGE = "Bad Request: message to edit not found";
private final AbsSender absSender;
private SendPreProcessing sendPreProcessing;
private SenderStorageService senderStorageService;
public TelegramSender(TelegramConnect telegramConnect) {
this.absSender = telegramConnect.getAbsSender();
}
public TelegramSender(TelegramConnect telegramConnect, SenderStorageService senderStorageService) {
this.absSender = telegramConnect.getAbsSender();
this.senderStorageService = senderStorageService;
}
public void setSendPreProcessing(SendPreProcessing sendPreProcessing) {
this.sendPreProcessing = sendPreProcessing;
}
public void setSenderRepository(SenderStorageService senderStorageService) {
this.senderStorageService = senderStorageService;
}
public Uni<Void> send(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) {
return sendBoxAnswer(telegramId, boxAnswer, true);
}
public Uni<Void> sendNotSave(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) {
return sendBoxAnswer(telegramId, boxAnswer, false);
}
private Uni<Void> sendBoxAnswer(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
isNotNull(telegramId, boxAnswer);
if (boxAnswer.isReplace() && checkNotNull(senderStorageService)) {
return senderStorageService.getLastSendMessage(telegramId)
.onItem().transformToUni(
lastId -> {
if (checkNotNull(lastId)) {
return replaceMessage(telegramId, lastId, boxAnswer);
} else {
return sendMessage(telegramId, boxAnswer, saveMessageId);
}
}
);
} else {
return sendMessage(telegramId, boxAnswer, saveMessageId);
}
}
private Uni<Void> replaceMessage(@NotNull Long telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer) {
return Uni.createFrom().voidItem()
.onItem().transformToUni(
v -> {
final EditMessageText editMessageText = new EditMessageText();
editMessageText.setChatId(String.valueOf(telegramId));
editMessageText.setMessageId(lastMessageId);
editMessageText.enableMarkdown(true);
editMessageText.setText(boxAnswer.getMessage());
editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard()));
try {
absSender.execute(editMessageText);
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
return Uni.createFrom().voidItem();
}
);
}
private Uni<Void> sendMessage(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
return Uni.createFrom().voidItem()
.onItem().transformToUni(
v -> {
final SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true);
sendMessage.setChatId(String.valueOf(telegramId));
sendMessage.setText(
sendPreProcessing != null
? sendPreProcessing.pretreatment(boxAnswer.getMessage())
: boxAnswer.getMessage()
);
sendMessage.setReplyMarkup(KeyBoardConvert.convertKeyBoard(boxAnswer.getKeyBoard()));
try {
final Message execute = absSender.execute(sendMessage);
if (checkNotNull(senderStorageService) && saveMessageId) {
return senderStorageService.saveLastSendMessage(telegramId, execute.getMessageId());
}
} catch (TelegramApiRequestException e) {
log.error(e.getApiResponse());
if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) {
return sendMessage(telegramId, boxAnswer, saveMessageId);
}
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
return Uni.createFrom().voidItem();
}
);
}
}

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-sender</artifactId>
<version>0.0.30</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>telegram-sender-core</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-domain</artifactId>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,4 +1,4 @@
package dev.struchkov.godfather.telegram.utils; package dev.struchkov.godfather.telegram.sender.util;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoard; import dev.struchkov.godfather.context.domain.keyboard.KeyBoard;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoardButton; import dev.struchkov.godfather.context.domain.keyboard.KeyBoardButton;
@ -24,9 +24,6 @@ import org.telegram.telegrambots.meta.api.objects.webapp.WebAppInfo;
import java.util.List; import java.util.List;
import static dev.struchkov.haiti.context.exception.ConvertException.convertException;
import static dev.struchkov.haiti.utils.Inspector.isNull;
public final class KeyBoardConvert { public final class KeyBoardConvert {
private KeyBoardConvert() { private KeyBoardConvert() {
@ -133,7 +130,7 @@ public final class KeyBoardConvert {
case SimpleButton.TYPE -> { case SimpleButton.TYPE -> {
final SimpleButton simpleButton = (SimpleButton) keyBoardButton; final SimpleButton simpleButton = (SimpleButton) keyBoardButton;
button.setText(simpleButton.getLabel()); button.setText(simpleButton.getLabel());
isNull(simpleButton.getCallbackData(), convertException("CallbackData поддерживает только Inline клавитаура")); Inspector.isNull(simpleButton.getCallbackData(), ConvertException.convertException("CallbackData поддерживает только Inline клавитаура"));
} }
case WebAppButton.TYPE -> { case WebAppButton.TYPE -> {
final WebAppButton webAppButton = (WebAppButton) keyBoardButton; final WebAppButton webAppButton = (WebAppButton) keyBoardButton;

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-sender</artifactId>
<version>0.0.30</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>telegram-simple-sender</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-sender-core</artifactId>
</dependency>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-core</artifactId>
</dependency>
<dependency>
<groupId>dev.struchkov.godfather</groupId>
<artifactId>bot-context</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,13 @@
package dev.struchkov.godfather.telegram.sender.service;
/**
* // TODO: 18.09.2020 Добавить описание.
*
* @author upagge 18.09.2020
*/
@FunctionalInterface
public interface SendPreProcessing {
String pretreatment(String messageText);
}

View File

@ -1,4 +1,4 @@
package dev.struchkov.godfather.telegram.service; package dev.struchkov.godfather.telegram.sender.service;
import dev.struchkov.godfather.context.domain.BoxAnswer; import dev.struchkov.godfather.context.domain.BoxAnswer;
import dev.struchkov.godfather.context.service.sender.SendType; import dev.struchkov.godfather.context.service.sender.SendType;
@ -6,7 +6,7 @@ import dev.struchkov.godfather.telegram.context.SenderStorageService;
import dev.struchkov.godfather.telegram.context.TelegramConnect; import dev.struchkov.godfather.telegram.context.TelegramConnect;
import dev.struchkov.godfather.telegram.context.TelegramSending; import dev.struchkov.godfather.telegram.context.TelegramSending;
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.utils.KeyBoardConvert; import dev.struchkov.godfather.telegram.sender.util.KeyBoardConvert;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.26</version> <version>0.0.30</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -24,12 +24,10 @@
<dependency> <dependency>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-consumer</artifactId> <artifactId>telegram-consumer</artifactId>
<version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-sender</artifactId> <artifactId>telegram-simple-sender</artifactId>
<version>${project.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>