Исправление преобразования клавиаутры

This commit is contained in:
Struchkov Mark 2022-05-11 15:28:00 +03:00
parent 760c771e8d
commit 2524de40f3
3 changed files with 34 additions and 26 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.5</version> <version>0.0.6</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>

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.5</version> <version>0.0.6</version>
</parent> </parent>
<artifactId>telegram-core</artifactId> <artifactId>telegram-core</artifactId>
@ -13,10 +13,6 @@
<name>Telegram Core</name> <name>Telegram Core</name>
<description>Allows you to create bots for Telegram</description> <description>Allows you to create bots for Telegram</description>
<properties>
<skip.deploy>false</skip.deploy>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>

View File

@ -12,6 +12,9 @@ import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.domain.keyboard.MarkupKeyBoard; import dev.struchkov.godfather.telegram.domain.keyboard.MarkupKeyBoard;
import dev.struchkov.godfather.telegram.domain.keyboard.button.ButtonUrl; import dev.struchkov.godfather.telegram.domain.keyboard.button.ButtonUrl;
import dev.struchkov.godfather.telegram.service.SendPreProcessing; import dev.struchkov.godfather.telegram.service.SendPreProcessing;
import dev.struchkov.haiti.context.exception.ConvertException;
import dev.struchkov.haiti.utils.Inspector;
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;
@ -31,6 +34,8 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static dev.struchkov.haiti.utils.Inspector.isNotNull;
/** /**
* TODO: Добавить описание класса. * TODO: Добавить описание класса.
* *
@ -53,7 +58,8 @@ public class TelegramSender implements Sending {
this.sendPreProcessing = sendPreProcessing; this.sendPreProcessing = sendPreProcessing;
} }
public void send(Long telegramId, BoxAnswer boxAnswer) { public void send(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) {
isNotNull(telegramId, boxAnswer);
try { try {
if (boxAnswer.isReplace() && map.containsKey(telegramId)) { if (boxAnswer.isReplace() && map.containsKey(telegramId)) {
final EditMessageText editMessageText = new EditMessageText(); final EditMessageText editMessageText = new EditMessageText();
@ -109,26 +115,31 @@ public class TelegramSender implements Sending {
} }
private ReplyKeyboard convertMarkupKeyBoard(MarkupKeyBoard keyBoard) { private ReplyKeyboard convertMarkupKeyBoard(MarkupKeyBoard keyBoard) {
final ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup(); if (keyBoard != null) {
keyboardMarkup.setOneTimeKeyboard(keyBoard.isOneTime()); final ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
keyboardMarkup.setInputFieldPlaceholder(keyBoard.getInputFieldPlaceholder()); keyboardMarkup.setOneTimeKeyboard(keyBoard.isOneTime());
keyboardMarkup.setResizeKeyboard(keyBoard.isResizeKeyboard()); keyboardMarkup.setInputFieldPlaceholder(keyBoard.getInputFieldPlaceholder());
keyboardMarkup.setKeyboard( keyboardMarkup.setResizeKeyboard(keyBoard.isResizeKeyboard());
keyBoard.getLines().stream() keyboardMarkup.setKeyboard(
.map(this::convertMarkupLine) keyBoard.getLines().stream()
.toList() .map(this::convertMarkupLine)
); .toList()
return keyboardMarkup; );
return keyboardMarkup;
}
} }
private InlineKeyboardMarkup convertInlineKeyBoard(InlineKeyBoard keyBoard) { private InlineKeyboardMarkup convertInlineKeyBoard(InlineKeyBoard keyBoard) {
final InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup(); if (keyBoard != null) {
inlineKeyboardMarkup.setKeyboard( final InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
keyBoard.getLines().stream() inlineKeyboardMarkup.setKeyboard(
.map(this::convertInlineLine) keyBoard.getLines().stream()
.toList() .map(this::convertInlineLine)
); .toList()
return inlineKeyboardMarkup; );
return inlineKeyboardMarkup;
}
return null;
} }
private List<InlineKeyboardButton> convertInlineLine(KeyBoardLine line) { private List<InlineKeyboardButton> convertInlineLine(KeyBoardLine line) {
@ -155,7 +166,7 @@ public class TelegramSender implements Sending {
button.setUrl(buttonUrl.getUrl()); button.setUrl(buttonUrl.getUrl());
button.setText(buttonUrl.getLabel()); button.setText(buttonUrl.getLabel());
} }
default -> throw new RuntimeException("Ошибка преобразования кнопки"); default -> throw new ConvertException("Ошибка преобразования кнопки");
} }
return button; return button;
} }
@ -166,8 +177,9 @@ public class TelegramSender implements Sending {
case SimpleButton.TYPE -> { case SimpleButton.TYPE -> {
final SimpleButton simpleButton = (SimpleButton) keyBoardButton; final SimpleButton simpleButton = (SimpleButton) keyBoardButton;
button.setText(simpleButton.getLabel()); button.setText(simpleButton.getLabel());
Inspector.isNull(simpleButton.getCallbackData(), ConvertException.supplier("CallbackData поддерживает только Inline клавитаура"));
} }
default -> throw new RuntimeException("Ошибка преобразования кнопки"); default -> throw new ConvertException("Ошибка преобразования кнопки");
} }
return button; return button;
} }