diff --git a/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java b/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java new file mode 100644 index 0000000..69a9677 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java @@ -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> listButton = new ArrayList<>(); + for (KeyBoardLine keyBoardLine : keyboard.getKeyBoardLines()) { + List 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; + } + +} 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 d2fb0b2..5a400cf 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java @@ -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();