diff --git a/bot-context/pom.xml b/bot-context/pom.xml index c53c835..a8eb960 100644 --- a/bot-context/pom.xml +++ b/bot-context/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.5 + 0.0.6 bot-context diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/BoxAnswer.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/BoxAnswer.java index cf9a970..426c5c5 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/BoxAnswer.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/BoxAnswer.java @@ -1,8 +1,6 @@ package dev.struchkov.godfather.context.domain; -import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.domain.keyboard.KeyBoard; -import dev.struchkov.godfather.context.service.usercode.ProcessingData; /** * Контейнер, которые содержит данные, которые будут отправлены пользователю как ответ на его запрос. @@ -11,30 +9,33 @@ import dev.struchkov.godfather.context.service.usercode.ProcessingData; */ public class BoxAnswer { + /** + * Клавиатура - меню. + */ + private final KeyBoard keyBoard; + + /** + * Флаг означающий, что надо перезаписать наше последнее отправленное сообщение, вместо отправки нового. + */ + private final boolean replace; + /** * Обычное текстовое сообщение. */ private String message; - /** - * Клавиатура - меню. - */ - private KeyBoard keyBoard; - - private boolean replace; - private BoxAnswer(Builder builder) { message = builder.message; keyBoard = builder.keyBoard; replace = builder.replace; } - public static BoxAnswer of(String message) { + public static BoxAnswer boxAnswer(String message) { return BoxAnswer.builder().message(message).build(); } - public static ProcessingData boxAnswer(String messageText) { - return message -> of(messageText); + public static BoxAnswer boxAnswer(String messageText, KeyBoard keyBoard) { + return BoxAnswer.builder().message(messageText).keyBoard(keyBoard).build(); } public static Builder builder() { @@ -66,7 +67,6 @@ public class BoxAnswer { '}'; } - public static final class Builder { private String message; private KeyBoard keyBoard; @@ -94,4 +94,5 @@ public class BoxAnswer { return new BoxAnswer(this); } } + } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/button/SimpleButton.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/button/SimpleButton.java index bd98769..eba5ff8 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/button/SimpleButton.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/button/SimpleButton.java @@ -28,11 +28,11 @@ public class SimpleButton implements KeyBoardButton { this.callbackData = callbackData; } - public static SimpleButton of(@NotNull String label, @NotNull String callbackData) { + public static SimpleButton simpleButton(@NotNull String label, @NotNull String callbackData) { return new SimpleButton(label, callbackData); } - public static SimpleButton of(@NotNull String label) { + public static SimpleButton simpleButton(@NotNull String label) { return new SimpleButton(label, label); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoard.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoard.java index fc39d17..8f737d4 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoard.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoard.java @@ -29,7 +29,7 @@ public class SimpleKeyBoard implements KeyBoard { lines = builder.lines; } - public static SimpleKeyBoard single(KeyBoardLine line) { + public static SimpleKeyBoard simpleKeyboard(KeyBoardLine line) { return new SimpleKeyBoard(List.of(line)); } @@ -37,8 +37,8 @@ public class SimpleKeyBoard implements KeyBoard { return new Builder(); } - public SimpleKeyBoard single(KeyBoardButton keyBoardButton) { - return single(SimpleKeyBoardLine.single(keyBoardButton)); + public SimpleKeyBoard simpleKeyboard(KeyBoardButton keyBoardButton) { + return simpleKeyboard(SimpleKeyBoardLine.simpleLine(keyBoardButton)); } @Override diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoardLine.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoardLine.java index b55a0fe..e5c599f 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoardLine.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoardLine.java @@ -26,7 +26,7 @@ public class SimpleKeyBoardLine implements KeyBoardLine { buttons = builder.buttons; } - public static SimpleKeyBoardLine single(KeyBoardButton keyBoardButton) { + public static SimpleKeyBoardLine simpleLine(KeyBoardButton keyBoardButton) { return new SimpleKeyBoardLine(List.of(keyBoardButton)); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/MessageFunction.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/MessageFunction.java new file mode 100644 index 0000000..2508b6a --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/MessageFunction.java @@ -0,0 +1,10 @@ +package dev.struchkov.godfather.context.service.usercode; + +import dev.struchkov.godfather.context.domain.BoxAnswer; +import dev.struchkov.godfather.context.domain.content.Message; + +public interface MessageFunction { + + void build(M message, BoxAnswer.Builder builder); + +} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/utils/KeyBoards.java b/bot-context/src/main/java/dev/struchkov/godfather/context/utils/KeyBoards.java index a163c4f..178c0f8 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/utils/KeyBoards.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/utils/KeyBoards.java @@ -14,8 +14,8 @@ import java.util.List; */ public class KeyBoards { - public static final SimpleButton YES_BUTTON = SimpleButton.of("Да", "{\"button\": \"yes\"}"); - public static final SimpleButton NO_BUTTON = SimpleButton.of("Нет", "{\"button\": \"no\"}"); + public static final SimpleButton YES_BUTTON = SimpleButton.simpleButton("Да", "{\"button\": \"yes\"}"); + public static final SimpleButton NO_BUTTON = SimpleButton.simpleButton("Нет", "{\"button\": \"no\"}"); private KeyBoards() { throw new IllegalStateException(); @@ -41,7 +41,7 @@ public class KeyBoards { public static SimpleKeyBoard verticalMenuString(List labelButtons) { final SimpleKeyBoard.Builder keyBoard = SimpleKeyBoard.build(); for (String labelButton : labelButtons) { - final SimpleButton simpleButton = SimpleButton.of(labelButton, "{\"button\": \"" + labelButton + "\"}"); + final SimpleButton simpleButton = SimpleButton.simpleButton(labelButton, "{\"button\": \"" + labelButton + "\"}"); keyBoard.line(SimpleKeyBoardLine.builder().button(simpleButton).build()); } return keyBoard.build(); @@ -79,7 +79,7 @@ public class KeyBoards { SimpleKeyBoardLine.Builder keyBoardLine = SimpleKeyBoardLine.builder(); for (int i = 0; i <= labelButton.size() - 1; i++) { String label = labelButton.get(i); - keyBoardLine.button(SimpleButton.of(label)); + keyBoardLine.button(SimpleButton.simpleButton(label)); if (flag) { if (i == labelButton.size() - 1) { keyBoard.line(keyBoardLine.build()); diff --git a/bot-core/pom.xml b/bot-core/pom.xml index 6f20ead..2e67d3f 100644 --- a/bot-core/pom.xml +++ b/bot-core/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.5 + 0.0.6 bot-core diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java b/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java index b3c8e40..c307260 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java @@ -61,7 +61,7 @@ public class GeneralAutoResponder extends TimerTask { this.modifiables = modifiables; } - protected void initActionUnit(String typeUnit, ActionUnit actionUnit) { + public void initActionUnit(String typeUnit, ActionUnit actionUnit) { if (!actionUnitMap.containsKey(typeUnit)) { actionUnitMap.put(typeUnit, actionUnit); } else { diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerText.java b/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerText.java index e31220c..4d8340b 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerText.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerText.java @@ -5,11 +5,13 @@ import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.exception.UnitConfigException; import dev.struchkov.godfather.context.service.sender.Sending; import dev.struchkov.godfather.context.service.usercode.Insert; +import dev.struchkov.godfather.context.service.usercode.MessageFunction; import dev.struchkov.godfather.context.service.usercode.ProcessingData; import dev.struchkov.godfather.core.utils.TypeUnit; import java.util.HashSet; import java.util.Set; +import java.util.function.Consumer; import java.util.regex.Pattern; import static dev.struchkov.haiti.utils.Inspector.isNotNull; @@ -29,12 +31,12 @@ public class AnswerText extends MainUnit { /** * Информация, которую необходимо вставить вместо маркеров в строку ответа. */ - private Insert insert; + private final Insert insert; /** * Объект нестандартной отправки ответа. */ - private Sending sending; + private final Sending sending; private AnswerText(Builder builder) { super(builder.keyWords, builder.phrase, builder.pattern, builder.matchThreshold, builder.priority, builder.nextUnits, builder.activeType, TypeUnit.TEXT); @@ -76,11 +78,31 @@ public class AnswerText extends MainUnit { private UnitActiveType activeType; private Builder() { - } - public Builder boxAnswer(ProcessingData boxAnswer) { - this.boxAnswer = boxAnswer; + public Builder message(ProcessingData message) { + this.boxAnswer = message; + return this; + } + + public Builder message(MessageFunction function) { + this.boxAnswer = message -> { + final BoxAnswer.Builder builder = BoxAnswer.builder(); + function.build(message, builder); + return builder.build(); + }; + return this; + } + + public Builder boxAnswer(Consumer boxAnswer) { + final BoxAnswer.Builder boxAnswerBuilder = BoxAnswer.builder(); + boxAnswer.accept(boxAnswerBuilder); + this.boxAnswer = message -> boxAnswerBuilder.build(); + return this; + } + + public Builder boxAnswer(BoxAnswer boxAnswer) { + this.boxAnswer = message -> boxAnswer; return this; } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java index 702957d..01b7e99 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java @@ -40,7 +40,7 @@ public class AnswerValidityAction implements ActionUnit .clearKeyWords().keyWords(WORDS_YES_NO) .build(); return AnswerText.builder() - .boxAnswer(mes -> clarification.getQuestion()) + .message(mes -> clarification.getQuestion()) .nextUnit(newValidity) .build(); } diff --git a/pom.xml b/pom.xml index 45f9e1c..a96df17 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.5 + 0.0.6 pom @@ -32,7 +32,7 @@ UTF-8 UTF-8 - 0.0.5 + 0.0.6 ${godfather.ver} ${godfather.ver}