Привел к новой версии SDK VK 1.0.2
This commit is contained in:
parent
aab0f6b075
commit
0a960d6972
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package org.sadtech.vkbot.core.keyboard;
|
||||
|
||||
public enum ColorButton {
|
||||
|
||||
PRIMARY, DEFAULT, NEGATIVE, POSITIVE
|
||||
|
||||
}
|
@ -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<LineKeyBoard> 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -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<KeyboardButton> 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<String> labelButtons) {
|
||||
KeyBoard keyBoard = KeyBoard.builder().oneTime(true).build();
|
||||
public static Keyboard verticalKeyboard(List<String> labelButtons) {
|
||||
Keyboard keyBoard = new Keyboard();
|
||||
keyBoard.setOneTime(true);
|
||||
List<KeyboardButton> 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<ButtonKeyBoard> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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<ButtonKeyBoard> buttonKeyBoards = new ArrayList<>();
|
||||
|
||||
public LineKeyBoard() {
|
||||
|
||||
}
|
||||
|
||||
public LineKeyBoard(List<ButtonKeyBoard> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user