diff --git a/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java b/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java index 9248802..3640594 100644 --- a/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java +++ b/src/main/java/org/sadtech/bot/core/domain/BoxAnswer.java @@ -1,11 +1,13 @@ package org.sadtech.bot.core.domain; +import org.sadtech.bot.core.domain.keyboard.KeyBoard; + import java.util.Objects; public class BoxAnswer { private String message; - private String keyboard; + private KeyBoard keyboard; private Float lat; private Float aLong; private Integer stickerId; @@ -33,11 +35,11 @@ public class BoxAnswer { this.message = message; } - public String getKeyboard() { + public KeyBoard getKeyboard() { return keyboard; } - public void setKeyboard(String keyboard) { + public void setKeyboard(KeyBoard keyboard) { this.keyboard = keyboard; } @@ -83,7 +85,7 @@ public class BoxAnswer { return this; } - public Builder keyBoard(String keyBoard) { + public Builder keyBoard(KeyBoard keyBoard) { BoxAnswer.this.keyboard = keyBoard; return this; } diff --git a/src/main/java/org/sadtech/bot/core/domain/keyboard/ButtonColor.java b/src/main/java/org/sadtech/bot/core/domain/keyboard/ButtonColor.java new file mode 100644 index 0000000..49263a2 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/keyboard/ButtonColor.java @@ -0,0 +1,7 @@ +package org.sadtech.bot.core.domain.keyboard; + +public enum ButtonColor { + + PRIMARY, DEFAULT, NEGATIVE, POSITIVE + +} diff --git a/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoard.java b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoard.java new file mode 100644 index 0000000..12dcf6e --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoard.java @@ -0,0 +1,48 @@ +package org.sadtech.bot.core.domain.keyboard; + +import java.util.ArrayList; +import java.util.List; + +public class KeyBoard { + + private List keyBoardLines = new ArrayList<>(); + private boolean oneTime = true; + + private KeyBoard() { + + } + + public List getKeyBoardLines() { + return keyBoardLines; + } + + public boolean isOneTime() { + return oneTime; + } + + public static Builder builder() { + return new KeyBoard().new Builder(); + } + + public class Builder { + + private Builder() { + + } + + public Builder lineKeyBoard(KeyBoardLine keyBoardLine) { + KeyBoard.this.keyBoardLines.add(keyBoardLine); + return this; + } + + public Builder oneTime(boolean oneTime) { + KeyBoard.this.oneTime = oneTime; + return this; + } + + public KeyBoard build() { + return KeyBoard.this; + } + + } +} diff --git a/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardButton.java b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardButton.java new file mode 100644 index 0000000..f59dff1 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardButton.java @@ -0,0 +1,81 @@ +package org.sadtech.bot.core.domain.keyboard; + +import com.google.gson.JsonObject; + +import java.util.Objects; + +public class KeyBoardButton { + + private String payload; + private String label; + private ButtonColor color = ButtonColor.DEFAULT; + + public KeyBoardButton() { + + } + + public String getPayload() { + return payload; + } + + public String getLabel() { + return label; + } + + public ButtonColor getColor() { + return color; + } + + private JsonObject generateAction() { + JsonObject action = new JsonObject(); + action.addProperty("payload", payload); + action.addProperty("label", label); + return action; + } + + public static Builder builder() { + return new KeyBoardButton().new Builder(); + } + + public class Builder { + + private Builder() { + + } + + public Builder color(ButtonColor color) { + KeyBoardButton.this.color = color; + return this; + } + + public Builder label(String label) { + KeyBoardButton.this.label = label; + return this; + } + + public Builder payload(String payload) { + KeyBoardButton.this.payload = payload; + return this; + } + + public KeyBoardButton build() { + return KeyBoardButton.this; + } + + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof KeyBoardButton)) return false; + KeyBoardButton that = (KeyBoardButton) o; + return Objects.equals(payload, that.payload) && + Objects.equals(label, that.label) && + color == that.color; + } + + @Override + public int hashCode() { + return Objects.hash(payload, label, color); + } +} diff --git a/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardLine.java b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardLine.java new file mode 100644 index 0000000..236ccc9 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/domain/keyboard/KeyBoardLine.java @@ -0,0 +1,41 @@ +package org.sadtech.bot.core.domain.keyboard; + +import java.util.ArrayList; +import java.util.List; + +public class KeyBoardLine { + + private List keyBoardButtons = new ArrayList<>(); + + public KeyBoardLine() { + + } + + public KeyBoardLine(List keyBoardButtons) { + this.keyBoardButtons = keyBoardButtons; + } + + public List getKeyBoardButtons() { + return keyBoardButtons; + } + + public static Builder builder() { + return new KeyBoardLine().new Builder(); + } + + public class Builder { + + private Builder() { + + } + + public Builder setButtonKeyBoard(KeyBoardButton keyBoardButton) { + KeyBoardLine.this.keyBoardButtons.add(keyBoardButton); + return this; + } + + public KeyBoardLine build() { + return KeyBoardLine.this; + } + } +} diff --git a/src/main/java/org/sadtech/bot/core/service/KeyBoards.java b/src/main/java/org/sadtech/bot/core/service/KeyBoards.java new file mode 100644 index 0000000..a9b54d7 --- /dev/null +++ b/src/main/java/org/sadtech/bot/core/service/KeyBoards.java @@ -0,0 +1,39 @@ +package org.sadtech.bot.core.service; + +import org.sadtech.bot.core.domain.keyboard.ButtonColor; +import org.sadtech.bot.core.domain.keyboard.KeyBoard; +import org.sadtech.bot.core.domain.keyboard.KeyBoardButton; +import org.sadtech.bot.core.domain.keyboard.KeyBoardLine; + +import java.util.List; + +public class KeyBoards { + + private KeyBoards() { + throw new IllegalStateException(); + } + + public static KeyBoard keyBoardYesNo() { + KeyBoardButton yesButton = KeyBoardButton.builder().color(ButtonColor.POSITIVE).label("Да").payload("{\"button\": \"yes\"}").build(); + KeyBoardButton noButton = KeyBoardButton.builder().color(ButtonColor.NEGATIVE).label("Нет").payload("{\"button\": \"no\"}").build(); + KeyBoardLine keyBoardLine = KeyBoardLine.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build(); + return KeyBoard.builder().lineKeyBoard(keyBoardLine).oneTime(true).build(); + } + + public static KeyBoard verticalMenuString(List labelButtons) { + KeyBoard.Builder keyBoard = KeyBoard.builder().oneTime(true); + for (String labelButton : labelButtons) { + KeyBoardButton keyBoardButton = KeyBoardButton.builder().label(labelButton).payload("{\"button\": \"" + labelButton + "\"}").build(); + keyBoard.lineKeyBoard(KeyBoardLine.builder().setButtonKeyBoard(keyBoardButton).build()); + } + return keyBoard.build(); + } + + public static KeyBoard verticalMenuButton(List keyBoardButtons) { + KeyBoard.Builder keyBoard = KeyBoard.builder().oneTime(true); + for (KeyBoardButton keyBoardButton : keyBoardButtons) { + keyBoard.lineKeyBoard(KeyBoardLine.builder().setButtonKeyBoard(keyBoardButton).build()); + } + return keyBoard.build(); + } +}