Переработка под новую версию godfather
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-01-15 16:37:24 +03:00
parent ff2d302db5
commit 00cf2705fd
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
7 changed files with 261 additions and 86 deletions

View File

@ -1,6 +1,45 @@
---
kind: pipeline kind: pipeline
type: docker 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: steps:
@ -22,12 +61,8 @@ steps:
- echo "$MAVEN_SETTINGS" >> maven-settings.xml - echo "$MAVEN_SETTINGS" >> maven-settings.xml
- gpg --pinentry-mode loopback --passphrase $GPG_PASSPHRASE --import gpg.key - gpg --pinentry-mode loopback --passphrase $GPG_PASSPHRASE --import gpg.key
- mvn --settings maven-settings.xml -U -P ossrh,release clean deploy - 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

98
pom.xml
View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?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"> <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">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>dev.struchkov.godfather.telegram</groupId> <groupId>dev.struchkov.godfather.telegram</groupId>
@ -37,7 +38,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.41</godfather.core.ver> <godfather.core.ver>0.0.42-SNAPSHOT</godfather.core.ver>
<telegrambots.ver>6.3.0</telegrambots.ver> <telegrambots.ver>6.3.0</telegrambots.ver>
<haiti.version>2.0.0</haiti.version> <haiti.version>2.0.0</haiti.version>
@ -228,11 +229,6 @@
<artifactId>nexus-staging-maven-plugin</artifactId> <artifactId>nexus-staging-maven-plugin</artifactId>
<version>${plugin.nexus.staging.ver}</version> <version>${plugin.nexus.staging.ver}</version>
<extensions>true</extensions> <extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
@ -289,7 +285,7 @@
<version>${plugin.maven.release.ver}</version> <version>${plugin.maven.release.ver}</version>
<configuration> <configuration>
<preparationGoals>clean compile</preparationGoals> <preparationGoals>clean compile</preparationGoals>
<goals>install</goals> <goal>install</goal>
<tagNameFormat>v.@{project.version}</tagNameFormat> <tagNameFormat>v.@{project.version}</tagNameFormat>
<autoVersionSubmodules>true</autoVersionSubmodules> <autoVersionSubmodules>true</autoVersionSubmodules>
<pushChanges>false</pushChanges> <pushChanges>false</pushChanges>
@ -312,6 +308,10 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId> <artifactId>maven-source-plugin</artifactId>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
</plugin>
</plugins> </plugins>
</build> </build>
@ -320,13 +320,14 @@
<id>release</id> <id>release</id>
<build> <build>
<plugins> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
</plugin>
<plugin> <plugin>
<groupId>org.sonatype.plugins</groupId> <groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId> <artifactId>nexus-staging-maven-plugin</artifactId>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
@ -343,28 +344,83 @@
</plugins> </plugins>
</build> </build>
</profile> </profile>
<profile>
<id>snapshot</id>
<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<configuration>
<serverId>struchkov-nexus</serverId>
<nexusUrl>https://nexus.struchkov.dev/nexus/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles> </profiles>
<scm> <repositories>
<connection>scm:git:https://github.com/Godfather-Bots/telegram-bot.git</connection> <repository>
<url>https://github.com/Godfather-Bots/telegram-bot</url> <id>struchkov-nexus-release</id>
<developerConnection>scm:git:https://github.com/Godfather-Bots/telegram-bot.git</developerConnection> <url>https://nexus.struchkov.dev/repository/maven-releases/</url>
<tag>HEAD</tag> <releases>
</scm> <enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>struchkov-nexus-snapshot</id>
<url>https://nexus.struchkov.dev/repository/maven-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
<distributionManagement> <distributionManagement>
<repository>
<id>struchkov-nexus-release</id>
<url>https://nexus.struchkov.dev/repository/maven-releases/</url>
</repository>
<snapshotRepository> <snapshotRepository>
<id>ossrh</id> <id>struchkov-nexus-snapshot</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url> <url>https://nexus.struchkov.dev/repository/maven-snapshots/</url>
</snapshotRepository> </snapshotRepository>
</distributionManagement> </distributionManagement>
<scm>
<connection>scm:git:https://git.struchkov.dev/Godfather-Bots/telegram-bot.git</connection>
<url>https://git.struchkov.dev/Godfather-Bots/telegram-bot</url>
<developerConnection>scm:git:https://git.struchkov.dev/Godfather-Bots/telegram-bot.git</developerConnection>
<tag>HEAD</tag>
</scm>
<developers> <developers>
<developer> <developer>
<id>uPagge</id> <id>uPagge</id>
<name>Struchkov Mark</name> <name>Struchkov Mark</name>
<email>mark@struchkov.dev</email> <email>mark@struchkov.dev</email>
<url>https://struchkov.dev</url> <url>https://mark.struchkov.dev</url>
</developer> </developer>
</developers> </developers>

