From 0a960d6972422620ba3dca6c51f5071f41829e4b Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Mon, 13 May 2019 21:14:08 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=B2=D0=B5=D0=BB=20=D0=BA?= =?UTF-8?q?=20=D0=BD=D0=BE=D0=B2=D0=BE=D0=B9=20=D0=B2=D0=B5=D1=80=D1=81?= =?UTF-8?q?=D0=B8=D0=B8=20SDK=20VK=201.0.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vkbot/core/keyboard/ButtonKeyBoard.java | 67 ------------------- .../vkbot/core/keyboard/ColorButton.java | 7 -- .../sadtech/vkbot/core/keyboard/KeyBoard.java | 60 ----------------- .../vkbot/core/keyboard/KeyBoardService.java | 60 ++++++++++++----- .../vkbot/core/keyboard/LineKeyBoard.java | 47 ------------- .../vkbot/core/sender/MailSenderVk.java | 10 +-- 6 files changed, 50 insertions(+), 201 deletions(-) delete mode 100644 src/main/java/org/sadtech/vkbot/core/keyboard/ButtonKeyBoard.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/keyboard/ColorButton.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoard.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/keyboard/LineKeyBoard.java diff --git a/src/main/java/org/sadtech/vkbot/core/keyboard/ButtonKeyBoard.java b/src/main/java/org/sadtech/vkbot/core/keyboard/ButtonKeyBoard.java deleted file mode 100644 index 77b6ed1..0000000 --- a/src/main/java/org/sadtech/vkbot/core/keyboard/ButtonKeyBoard.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.sadtech.vkbot.core.keyboard; - -import com.google.gson.JsonObject; - -public class ButtonKeyBoard { - - private String type = "text"; - private String payload; - private String label; - private ColorButton color = ColorButton.DEFAULT; - - public ButtonKeyBoard() { - - } - - public JsonObject getButton() { - JsonObject newButton = new JsonObject(); - newButton.addProperty("color", color.toString().toLowerCase()); - newButton.add("action", generateAction()); - return newButton; - } - - private JsonObject generateAction() { - JsonObject action = new JsonObject(); - action.addProperty("type", type); - action.addProperty("payload", payload); - action.addProperty("label", label); - return action; - } - - public static Builder builder() { - return new ButtonKeyBoard().new Builder(); - } - - public class Builder { - - private Builder() { - - } - - public Builder color(ColorButton color) { - ButtonKeyBoard.this.color = color; - return this; - } - - public Builder label(String label) { - ButtonKeyBoard.this.label = label; - return this; - } - - public Builder payload(String payload) { - ButtonKeyBoard.this.payload = payload; - return this; - } - - public Builder type(String type) { - ButtonKeyBoard.this.type = type; - return this; - } - - public ButtonKeyBoard build() { - return ButtonKeyBoard.this; - } - - } - -} diff --git a/src/main/java/org/sadtech/vkbot/core/keyboard/ColorButton.java b/src/main/java/org/sadtech/vkbot/core/keyboard/ColorButton.java deleted file mode 100644 index 6eb856b..0000000 --- a/src/main/java/org/sadtech/vkbot/core/keyboard/ColorButton.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.sadtech.vkbot.core.keyboard; - -public enum ColorButton { - - PRIMARY, DEFAULT, NEGATIVE, POSITIVE - -} diff --git a/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoard.java b/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoard.java deleted file mode 100644 index d139675..0000000 --- a/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoard.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.sadtech.vkbot.core.keyboard; - -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - -import java.util.ArrayList; -import java.util.List; - -public class KeyBoard { - - private List lineKeyBoards = new ArrayList<>(); - private boolean oneTime = true; - - public KeyBoard() { - - } - - public JsonObject getKeyboard() { - JsonObject keyboard = new JsonObject(); - keyboard.addProperty("one_time", oneTime); - - JsonArray menuLine = new JsonArray(); - for (LineKeyBoard lineKeyboard : lineKeyBoards) { - menuLine.add(lineKeyboard.getLine()); - } - - keyboard.add("buttons", menuLine); - return keyboard; - } - - public static Builder builder() { - return new KeyBoard().new Builder(); - } - - public void addLine(LineKeyBoard lineKeyBoard) { - lineKeyBoards.add(lineKeyBoard); - } - - public class Builder { - - private Builder() { - - } - - public Builder lineKeyBoard(LineKeyBoard lineKeyBoard) { - KeyBoard.this.lineKeyBoards.add(lineKeyBoard); - 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/vkbot/core/keyboard/KeyBoardService.java b/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoardService.java index 11f70b1..dcecd16 100644 --- a/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoardService.java +++ b/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoardService.java @@ -1,5 +1,10 @@ package org.sadtech.vkbot.core.keyboard; +import com.vk.api.sdk.objects.messages.*; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; public class KeyBoardService { @@ -8,27 +13,50 @@ public class KeyBoardService { throw new IllegalStateException(); } - public static KeyBoard keyBoardYesNo() { - ButtonKeyBoard yesButton = ButtonKeyBoard.builder().color(ColorButton.POSITIVE).label("Да").payload("{\"button\": \"yes\"}").build(); - ButtonKeyBoard noButton = ButtonKeyBoard.builder().color(ColorButton.NEGATIVE).label("Нет").payload("{\"button\": \"no\"}").build(); - LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build(); - return KeyBoard.builder().lineKeyBoard(lineKeyBoard).oneTime(true).build(); + public static Keyboard keyBoardYesNo() { + KeyboardButton yesButton = new KeyboardButton(); + KeyboardButtonAction buttonActionYes = new KeyboardButtonAction(); + buttonActionYes.setLabel("Да"); + buttonActionYes.setType(KeyboardButtonActionType.TEXT); + buttonActionYes.setPayload("{\"button\": \"yes\"}"); + yesButton.setAction(buttonActionYes); + yesButton.setColor(KeyboardButtonColor.POSITIVE); + + KeyboardButton noButton = new KeyboardButton(); + KeyboardButtonAction buttonActionNo = new KeyboardButtonAction(); + buttonActionNo.setLabel("Да"); + buttonActionNo.setType(KeyboardButtonActionType.TEXT); + buttonActionNo.setPayload("{\"button\": \"no\"}"); + noButton.setAction(buttonActionNo); + noButton.setColor(KeyboardButtonColor.NEGATIVE); + + List line1 = new ArrayList<>(); + line1.add(yesButton); + line1.add(noButton); + + Keyboard keyboard = new Keyboard(); + keyboard.setButtons(Collections.singletonList(line1)); + keyboard.setOneTime(true); + return keyboard; } - public static KeyBoard verticalMenuString(List labelButtons) { - KeyBoard keyBoard = KeyBoard.builder().oneTime(true).build(); + public static Keyboard verticalKeyboard(List labelButtons) { + Keyboard keyBoard = new Keyboard(); + keyBoard.setOneTime(true); + List menu = new ArrayList<>(); for (String labelButton : labelButtons) { - ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().label(labelButton).type("text").payload("{\"button\": \"" + labelButton + "\"}").build(); - keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build()); - } - return keyBoard; - } + KeyboardButton button = new KeyboardButton(); + button.setColor(KeyboardButtonColor.DEFAULT); - public static KeyBoard verticalMenuButton(List buttonKeyBoards) { - KeyBoard keyBoard = KeyBoard.builder().oneTime(true).build(); - for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) { - keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build()); + KeyboardButtonAction buttonAction = new KeyboardButtonAction(); + buttonAction.setPayload("{\"button\": \"" + labelButton + "\"}"); + buttonAction.setType(KeyboardButtonActionType.TEXT); + buttonAction.setLabel(labelButton); + + button.setAction(buttonAction); + menu.add(button); } + keyBoard.setButtons(Collections.singletonList(menu)); return keyBoard; } } diff --git a/src/main/java/org/sadtech/vkbot/core/keyboard/LineKeyBoard.java b/src/main/java/org/sadtech/vkbot/core/keyboard/LineKeyBoard.java deleted file mode 100644 index 6b28849..0000000 --- a/src/main/java/org/sadtech/vkbot/core/keyboard/LineKeyBoard.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.sadtech.vkbot.core.keyboard; - -import com.google.gson.JsonArray; - -import java.util.ArrayList; -import java.util.List; - -public class LineKeyBoard { - - private List buttonKeyBoards = new ArrayList<>(); - - public LineKeyBoard() { - - } - - public LineKeyBoard(List buttonKeyBoards) { - this.buttonKeyBoards = buttonKeyBoards; - } - - public JsonArray getLine() { - JsonArray line = new JsonArray(); - for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) { - line.add(buttonKeyBoard.getButton()); - } - return line; - } - - public static Builder builder() { - return new LineKeyBoard().new Builder(); - } - - public class Builder { - - private Builder() { - - } - - public Builder setButtonKeyBoard(ButtonKeyBoard buttonKeyBoard) { - LineKeyBoard.this.buttonKeyBoards.add(buttonKeyBoard); - return this; - } - - public LineKeyBoard build() { - return LineKeyBoard.this; - } - } -} diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java index 1ea63a4..d2fb0b2 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java @@ -1,9 +1,11 @@ package org.sadtech.vkbot.core.sender; +import com.google.gson.Gson; import com.vk.api.sdk.client.VkApiClient; import com.vk.api.sdk.client.actors.GroupActor; import com.vk.api.sdk.exceptions.ApiException; import com.vk.api.sdk.exceptions.ClientException; +import com.vk.api.sdk.objects.messages.Keyboard; import com.vk.api.sdk.queries.messages.MessagesSendQuery; import org.apache.log4j.Logger; import org.sadtech.bot.core.domain.BoxAnswer; @@ -21,6 +23,7 @@ public class MailSenderVk implements Sent { private final GroupActor groupActor; private final VkInsertData vkInsertData; + private final Gson gson = new Gson(); public MailSenderVk(VkConnect vkConnect) { this.vkApiClient = vkConnect.getVkApiClient(); @@ -43,7 +46,8 @@ public class MailSenderVk implements Sent { private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Integer peerId) { MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId).message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId)).randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE)); if (boxAnswer.getKeyboard() != null) { -// messages.keyboard(boxAnswer.getKeyboard()); + Keyboard keyBoard = gson.fromJson(boxAnswer.getKeyboard(), Keyboard.class); + messages.keyboard(keyBoard); } else { // messages.keyboard("{\"buttons\":[],\"one_time\":true}"); } @@ -68,7 +72,5 @@ public class MailSenderVk implements Sent { } } - private Integer reandomId() { - return null; - } + }