From ccdc44d6890b6c22b296ac1724404233e6495245 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sun, 3 Feb 2019 20:33:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=82=D0=B8?= =?UTF-8?q?=D0=BF=20=D1=8E=D0=BD=D0=B8=D1=82=D0=BE=D0=B2=20-=20=D1=82?= =?UTF-8?q?=D0=B0=D0=B9=D0=BC=D0=B5=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Таймер срабатывает не сразу а через какое-то время, заставляет сработать любой другой тип юнита --- .../java/org/sadtech/vkbot/core/VkApi.java | 12 +++++ .../org/sadtech/vkbot/core/VkInsertData.java | 39 ++++++++------ .../sadtech/vkbot/core/entity/MailSend.java | 10 ---- .../core/keyboard/ButtonKeyBoardService.java | 7 --- .../vkbot/core/keyboard/KeyBoardService.java | 3 ++ .../core/keyboard/LineKeyBoardService.java | 4 -- .../vkbot/core/sender/MailSanderVk.java | 51 ++++++++++--------- .../vkbot/core/sender/MailSandler.java | 6 ++- 8 files changed, 72 insertions(+), 60 deletions(-) delete mode 100644 src/main/java/org/sadtech/vkbot/core/keyboard/ButtonKeyBoardService.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/keyboard/LineKeyBoardService.java diff --git a/src/main/java/org/sadtech/vkbot/core/VkApi.java b/src/main/java/org/sadtech/vkbot/core/VkApi.java index 3f7e219..7a0f84d 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkApi.java +++ b/src/main/java/org/sadtech/vkbot/core/VkApi.java @@ -51,4 +51,16 @@ public class VkApi { } return temp != null ? temp.get(0).getUniversities().get(0).getName() : null; } + + public String getUserCity(Integer id) { + List temp = null; + try { + temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(UserField.CITY).execute(); + } catch (ApiException | ClientException e) { + e.printStackTrace(); + } + return temp != null ? temp.get(0).getCity().getTitle() : null; + } + + } diff --git a/src/main/java/org/sadtech/vkbot/core/VkInsertData.java b/src/main/java/org/sadtech/vkbot/core/VkInsertData.java index fe11744..be36efc 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkInsertData.java +++ b/src/main/java/org/sadtech/vkbot/core/VkInsertData.java @@ -1,29 +1,38 @@ package org.sadtech.vkbot.core; +import org.sadtech.vkbot.core.entity.MailSend; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class VkInsertData { private VkApi vkApi; - private Integer userId; public VkInsertData(VkConnect vkConnect) { this.vkApi = new VkApi(vkConnect); } - public Integer getUserId() { - return userId; - } - - public void setUserId(Integer userId) { - this.userId = userId; - } - - public String insert(String key) { - String string = ""; - if (key.toLowerCase().equals("%firstname%")) { - string = vkApi.getUserMini(userId).getFirstName(); + public String insertWords(MailSend mailSend, Integer idUser) { + Pattern pattern = Pattern.compile("%(\\w+)%"); + Matcher m = pattern.matcher(mailSend.getMessage()); + StringBuffer result = new StringBuffer(); + while (m.find()) { + m.appendReplacement(result, insert(m.group(0), idUser)); } - if (key.toLowerCase().equals("%lastname%")) { - string = vkApi.getUserMini(userId).getLastName(); + m.appendTail(result); + return result.toString(); + } + + private String insert(String key, Integer userId) { + String string = ""; + switch (key) { + case "%firstname%": + string = vkApi.getUserMini(userId).getFirstName(); + break; + case "%lastname%": + string = vkApi.getUserMini(userId).getLastName(); + break; } return string; } diff --git a/src/main/java/org/sadtech/vkbot/core/entity/MailSend.java b/src/main/java/org/sadtech/vkbot/core/entity/MailSend.java index 4982ffb..999f9b0 100644 --- a/src/main/java/org/sadtech/vkbot/core/entity/MailSend.java +++ b/src/main/java/org/sadtech/vkbot/core/entity/MailSend.java @@ -4,7 +4,6 @@ public class MailSend { private String message; private String keyboard; - private Integer idRecipient; private Float lat; private Float aLong; private Integer stickerId; @@ -29,14 +28,6 @@ public class MailSend { this.message = message; } - public Integer getIdRecipient() { - return idRecipient; - } - - public void setIdRecipient(Integer idRecipient) { - this.idRecipient = idRecipient; - } - public Float getLat() { return lat; } @@ -66,7 +57,6 @@ public class MailSend { return "MailSend{" + "message='" + message + '\'' + ", keyboard='" + keyboard + '\'' + - ", idRecipient=" + idRecipient + ", lat=" + lat + ", aLong=" + aLong + ", stickerId=" + stickerId + diff --git a/src/main/java/org/sadtech/vkbot/core/keyboard/ButtonKeyBoardService.java b/src/main/java/org/sadtech/vkbot/core/keyboard/ButtonKeyBoardService.java deleted file mode 100644 index c021d5e..0000000 --- a/src/main/java/org/sadtech/vkbot/core/keyboard/ButtonKeyBoardService.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.sadtech.vkbot.core.keyboard; - -public interface ButtonKeyBoardService { - - - -} 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 8ac1824..ade5e6e 100644 --- a/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoardService.java +++ b/src/main/java/org/sadtech/vkbot/core/keyboard/KeyBoardService.java @@ -9,6 +9,7 @@ public class KeyBoardService { ButtonKeyBoard noButton = ButtonKeyBoard.builder().setColor(ColorButton.NEGATIVE).setLabel("Нет").setPayload("{\"button\": \"no\"}").build(); LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build(); KeyBoard keyBoard = KeyBoard.builder().setLineKeyBoard(lineKeyBoard).build(); + keyBoard.setOneTime(true); return keyBoard; } @@ -18,6 +19,7 @@ public class KeyBoardService { ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().setLabel(labelButton).setType("text").setPayload("{\"button\": \"" + labelButton + "\"}").build(); keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build()); } + keyBoard.setOneTime(true); return keyBoard; } @@ -26,6 +28,7 @@ public class KeyBoardService { for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) { keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build()); } + keyBoard.setOneTime(true); return keyBoard; } } diff --git a/src/main/java/org/sadtech/vkbot/core/keyboard/LineKeyBoardService.java b/src/main/java/org/sadtech/vkbot/core/keyboard/LineKeyBoardService.java deleted file mode 100644 index d36ed38..0000000 --- a/src/main/java/org/sadtech/vkbot/core/keyboard/LineKeyBoardService.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.sadtech.vkbot.core.keyboard; - -public interface LineKeyBoardService { -} 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 b484b22..85485fc 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java @@ -9,9 +9,9 @@ import org.apache.log4j.Logger; import org.sadtech.vkbot.core.VkConnect; import org.sadtech.vkbot.core.VkInsertData; import org.sadtech.vkbot.core.entity.MailSend; +import org.sadtech.vkbot.core.insert.InsertWords; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.List; public class MailSanderVk implements MailSandler { @@ -28,12 +28,8 @@ public class MailSanderVk implements MailSandler { this.vkInsertData = new VkInsertData(vkConnect); } - @Override - public void send(MailSend mailSend) { - MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient()); - if (mailSend.getMessage() != null) { - messages.message(insertWords(mailSend)); - } + private MessagesSendQuery createMessage (MailSend mailSend, Integer idUser) { + MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(idUser); if (mailSend.getKeyboard() != null) { messages.keyboard(mailSend.getKeyboard()); } else { @@ -44,30 +40,39 @@ public class MailSanderVk implements MailSandler { } if (mailSend.getStickerId() != null) { try { - vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient()).stickerId(mailSend.getStickerId()).execute(); + vkApiClient.messages().send(groupActor).peerId(idUser).stickerId(mailSend.getStickerId()).execute(); } catch (ApiException | ClientException e) { e.printStackTrace(); } } + return messages; + } + @Override + public void send(MailSend mailSend, Integer idUser) { + MessagesSendQuery messages = createMessage(mailSend, idUser); + if (mailSend.getMessage() != null) { + messages.message(vkInsertData.insertWords(mailSend, idUser)); + } + sendMessage(messages); + } + + public void send(MailSend mailSend, Integer idUser, List insertWords) { + MessagesSendQuery messages = createMessage(mailSend, idUser); + if (mailSend.getMessage() != null) { + InsertWords insert = new InsertWords(); + insert.setInText(vkInsertData.insertWords(mailSend, idUser)); + insert.insert(insertWords); + messages.message(insert.getOutText()); + } + sendMessage(messages); + } + + private void sendMessage(MessagesSendQuery messages) { try { - log.info(mailSend); messages.execute(); } catch (ApiException | ClientException e) { e.printStackTrace(); } } - - private String insertWords(MailSend mailSend) { - vkInsertData.setUserId(mailSend.getIdRecipient()); - - Pattern pattern = Pattern.compile("\\%(\\w+)%"); - Matcher m = pattern.matcher(mailSend.getMessage()); - StringBuffer result = new StringBuffer(); - while (m.find()) { - m.appendReplacement(result, vkInsertData.insert(m.group(0))); - } - m.appendTail(result); - return result.toString(); - } } diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java index 511cb76..e2998e1 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSandler.java @@ -2,8 +2,12 @@ package org.sadtech.vkbot.core.sender; import org.sadtech.vkbot.core.entity.MailSend; +import java.util.List; + public interface MailSandler { - void send(MailSend mailSend); + void send(MailSend mailSend, Integer idUser); + + void send(MailSend mailSend, Integer idUser, List insertWords); }