From ac50183423b29b200b29c1dfe2a9383620ae6d83 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Wed, 30 Jan 2019 12:35:54 +0300 Subject: [PATCH] =?UTF-8?q?*=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20KeyBoardService?= =?UTF-8?q?.=20=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B2=D1=81=D0=B5?= =?UTF-8?q?=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D1=82=D0=B8=D1=87=D0=BD=D1=8B=D0=B5=20*=20=D0=94=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9?= =?UTF-8?q?=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D0=B4=D0=BB=D1=8F=20=D0=B2?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=B2=D0=BA=D0=B8=20=D1=81=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=20=D0=B8=D0=B7=20List=20=D0=B2=20=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D1=83=20*=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=81=D1=82=D1=80=D1=83=D0=B3=D1=82=D1=83=D1=80=D0=B0?= =?UTF-8?q?=20Unit.=20=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BE=D0=BD?= =?UTF-8?q?=D0=B8=20=D0=BD=D0=B5=20=D1=85=D1=80=D0=B0=D0=BD=D1=8F=D1=82=20?= =?UTF-8?q?MailSend,=20=D0=BE=D0=BD=D0=B8=20=D1=85=D1=80=D0=B0=D0=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/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vkbot/core/insert/InsertWords.java | 36 +++++++++++++++++++ .../vkbot/core/keyboard/KeyBoardService.java | 14 ++++---- .../vkbot/core/sender/MailSanderVk.java | 13 ++++--- .../sadtech/vkbot/core/sender/MailSend.java | 12 +++++++ 4 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/sadtech/vkbot/core/insert/InsertWords.java diff --git a/src/main/java/org/sadtech/vkbot/core/insert/InsertWords.java b/src/main/java/org/sadtech/vkbot/core/insert/InsertWords.java new file mode 100644 index 0000000..f8c23e7 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/insert/InsertWords.java @@ -0,0 +1,36 @@ +package org.sadtech.vkbot.core.insert; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class InsertWords { + + private String inText; + private String outText; + + public void insert(List words) { + Pattern pattern = Pattern.compile("\\{(\\d+)}"); // Задаем шаблон + Matcher m = pattern.matcher(inText); // Инициализация Matcher + StringBuffer result = new StringBuffer(); // Буфер для конечного значения + while (m.find()) { // Проверка на совпадение + if (Integer.parseInt(m.group(1)) < words.size()) { + m.appendReplacement(result, words.get(Integer.parseInt(m.group(1)))); // Подставляем значение из HashMap + } else { + m.appendReplacement(result, m.group(0)); + } + } + m.appendTail(result); // Добавить остаток строки + outText = result.toString(); + } + + public void setInText(String inText) { + this.inText = inText; + } + + public String getOutText() { + return outText; + } +} + + 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 fc77787..8ac1824 100644 --- a/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoardService.java +++ b/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoardService.java @@ -4,9 +4,7 @@ import java.util.List; public class KeyBoardService { - private KeyBoard keyBoardYesNo = generateKeyBoardYesNo(); - - private KeyBoard generateKeyBoardYesNo() { + public static KeyBoard keyBoardYesNo() { ButtonKeyBoard yesButton = ButtonKeyBoard.builder().setColor(ColorButton.POSITIVE).setLabel("Да").setPayload("{\"button\": \"yes\"}").build(); ButtonKeyBoard noButton = ButtonKeyBoard.builder().setColor(ColorButton.NEGATIVE).setLabel("Нет").setPayload("{\"button\": \"no\"}").build(); LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build(); @@ -14,7 +12,7 @@ public class KeyBoardService { return keyBoard; } - public static KeyBoard verticalMenu(List labelButtons) { + public static KeyBoard verticalMenuString(List labelButtons) { KeyBoard keyBoard = new KeyBoard(); for (String labelButton : labelButtons) { ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().setLabel(labelButton).setType("text").setPayload("{\"button\": \"" + labelButton + "\"}").build(); @@ -23,7 +21,11 @@ public class KeyBoardService { return keyBoard; } - public KeyBoard getKeyBoardYesNo() { - return keyBoardYesNo; + public static KeyBoard verticalMenuButton(List buttonKeyBoards) { + KeyBoard keyBoard = new KeyBoard(); + for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) { + keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build()); + } + return keyBoard; } } diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java index 41e509b..ba2de4e 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java @@ -5,10 +5,13 @@ 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.queries.messages.MessagesSendQuery; +import org.apache.log4j.Logger; import org.sadtech.vkbot.core.VkConnect; public class MailSanderVk implements MailSandler { + public static final Logger log = Logger.getLogger(MailSanderVk.class); + private VkApiClient vkApiClient; private GroupActor groupActor; @@ -16,21 +19,22 @@ public class MailSanderVk implements MailSandler { this.vkApiClient = vkConnect.getVkApiClient(); this.groupActor = vkConnect.getGroupActor(); } + @Override public void send(MailSend mailSend) { MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient()); - if (mailSend.getMessage()!=null) { + if (mailSend.getMessage() != null) { messages.message(mailSend.getMessage()); } - if (mailSend.getKeyboard()!=null) { + if (mailSend.getKeyboard() != null) { messages.keyboard(mailSend.getKeyboard()); } else { messages.keyboard("{\"buttons\":[],\"one_time\":true}"); } - if (mailSend.getLat()!=null && mailSend.getaLong()!=null) { + if (mailSend.getLat() != null && mailSend.getaLong() != null) { messages.lat(mailSend.getLat()).lng(mailSend.getaLong()); } - if (mailSend.getStickerId()!=null) { + if (mailSend.getStickerId() != null) { try { vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient()).stickerId(mailSend.getStickerId()).execute(); } catch (ApiException | ClientException e) { @@ -39,6 +43,7 @@ public class MailSanderVk implements MailSandler { } try { + log.info(mailSend); messages.execute(); } catch (ApiException | ClientException e) { e.printStackTrace(); diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSend.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSend.java index 75d5462..d954f60 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSend.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSend.java @@ -60,4 +60,16 @@ public class MailSend { public void setStickerId(Integer stickerId) { this.stickerId = stickerId; } + + @Override + public String toString() { + return "MailSend{" + + "message='" + message + '\'' + + ", keyboard='" + keyboard + '\'' + + ", idRecipient=" + idRecipient + + ", lat=" + lat + + ", aLong=" + aLong + + ", stickerId=" + stickerId + + '}'; + } }