Переосмыслил клавиатуру

This commit is contained in:
Mark Struchkov 2019-05-15 13:22:23 +03:00
parent 9026f50020
commit 2ebbe9356d
2 changed files with 82 additions and 3 deletions

View File

@ -0,0 +1,70 @@
package org.sadtech.vkbot.core.convert;
import com.vk.api.sdk.objects.messages.*;
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.ArrayList;
import java.util.List;
public final class KeyBoardConvert {
private KeyBoardConvert() {
}
public static Keyboard convertKeyboard(KeyBoard keyboard) {
Keyboard keyboardVk = new Keyboard();
keyboardVk.setOneTime(keyboard.isOneTime());
List<List<KeyboardButton>> listButton = new ArrayList<>();
for (KeyBoardLine keyBoardLine : keyboard.getKeyBoardLines()) {
List<KeyboardButton> buttonList = new ArrayList<>();
for (KeyBoardButton button : keyBoardLine.getKeyBoardButtons()) {
buttonList.add(convertButton(button));
}
listButton.add(buttonList);
}
keyboardVk.setButtons(listButton);
return keyboardVk;
}
private static KeyboardButton convertButton(KeyBoardButton button) {
KeyboardButton buttonVk = new KeyboardButton();
buttonVk.setColor(convertColor(button.getColor()));
KeyboardButtonAction buttonActionVk = new KeyboardButtonAction();
buttonActionVk.setLabel(button.getLabel());
buttonActionVk.setType(KeyboardButtonActionType.TEXT);
buttonActionVk.setPayload(button.getPayload());
buttonVk.setAction(buttonActionVk);
return buttonVk;
}
private static KeyboardButtonColor convertColor(ButtonColor color) {
KeyboardButtonColor buttonColorVk;
switch (color) {
case DEFAULT:
buttonColorVk = KeyboardButtonColor.DEFAULT;
break;
case PRIMARY:
buttonColorVk = KeyboardButtonColor.PRIMARY;
break;
case NEGATIVE:
buttonColorVk = KeyboardButtonColor.NEGATIVE;
break;
case POSITIVE:
buttonColorVk = KeyboardButtonColor.POSITIVE;
break;
default:
buttonColorVk = KeyboardButtonColor.DEFAULT;
}
return buttonColorVk;
}
}

View File

@ -5,14 +5,21 @@ 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.objects.messages.*;
import com.vk.api.sdk.queries.messages.MessagesSendQuery;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.domain.BoxAnswer;
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 org.sadtech.bot.core.sender.Sent;
import org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.VkInsertData;
import org.sadtech.vkbot.core.convert.KeyBoardConvert;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class MailSenderVk implements Sent {
@ -46,8 +53,7 @@ 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) {
Keyboard keyBoard = gson.fromJson(boxAnswer.getKeyboard(), Keyboard.class);
messages.keyboard(keyBoard);
messages.keyboard(KeyBoardConvert.convertKeyboard(boxAnswer.getKeyboard()));
} else {
// messages.keyboard("{\"buttons\":[],\"one_time\":true}");
}
@ -64,6 +70,9 @@ public class MailSenderVk implements Sent {
return messages;
}
private void sendMessage(MessagesSendQuery messages) {
try {
messages.execute();