View File

@ -1,12 +1,13 @@
package dev.struchkov.godfather.telegram.quarkus.context.service; package dev.struchkov.godfather.telegram.quarkus.context.service;
import dev.struchkov.godfather.main.domain.BoxAnswer; 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 io.smallrye.mutiny.Uni;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public interface TelegramSending extends Sending { public interface TelegramSending extends SendingService<Integer> {
Uni<Void> sendNotSave(@NotNull BoxAnswer boxAnswer); Uni<SentBox<Integer>> sendNotSave(@NotNull BoxAnswer boxAnswer);
} }

View File

@ -1,11 +1,14 @@
package dev.struchkov.godfather.telegram.simple.context.service; package dev.struchkov.godfather.telegram.simple.context.service;
import dev.struchkov.godfather.main.domain.BoxAnswer; 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; import org.jetbrains.annotations.NotNull;
public interface TelegramSending extends Sending { import java.util.Optional;
void sendNotSave(@NotNull BoxAnswer boxAnswer); public interface TelegramSending extends SendingService<Integer> {
Optional<SentBox<Integer>> sendNotSave(@NotNull BoxAnswer boxAnswer);
} }

View File

@ -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<Mail> clickButtonRaw(String rawCallBackData) {
return mail -> {
final Optional<ButtonClickAttachment> optButtonClick = Attachments.findFirstButtonClick(mail.getAttachments());
if (optButtonClick.isPresent()) {
final ButtonClickAttachment buttonClick = optButtonClick.get();
final String rawData = buttonClick.getRawCallBackData();
return rawData.equals(rawCallBackData);
}
return false;
};
}
}

View File

