From 2524de40f35ffc96a3046a8c91bb3e6e77cea691 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Wed, 11 May 2022 15:28:00 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B5=D0=BE=D0=B1=D1=80?= =?UTF-8?q?=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B0=D0=B2=D0=B8=D0=B0=D1=83=D1=82=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- telegram-core/pom.xml | 6 +-- .../telegram/listen/TelegramSender.java | 52 ++++++++++++------- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index 9f8c6c6..29dc98e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.5 + 0.0.6 pom diff --git a/telegram-core/pom.xml b/telegram-core/pom.xml index 615235f..34332d2 100644 --- a/telegram-core/pom.xml +++ b/telegram-core/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.5 + 0.0.6 telegram-core @@ -13,10 +13,6 @@ Telegram Core Allows you to create bots for Telegram - - false - - dev.struchkov.godfather diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramSender.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramSender.java index d602d69..0961f3c 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramSender.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/TelegramSender.java @@ -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.button.ButtonUrl; 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.LoggerFactory; import org.telegram.telegrambots.meta.api.methods.send.SendMessage; @@ -31,6 +34,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import static dev.struchkov.haiti.utils.Inspector.isNotNull; + /** * TODO: Добавить описание класса. * @@ -53,7 +58,8 @@ public class TelegramSender implements Sending { this.sendPreProcessing = sendPreProcessing; } - public void send(Long telegramId, BoxAnswer boxAnswer) { + public void send(@NotNull Long telegramId, @NotNull BoxAnswer boxAnswer) { + isNotNull(telegramId, boxAnswer); try { if (boxAnswer.isReplace() && map.containsKey(telegramId)) { final EditMessageText editMessageText = new EditMessageText(); @@ -109,26 +115,31 @@ public class TelegramSender implements Sending { } private ReplyKeyboard convertMarkupKeyBoard(MarkupKeyBoard keyBoard) { - final ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup(); - keyboardMarkup.setOneTimeKeyboard(keyBoard.isOneTime()); - keyboardMarkup.setInputFieldPlaceholder(keyBoard.getInputFieldPlaceholder()); - keyboardMarkup.setResizeKeyboard(keyBoard.isResizeKeyboard()); - keyboardMarkup.setKeyboard( - keyBoard.getLines().stream() - .map(this::convertMarkupLine) - .toList() - ); - return keyboardMarkup; + if (keyBoard != null) { + final ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup(); + keyboardMarkup.setOneTimeKeyboard(keyBoard.isOneTime()); + keyboardMarkup.setInputFieldPlaceholder(keyBoard.getInputFieldPlaceholder()); + keyboardMarkup.setResizeKeyboard(keyBoard.isResizeKeyboard()); + keyboardMarkup.setKeyboard( + keyBoard.getLines().stream() + .map(this::convertMarkupLine) + .toList() + ); + return keyboardMarkup; + } } private InlineKeyboardMarkup convertInlineKeyBoard(InlineKeyBoard keyBoard) { - final InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup(); - inlineKeyboardMarkup.setKeyboard( - keyBoard.getLines().stream() - .map(this::convertInlineLine) - .toList() - ); - return inlineKeyboardMarkup; + if (keyBoard != null) { + final InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup(); + inlineKeyboardMarkup.setKeyboard( + keyBoard.getLines().stream() + .map(this::convertInlineLine) + .toList() + ); + return inlineKeyboardMarkup; + } + return null; } private List convertInlineLine(KeyBoardLine line) { @@ -155,7 +166,7 @@ public class TelegramSender implements Sending { button.setUrl(buttonUrl.getUrl()); button.setText(buttonUrl.getLabel()); } - default -> throw new RuntimeException("Ошибка преобразования кнопки"); + default -> throw new ConvertException("Ошибка преобразования кнопки"); } return button; } @@ -166,8 +177,9 @@ public class TelegramSender implements Sending { case SimpleButton.TYPE -> { final SimpleButton simpleButton = (SimpleButton) keyBoardButton; button.setText(simpleButton.getLabel()); + Inspector.isNull(simpleButton.getCallbackData(), ConvertException.supplier("CallbackData поддерживает только Inline клавитаура")); } - default -> throw new RuntimeException("Ошибка преобразования кнопки"); + default -> throw new ConvertException("Ошибка преобразования кнопки"); } return button; }