From 2ebbe9356dfdc5b969c7a378ab4a8d56b47f7238 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Wed, 15 May 2019 13:22:23 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BE=D1=81=D0=BC?= =?UTF-8?q?=D1=8B=D1=81=D0=BB=D0=B8=D0=BB=20=D0=BA=D0=BB=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=B0=D1=82=D1=83=D1=80=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vkbot/core/convert/KeyBoardConvert.java | 70 +++++++++++++++++++ .../vkbot/core/sender/MailSenderVk.java | 15 +++- 2 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java 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();