@ -2,6 +2,7 @@ package dev.struchkov.godfather.telegram.quarkus.sender;
import dev.struchkov.godfather.main.domain.BoxAnswer; import dev.struchkov.godfather.main.domain.BoxAnswer;
import dev.struchkov.godfather.main.domain.SendType; 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.quarkus.context.service.PreSendProcessing;
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.main.context.TelegramConnect;
@ -13,8 +14,8 @@ import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; 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.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
@ -50,7 +51,7 @@ public class TelegramSender implements TelegramSending {
} }
@Override @Override
public Uni<Void> send(@NotNull BoxAnswer boxAnswer) { public Uni<SentBox<Integer>> send(@NotNull BoxAnswer boxAnswer) {
return sendBoxAnswer(boxAnswer, true); return sendBoxAnswer(boxAnswer, true);
} }
@ -60,11 +61,29 @@ public class TelegramSender implements TelegramSending {
} }
@Override @Override
public Uni<Void> sendNotSave(@NotNull BoxAnswer boxAnswer) { public Uni<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);
}
return Uni.createFrom().voidItem();
}
@Override
public Uni<SentBox<Integer>> replaceMessage(@NotNull String personId, @NotNull Integer messageId, @NotNull BoxAnswer newAnswer) {
return replace(personId, messageId, newAnswer, true);
}
@Override
public Uni<SentBox<Integer>> sendNotSave(@NotNull BoxAnswer boxAnswer) {
return sendBoxAnswer(boxAnswer, false); return sendBoxAnswer(boxAnswer, false);
} }
private Uni<Void> sendBoxAnswer(@NotNull BoxAnswer boxAnswer, boolean saveMessageId) { private Uni<SentBox<Integer>> sendBoxAnswer(@NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
return Uni.createFrom().voidItem() return Uni.createFrom().voidItem()
.onItem().transformToUni( .onItem().transformToUni(
v -> { v -> {
@ -74,7 +93,7 @@ public class TelegramSender implements TelegramSending {
.onItem().transformToUni( .onItem().transformToUni(
lastId -> { lastId -> {
if (checkNotNull(lastId)) { if (checkNotNull(lastId)) {
return replaceMessage(recipientTelegramId, lastId, boxAnswer); return replace(recipientTelegramId, lastId, boxAnswer, saveMessageId);
} else { } else {
return sendMessage(recipientTelegramId, boxAnswer, saveMessageId); return sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
} }
@ -87,7 +106,7 @@ public class TelegramSender implements TelegramSending {
); );
} }
private Uni<Void> replaceMessage(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer) { private Uni<SentBox<Integer>> replace(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
return Uni.createFrom().voidItem() return Uni.createFrom().voidItem()
.onItem().transformToUni( .onItem().transformToUni(
v -> { v -> {
@ -99,17 +118,23 @@ public class TelegramSender implements TelegramSending {
editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard())); editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard()));
try { try {
absSender.execute(editMessageText); 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) { } catch (TelegramApiException e) {
log.error(e.getMessage()); log.error(e.getMessage(), e);
} }
return Uni.createFrom().voidItem(); return Uni.createFrom().nullItem();
} }
); );
} }
private Uni<Void> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) { private Uni<SentBox<Integer>> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, boolean saveMessageId) {
return Uni.createFrom().voidItem() return Uni.createFrom().voidItem()
.onItem().transformToUni( .onItem().transform(
v -> { v -> {
final SendMessage sendMessage = new SendMessage(); final SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true); sendMessage.enableMarkdown(true);
@ -118,21 +143,23 @@ public class TelegramSender implements TelegramSending {
sendMessage.setReplyMarkup(KeyBoardConvert.convertKeyBoard(boxAnswer.getKeyBoard())); sendMessage.setReplyMarkup(KeyBoardConvert.convertKeyBoard(boxAnswer.getKeyBoard()));
try { try {
final Message execute = absSender.execute(sendMessage); return absSender.execute(sendMessage);
if (checkNotNull(senderRepository) && saveMessageId) {
return senderRepository.saveLastSendMessage(telegramId, execute.getMessageId());
}
} catch (TelegramApiRequestException e) { } catch (TelegramApiRequestException e) {
log.error(e.getApiResponse()); log.error(e.getApiResponse());
if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) {
return sendMessage(telegramId, boxAnswer, saveMessageId);
}
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
log.error(e.getMessage()); 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 @Override

View File

@ -2,6 +2,7 @@ package dev.struchkov.godfather.telegram.simple.sender;
import dev.struchkov.godfather.main.domain.BoxAnswer; import dev.struchkov.godfather.main.domain.BoxAnswer;
import dev.struchkov.godfather.main.domain.SendType; 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.simple.context.service.PreSendProcessing;
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.main.context.TelegramConnect;
@ -12,6 +13,7 @@ import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; 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.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.bots.AbsSender;
@ -33,7 +35,7 @@ public class TelegramSender implements TelegramSending {
private final AbsSender absSender; private final AbsSender absSender;
private List<PreSendProcessing> preSendProcessors = new ArrayList<>(); private final List<PreSendProcessing> preSendProcessors = new ArrayList<>();
private SenderRepository senderRepository; private SenderRepository senderRepository;
public TelegramSender(TelegramConnect telegramConnect) { public TelegramSender(TelegramConnect telegramConnect) {
@ -49,62 +51,82 @@ public class TelegramSender implements TelegramSending {
this.senderRepository = senderRepository; this.senderRepository = senderRepository;
} }
@Override
public void send(@NotNull BoxAnswer boxAnswer) {
isNotNull(boxAnswer.getRecipientPersonId());
sendBoxAnswer(boxAnswer, true);
}
@Override @Override
public void addPreSendProcess(@NotNull PreSendProcessing processing) { public void addPreSendProcess(@NotNull PreSendProcessing processing) {
preSendProcessors.add(processing); preSendProcessors.add(processing);
} }
@Override @Override
public void sendNotSave(@NotNull BoxAnswer boxAnswer) { public void deleteMessage(@NotNull String personId, @NotNull Integer messageId) {
sendBoxAnswer(boxAnswer, false); 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<SentBox<Integer>> replaceMessage(@NotNull String personId, @NotNull Integer messageId, @NotNull BoxAnswer newAnswer) {
return replace(personId, messageId, newAnswer, newAnswer, true);
}
@Override
public Optional<SentBox<Integer>> send(@NotNull BoxAnswer boxAnswer) {
isNotNull(boxAnswer.getRecipientPersonId());
return sendBoxAnswer(boxAnswer, true);
}
@Override
public Optional<SentBox<Integer>> sendNotSave(@NotNull BoxAnswer boxAnswer) {
return sendBoxAnswer(boxAnswer, false);
}
private Optional<SentBox<Integer>> sendBoxAnswer(BoxAnswer boxAnswer, boolean saveMessageId) {
final String recipientTelegramId = boxAnswer.getRecipientPersonId(); final String recipientTelegramId = boxAnswer.getRecipientPersonId();
isNotNull(recipientTelegramId); isNotNull(recipientTelegramId);
BoxAnswer preparedAnswer = boxAnswer;
for (PreSendProcessing preSendProcessor : preSendProcessors) { for (PreSendProcessing preSendProcessor : preSendProcessors) {
boxAnswer = preSendProcessor.pretreatment(boxAnswer); preparedAnswer = preSendProcessor.pretreatment(boxAnswer);
} }
try { if (preparedAnswer.isReplace() && checkNotNull(senderRepository)) {
if (boxAnswer.isReplace() && checkNotNull(senderRepository)) { final Optional<Integer> optLastId = senderRepository.getLastSendMessage(recipientTelegramId);
final Optional<Integer> optLastId = senderRepository.getLastSendMessage(recipientTelegramId); if (optLastId.isPresent()) {
if (optLastId.isPresent()) { return replace(recipientTelegramId, optLastId.get(), boxAnswer, preparedAnswer, saveMessageId);
replaceMessage(recipientTelegramId, optLastId.get(), boxAnswer);
} else {
sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
}
} else { } else {
sendMessage(recipientTelegramId, boxAnswer, saveMessageId); return sendMessage(recipientTelegramId, boxAnswer, preparedAnswer, saveMessageId);
} }
} catch (TelegramApiRequestException e) { } else {
log.error(e.getApiResponse()); return sendMessage(recipientTelegramId, boxAnswer, preparedAnswer, saveMessageId);
if (ERROR_REPLACE_MESSAGE.equals(e.getApiResponse())) {
sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
}
} catch (TelegramApiException e) {
log.error(e.getMessage());
} }
} }
private void replaceMessage(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer) throws TelegramApiException { private Optional<SentBox<Integer>> replace(@NotNull String telegramId, @NotNull Integer lastMessageId, @NotNull BoxAnswer boxAnswer, BoxAnswer preparedAnswer, boolean saveMessageId) {
final EditMessageText editMessageText = new EditMessageText(); final EditMessageText editMessageText = new EditMessageText();
editMessageText.setChatId(telegramId); editMessageText.setChatId(telegramId);
editMessageText.setMessageId(lastMessageId); editMessageText.setMessageId(lastMessageId);
editMessageText.enableMarkdown(true); editMessageText.enableMarkdown(true);
editMessageText.setText(boxAnswer.getMessage()); editMessageText.setText(boxAnswer.getMessage());
editMessageText.setReplyMarkup(KeyBoardConvert.convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard())); 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<SentBox<Integer>> sendMessage(@NotNull String telegramId, @NotNull BoxAnswer boxAnswer, BoxAnswer preparedAnswer, boolean saveMessageId) {
final SendMessage sendMessage = new SendMessage(); final SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true); sendMessage.enableMarkdown(true);
sendMessage.setChatId(telegramId); sendMessage.setChatId(telegramId);
@ -115,12 +137,13 @@ public class TelegramSender implements TelegramSending {
if (checkNotNull(senderRepository) && saveMessageId) { if (checkNotNull(senderRepository) && saveMessageId) {
senderRepository.saveLastSendMessage(telegramId, execute.getMessageId()); senderRepository.saveLastSendMessage(telegramId, execute.getMessageId());
} }
return SentBox.optional(execute.getMessageId(), preparedAnswer, boxAnswer);
} catch (TelegramApiRequestException e) { } catch (TelegramApiRequestException e) {
log.error(e.getApiResponse()); log.error(e.getApiResponse());
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
log.error(e.getMessage()); log.error(e.getMessage());
} }
return Optional.empty();
} }
@Override @Override