Добавил поддержку клавиатуры в ответах

This commit is contained in:
Mark Struchkov 2019-05-15 13:17:29 +03:00
parent d8add9f109
commit 0eda787b44
6 changed files with 222 additions and 4 deletions

View File

@ -1,11 +1,13 @@
package org.sadtech.bot.core.domain; package org.sadtech.bot.core.domain;
import org.sadtech.bot.core.domain.keyboard.KeyBoard;
import java.util.Objects; import java.util.Objects;
public class BoxAnswer { public class BoxAnswer {
private String message; private String message;
private String keyboard; private KeyBoard keyboard;
private Float lat; private Float lat;
private Float aLong; private Float aLong;
private Integer stickerId; private Integer stickerId;
@ -33,11 +35,11 @@ public class BoxAnswer {
this.message = message; this.message = message;
} }
public String getKeyboard() { public KeyBoard getKeyboard() {
return keyboard; return keyboard;
} }
public void setKeyboard(String keyboard) { public void setKeyboard(KeyBoard keyboard) {
this.keyboard = keyboard; this.keyboard = keyboard;
} }
@ -83,7 +85,7 @@ public class BoxAnswer {
return this; return this;
} }
public Builder keyBoard(String keyBoard) { public Builder keyBoard(KeyBoard keyBoard) {
BoxAnswer.this.keyboard = keyBoard; BoxAnswer.this.keyboard = keyBoard;
return this; return this;
} }

View File

@ -0,0 +1,7 @@
package org.sadtech.bot.core.domain.keyboard;
public enum ButtonColor {
PRIMARY, DEFAULT, NEGATIVE, POSITIVE
}

View File

@ -0,0 +1,48 @@
package org.sadtech.bot.core.domain.keyboard;
import java.util.ArrayList;
import java.util.List;
public class KeyBoard {
private List<KeyBoardLine> keyBoardLines = new ArrayList<>();
private boolean oneTime = true;
private KeyBoard() {
}
public List<KeyBoardLine> 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;
}
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,41 @@
package org.sadtech.bot.core.domain.keyboard;
import java.util.ArrayList;
import java.util.List;
public class KeyBoardLine {
private List<KeyBoardButton> keyBoardButtons = new ArrayList<>();
public KeyBoardLine() {
}
public KeyBoardLine(List<KeyBoardButton> keyBoardButtons) {
this.keyBoardButtons = keyBoardButtons;
}
public List<KeyBoardButton> 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;
}
}
}

View File

@ -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<String> 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<KeyBoardButton> keyBoardButtons) {
KeyBoard.Builder keyBoard = KeyBoard.builder().oneTime(true);
for (KeyBoardButton keyBoardButton : keyBoardButtons) {
keyBoard.lineKeyBoard(KeyBoardLine.builder().setButtonKeyBoard(keyBoardButton).build());
}
return keyBoard.build();
}
}