diff --git a/.drone.yml b/.drone.yml index 670e29f..ff49fbd 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,6 +1,45 @@ +--- kind: pipeline type: docker -name: haiti-framework +name: snapshot-publish + +trigger: + branch: + - develop + +volumes: + - name: m2 + host: + path: /drone/volume/m2 + +steps: + + - name: publish + image: maven:3.8.5-openjdk-17 + privileged: true + volumes: + - name: m2 + path: /root/.m2/repository + environment: + MAVEN_SETTINGS: + from_secret: MAVEN_SETTINGS + commands: + - echo "$MAVEN_SETTINGS" >> maven-settings.xml + - mvn --settings maven-settings.xml -U -P snapshot clean deploy + +--- +kind: pipeline +type: docker +name: release-publish + +trigger: + ref: + - refs/tags/v.*.*.* + +volumes: + - name: m2 + host: + path: /drone/volume/m2 steps: @@ -22,12 +61,8 @@ steps: - echo "$MAVEN_SETTINGS" >> maven-settings.xml - gpg --pinentry-mode loopback --passphrase $GPG_PASSPHRASE --import gpg.key - mvn --settings maven-settings.xml -U -P ossrh,release clean deploy +--- +kind: signature +hmac: b723bf6653b2825acdb1de24d88acc510d58c7336f642e71ed0ba0489a7066eb -trigger: - ref: - - refs/tags/v.*.*.* - -volumes: - - name: m2 - host: - path: /drone/volume/m2 \ No newline at end of file +... diff --git a/pom.xml b/pom.xml index 9980df9..2a7947f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 dev.struchkov.godfather.telegram @@ -37,7 +38,7 @@ UTF-8 UTF-8 - 0.0.41 + 0.0.42-SNAPSHOT 6.3.0 2.0.0 @@ -228,11 +229,6 @@ nexus-staging-maven-plugin ${plugin.nexus.staging.ver} true - - ossrh - https://s01.oss.sonatype.org/ - true - org.apache.maven.plugins @@ -289,7 +285,7 @@ ${plugin.maven.release.ver} clean compile - install + install v.@{project.version} true false @@ -312,6 +308,10 @@ org.apache.maven.plugins maven-source-plugin + + org.apache.maven.plugins + maven-release-plugin + @@ -320,13 +320,14 @@ release - - org.apache.maven.plugins - maven-release-plugin - org.sonatype.plugins nexus-staging-maven-plugin + + ossrh + https://s01.oss.sonatype.org/ + true + org.apache.maven.plugins @@ -343,28 +344,83 @@ + + snapshot + + + + org.sonatype.plugins + nexus-staging-maven-plugin + + struchkov-nexus + https://nexus.struchkov.dev/nexus/ + true + + + + org.apache.maven.plugins + maven-source-plugin + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + - - scm:git:https://github.com/Godfather-Bots/telegram-bot.git - https://github.com/Godfather-Bots/telegram-bot - scm:git:https://github.com/Godfather-Bots/telegram-bot.git - HEAD - + + + struchkov-nexus-release + https://nexus.struchkov.dev/repository/maven-releases/ + + true + always + fail + + + false + + + + struchkov-nexus-snapshot + https://nexus.struchkov.dev/repository/maven-snapshots/ + + false + + + true + always + warn + + + + + struchkov-nexus-release + https://nexus.struchkov.dev/repository/maven-releases/ + - ossrh - https://s01.oss.sonatype.org/content/repositories/snapshots + struchkov-nexus-snapshot + https://nexus.struchkov.dev/repository/maven-snapshots/ + + scm:git:https://git.struchkov.dev/Godfather-Bots/telegram-bot.git + https://git.struchkov.dev/Godfather-Bots/telegram-bot + scm:git:https://git.struchkov.dev/Godfather-Bots/telegram-bot.git + HEAD + + uPagge Struchkov Mark mark@struchkov.dev - https://struchkov.dev + https://mark.struchkov.dev diff --git a/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramSending.java b/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramSending.java index f60763f..bdef065 100644 --- a/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramSending.java +++ b/telegram-context/telegram-context-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/context/service/TelegramSending.java @@ -1,12 +1,13 @@ package dev.struchkov.godfather.telegram.quarkus.context.service; import dev.struchkov.godfather.main.domain.BoxAnswer; -import dev.struchkov.godfather.quarkus.context.service.Sending; +import dev.struchkov.godfather.main.domain.SentBox; +import dev.struchkov.godfather.quarkus.context.service.SendingService; import io.smallrye.mutiny.Uni; import org.jetbrains.annotations.NotNull; -public interface TelegramSending extends Sending { +public interface TelegramSending extends SendingService { - Uni sendNotSave(@NotNull BoxAnswer boxAnswer); + Uni> sendNotSave(@NotNull BoxAnswer boxAnswer); } diff --git a/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramSending.java b/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramSending.java index d2d612e..30f9174 100644 --- a/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramSending.java +++ b/telegram-context/telegram-context-simple/src/main/java/dev/struchkov/godfather/telegram/simple/context/service/TelegramSending.java @@ -1,11 +1,14 @@ package dev.struchkov.godfather.telegram.simple.context.service; import dev.struchkov.godfather.main.domain.BoxAnswer; -import dev.struchkov.godfather.simple.context.service.Sending; +import dev.struchkov.godfather.main.domain.SentBox; +import dev.struchkov.godfather.simple.context.service.SendingService; import org.jetbrains.annotations.NotNull; -public interface TelegramSending extends Sending { +import java.util.Optional; - void sendNotSave(@NotNull BoxAnswer boxAnswer); +public interface TelegramSending extends SendingService { + + Optional> sendNotSave(@NotNull BoxAnswer boxAnswer); } diff --git a/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/util/TriggerChecks.java b/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/util/TriggerChecks.java new file mode 100644 index 0000000..3252609 --- /dev/null +++ b/telegram-core/telegram-core-simple/src/main/java/dev/struchkov/godfather/telegram/simple/core/util/TriggerChecks.java @@ -0,0 +1,30 @@ +package dev.struchkov.godfather.telegram.simple.core.util; + +import dev.struchkov.godfather.main.domain.content.Mail; +import dev.struchkov.godfather.telegram.domain.attachment.ButtonClickAttachment; +import dev.struchkov.godfather.telegram.main.core.util.Attachments; + +import java.util.Optional; +import java.util.function.Predicate; + +import static dev.struchkov.haiti.utils.Exceptions.utilityClass; + +public class TriggerChecks { + + private TriggerChecks() { + utilityClass(); + } + + public static Predicate clickButtonRaw(String rawCallBackData) { + return mail -> { + final Optional optButtonClick = Attachments.findFirstButtonClick(mail.getAttachments()); + if (optButtonClick.isPresent()) { + final ButtonClickAttachment buttonClick = optButtonClick.get(); + final String rawData = buttonClick.getRawCallBackData(); + return rawData.equals(rawCallBackData); + } + return false; + }; + } + +} diff --git a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java b/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java index 7357618..935c35b 100644 --- a/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java +++ b/telegram-sender/telegram-sender-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/sender/TelegramSender.java @@ -2,6 +2,7 @@ package dev.struchkov.godfather.telegram.quarkus.sender; import dev.struchkov.godfather.main.domain.BoxAnswer; import dev.struchkov.godfather.main.domain.SendType; +import dev.struchkov.godfather.main.domain.SentBox; import dev.struchkov.godfather.quarkus.context.service.PreSendProcessing; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.main.context.TelegramConnect; @@ -13,8 +14,8 @@ 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.DeleteMessage; 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; @@ -50,7 +51,7 @@ public class TelegramSender implements TelegramSending { } @Override - public Uni send(@NotNull BoxAnswer boxAnswer) { + public Uni> send(@NotNull BoxAnswer boxAnswer) { return sendBoxAnswer(boxAnswer, true); } @@ -60,11 +61,29 @@ public class TelegramSender implements TelegramSending { } @Override - public Uni sendNotSave(@NotNull BoxAnswer boxAnswer) { + public Uni deleteMessage(@NotNull String personId, @NotNull Integer messageId) { + final DeleteMessage deleteMessage = new DeleteMessage(); + deleteMessage.setChatId(personId); + deleteMessage.setMessageId(messageId); + try { + absSender.execute(deleteMessage); + } catch (TelegramApiException e) { + log.error(e.getMessage(), e); + } + return Uni.createFrom().voidItem(); + } + + @Override + public Uni> replaceMessage(@NotNull String personId, @NotNull Integer messageId, @NotNull BoxAnswer newAnswer) { + return replace(personId, messageId, newAnswer, true); + } + + @Override + public Uni> sendNotSave(@NotNull BoxAnswer boxAnswer) { return sendBoxAnswer(boxAnswer, false); } - private Uni sendBoxAnswer(@NotNull BoxAnswer boxAnswer, boolean saveMessageId) { + private Uni> sendBoxAnswer(@NotNull BoxAnswer boxAnswer, boolean saveMessageId) { return Uni.createFrom().voidItem() .onItem().transformToUni( v -> { @@ -74,7 +93,7 @@ public class TelegramSender implements TelegramSending { .onItem().transformToUni( lastId -> { if (checkNotNull(lastId)) { - return replaceMessage(recipientTelegramId, lastId, boxAnswer); + return replace(recipientTelegramId, lastId, boxAnswer, saveMessageId); } else { return sendMessage(recipientTelegramId, boxAnswer, saveMessageId); } @@ -87,7 +106,7 @@ public class TelegramSender implements TelegramSending { ); } - private Uni replaceMessage(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer) { + private Uni> replace(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { return Uni.createFrom().voidItem() .onItem().transformToUni( v -> { @@ -99,17 +118,23 @@ public class TelegramSender implements TelegramSending { editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard())); try { absSender.execute(editMessageText); + return Uni.createFrom().optional(SentBox.optional(lastMessageId, boxAnswer, boxAnswer)); + } 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()); + log.error(e.getMessage(), e); } - return Uni.createFrom().voidItem(); + return Uni.createFrom().nullItem(); } ); } - private Uni sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { + private Uni> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { return Uni.createFrom().voidItem() - .onItem().transformToUni( + .onItem().transform( v -> { final SendMessage sendMessage = new SendMessage(); sendMessage.enableMarkdown(true); @@ -118,21 +143,23 @@ public class TelegramSender implements TelegramSending { sendMessage.setReplyMarkup(KeyBoardConvert.convertKeyBoard(boxAnswer.getKeyBoard())); try { - final Message execute = absSender.execute(sendMessage); - if (checkNotNull(senderRepository) && saveMessageId) { - return senderRepository.saveLastSendMessage(telegramId, execute.getMessageId()); - } + return absSender.execute(sendMessage); } 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(); + return null; } - ).replaceWithVoid(); + ).onItem().ifNotNull().call(answerMessage -> { + if (checkNotNull(senderRepository) && saveMessageId) { + return senderRepository.saveLastSendMessage(telegramId, answerMessage.getMessageId()); + } + return null; + }) + .onItem().ifNotNull().transformToUni( + answerMessage -> Uni.createFrom().optional(SentBox.optional(answerMessage.getMessageId(), boxAnswer, boxAnswer)) + ); } @Override diff --git a/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java b/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java index 0d01459..85487d8 100644 --- a/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java +++ b/telegram-sender/telegram-sender-simple/src/main/java/dev/struchkov/godfather/telegram/simple/sender/TelegramSender.java @@ -2,6 +2,7 @@ package dev.struchkov.godfather.telegram.simple.sender; import dev.struchkov.godfather.main.domain.BoxAnswer; import dev.struchkov.godfather.main.domain.SendType; +import dev.struchkov.godfather.main.domain.SentBox; import dev.struchkov.godfather.simple.context.service.PreSendProcessing; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.main.context.TelegramConnect; @@ -12,6 +13,7 @@ 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.DeleteMessage; import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText; import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.bots.AbsSender; @@ -33,7 +35,7 @@ public class TelegramSender implements TelegramSending { private final AbsSender absSender; - private List preSendProcessors = new ArrayList<>(); + private final List preSendProcessors = new ArrayList<>(); private SenderRepository senderRepository; public TelegramSender(TelegramConnect telegramConnect) { @@ -49,62 +51,82 @@ public class TelegramSender implements TelegramSending { this.senderRepository = senderRepository; } - @Override - public void send(@NotNull BoxAnswer boxAnswer) { - isNotNull(boxAnswer.getRecipientPersonId()); - sendBoxAnswer(boxAnswer, true); - } - @Override public void addPreSendProcess(@NotNull PreSendProcessing processing) { preSendProcessors.add(processing); } @Override - public void sendNotSave(@NotNull BoxAnswer boxAnswer) { - sendBoxAnswer(boxAnswer, false); + public void deleteMessage(@NotNull String personId, @NotNull Integer messageId) { + final DeleteMessage deleteMessage = new DeleteMessage(); + deleteMessage.setChatId(personId); + deleteMessage.setMessageId(messageId); + try { + absSender.execute(deleteMessage); + } catch (TelegramApiException e) { + log.error(e.getMessage(), e); + } } - private void sendBoxAnswer(BoxAnswer boxAnswer, boolean saveMessageId) { + @Override + public Optional> replaceMessage(@NotNull String personId, @NotNull Integer messageId, @NotNull BoxAnswer newAnswer) { + return replace(personId, messageId, newAnswer, newAnswer, true); + } + + @Override + public Optional> send(@NotNull BoxAnswer boxAnswer) { + isNotNull(boxAnswer.getRecipientPersonId()); + return sendBoxAnswer(boxAnswer, true); + } + + @Override + public Optional> sendNotSave(@NotNull BoxAnswer boxAnswer) { + return sendBoxAnswer(boxAnswer, false); + } + + private Optional> sendBoxAnswer(BoxAnswer boxAnswer, boolean saveMessageId) { final String recipientTelegramId = boxAnswer.getRecipientPersonId(); isNotNull(recipientTelegramId); + BoxAnswer preparedAnswer = boxAnswer; for (PreSendProcessing preSendProcessor : preSendProcessors) { - boxAnswer = preSendProcessor.pretreatment(boxAnswer); + preparedAnswer = preSendProcessor.pretreatment(boxAnswer); } - try { - if (boxAnswer.isReplace() && checkNotNull(senderRepository)) { - final Optional optLastId = senderRepository.getLastSendMessage(recipientTelegramId); - if (optLastId.isPresent()) { - replaceMessage(recipientTelegramId, optLastId.get(), boxAnswer); - } else { - sendMessage(recipientTelegramId, boxAnswer, saveMessageId); - } + if (preparedAnswer.isReplace() && checkNotNull(senderRepository)) { + final Optional optLastId = senderRepository.getLastSendMessage(recipientTelegramId); + if (optLastId.isPresent()) { + return replace(recipientTelegramId, optLastId.get(), boxAnswer, preparedAnswer, saveMessageId); } else { - sendMessage(recipientTelegramId, boxAnswer, saveMessageId); + return sendMessage(recipientTelegramId, boxAnswer, preparedAnswer, saveMessageId); } - } catch (TelegramApiRequestException e) { - log.error(e.getApiResponse()); - if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) { - sendMessage(recipientTelegramId, boxAnswer, saveMessageId); - } - } catch (TelegramApiException e) { - log.error(e.getMessage()); + } else { + return sendMessage(recipientTelegramId, boxAnswer, preparedAnswer, saveMessageId); } } - private void replaceMessage(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer) throws TelegramApiException { + private Optional> replace(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer, BoxAnswer preparedAnswer, boolean saveMessageId) { final EditMessageText editMessageText = new EditMessageText(); editMessageText.setChatId(telegramId); editMessageText.setMessageId(lastMessageId); editMessageText.enableMarkdown(true); editMessageText.setText(boxAnswer.getMessage()); editMessageText.setReplyMarkup(KeyBoardConvert.convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard())); - absSender.execute(editMessageText); + try { + absSender.execute(editMessageText); + return SentBox.optional(lastMessageId, preparedAnswer, boxAnswer); + } catch (TelegramApiRequestException e) { + log.error(e.getApiResponse()); + if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) { + return sendMessage(telegramId, preparedAnswer, preparedAnswer, saveMessageId); + } + } catch (TelegramApiException e) { + log.error(e.getMessage(), e); + } + return Optional.empty(); } - private void sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { + private Optional> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, BoxAnswer preparedAnswer, boolean saveMessageId) { final SendMessage sendMessage = new SendMessage(); sendMessage.enableMarkdown(true); sendMessage.setChatId(telegramId); @@ -115,12 +137,13 @@ public class TelegramSender implements TelegramSending { if (checkNotNull(senderRepository) && saveMessageId) { senderRepository.saveLastSendMessage(telegramId, execute.getMessageId()); } + return SentBox.optional(execute.getMessageId(), preparedAnswer, boxAnswer); } catch (TelegramApiRequestException e) { log.error(e.getApiResponse()); } catch (TelegramApiException e) { log.error(e.getMessage()); } - + return Optional.empty(); } @Override