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 + + '}'; + } }