From 57ac698715db6782149f4a1b3cfba669ce84fec2 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Thu, 21 Feb 2019 21:27:38 +0300 Subject: [PATCH] =?UTF-8?q?=D0=90=D1=80=D1=85=D0=B8=D1=82=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D1=83=D1=80=D0=BD=D1=8B=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 17 +++++ .../sadtech/vkbot/core/entity/BoxAnswer.java | 69 +++++++++++++++++++ .../vkbot/core/insert/VkInsertData.java | 5 +- .../vkbot/core/listener/EventListenerVk.java | 5 +- .../vkbot/core/sender/MailSenderVk.java | 56 ++++++--------- .../sadtech/vkbot/core/sender/MailSent.java | 13 ---- .../org/sadtech/vkbot/core/sender/Sent.java | 11 +++ .../distribution/impl/MailServiceImpl.java | 1 - 8 files changed, 124 insertions(+), 53 deletions(-) create mode 100644 src/main/java/org/sadtech/vkbot/core/entity/BoxAnswer.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/sender/MailSent.java create mode 100644 src/main/java/org/sadtech/vkbot/core/sender/Sent.java diff --git a/pom.xml b/pom.xml index d43159e..d871895 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,23 @@ log4j ${log4j.ver} + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.7 + + + + org.apache.logging.log4j + log4j-api + 2.7 + + + org.apache.logging.log4j + log4j-core + 2.7 + junit junit diff --git a/src/main/java/org/sadtech/vkbot/core/entity/BoxAnswer.java b/src/main/java/org/sadtech/vkbot/core/entity/BoxAnswer.java new file mode 100644 index 0000000..56f6121 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/entity/BoxAnswer.java @@ -0,0 +1,69 @@ +package org.sadtech.vkbot.core.entity; + +import java.util.Objects; + +public class BoxAnswer { + + private String message; + private String keyboard; + private Float lat; + private Float aLong; + private Integer stickerId; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getKeyboard() { + return keyboard; + } + + public void setKeyboard(String keyboard) { + this.keyboard = keyboard; + } + + public Float getLat() { + return lat; + } + + public void setLat(Float lat) { + this.lat = lat; + } + + public Float getaLong() { + return aLong; + } + + public void setaLong(Float aLong) { + this.aLong = aLong; + } + + public Integer getStickerId() { + return stickerId; + } + + public void setStickerId(Integer stickerId) { + this.stickerId = stickerId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + BoxAnswer boxAnswer = (BoxAnswer) o; + return Objects.equals(message, boxAnswer.message) && + Objects.equals(keyboard, boxAnswer.keyboard) && + Objects.equals(lat, boxAnswer.lat) && + Objects.equals(aLong, boxAnswer.aLong) && + Objects.equals(stickerId, boxAnswer.stickerId); + } + + @Override + public int hashCode() { + return Objects.hash(message, keyboard, lat, aLong, stickerId); + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/insert/VkInsertData.java b/src/main/java/org/sadtech/vkbot/core/insert/VkInsertData.java index c641171..da628a5 100644 --- a/src/main/java/org/sadtech/vkbot/core/insert/VkInsertData.java +++ b/src/main/java/org/sadtech/vkbot/core/insert/VkInsertData.java @@ -2,7 +2,6 @@ package org.sadtech.vkbot.core.insert; import org.sadtech.vkbot.core.VkApi; import org.sadtech.vkbot.core.VkConnect; -import org.sadtech.vkbot.core.entity.MailSend; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -15,9 +14,9 @@ public class VkInsertData { this.vkApi = new VkApi(vkConnect); } - public String insertWords(MailSend mailSend, Integer idUser) { + public String insertWords(String message, Integer idUser) { Pattern pattern = Pattern.compile("%(\\w+)%"); - Matcher m = pattern.matcher(mailSend.getMessage()); + Matcher m = pattern.matcher(message); StringBuffer result = new StringBuffer(); while (m.find()) { m.appendReplacement(result, insert(m.group(0), idUser)); diff --git a/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java b/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java index 13528a3..8502663 100644 --- a/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java +++ b/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java @@ -21,6 +21,8 @@ public class EventListenerVk implements EventListener, Runnable { private VkApiClient vk; private GroupActor actor; + private static Integer DEFAULT_WAIT_TIME = 10; + private RawEventService rawEventService; public EventListenerVk(VkConnect vkConnect) { @@ -44,7 +46,7 @@ public class EventListenerVk implements EventListener, Runnable { int lastTimeStamp = longPollServer.getTs(); while (true) { try { - GetLongPollEventsResponse eventsResponse = vk.longPoll().getEvents(longPollServer.getServer(), longPollServer.getKey(), lastTimeStamp).waitTime(25).execute(); + GetLongPollEventsResponse eventsResponse = vk.longPoll().getEvents(longPollServer.getServer(), longPollServer.getKey(), lastTimeStamp).waitTime(DEFAULT_WAIT_TIME).execute(); for (JsonObject jsonObject: eventsResponse.getUpdates()) { log.info("Новое событие от LongPoll\n" + jsonObject); rawEventService.add(jsonObject); @@ -52,6 +54,7 @@ public class EventListenerVk implements EventListener, Runnable { lastTimeStamp = eventsResponse.getTs(); } catch (LongPollServerKeyExpiredException e) { longPollServer = getLongPollServer(); + lastTimeStamp = longPollServer.getTs(); } } 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 a88ded9..b84c07b 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java @@ -7,13 +7,10 @@ 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; +import org.sadtech.vkbot.core.entity.BoxAnswer; import org.sadtech.vkbot.core.insert.VkInsertData; -import org.sadtech.vkbot.core.entity.MailSend; -import org.sadtech.vkbot.core.insert.InsertWords; -import java.util.List; - -public class MailSenderVk implements MailSent { +public class MailSenderVk implements Sent { public static final Logger log = Logger.getLogger(MailSenderVk.class); @@ -28,19 +25,30 @@ public class MailSenderVk implements MailSent { this.vkInsertData = new VkInsertData(vkConnect); } - private MessagesSendQuery createMessage(MailSend mailSend, Integer peerId) { - MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId); - if (mailSend.getKeyboard() != null) { - messages.keyboard(mailSend.getKeyboard()); + @Override + public void send(Integer idPerson, String message) { + sendMessage(vkApiClient.messages().send(groupActor).peerId(idPerson).message(message)); + } + + @Override + public void send(Integer idPerson, BoxAnswer boxAnswer) { + MessagesSendQuery messagesSendQuery = createMessage(boxAnswer, idPerson); + sendMessage(messagesSendQuery); + } + + private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Integer peerId) { + MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId).message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId)); + if (boxAnswer.getKeyboard() != null) { + messages.keyboard(boxAnswer.getKeyboard()); } else { messages.keyboard("{\"buttons\":[],\"one_time\":true}"); } - if (mailSend.getLat() != null && mailSend.getaLong() != null) { - messages.lat(mailSend.getLat()).lng(mailSend.getaLong()); + if (boxAnswer.getLat() != null && boxAnswer.getaLong() != null) { + messages.lat(boxAnswer.getLat()).lng(boxAnswer.getaLong()); } - if (mailSend.getStickerId() != null) { + if (boxAnswer.getStickerId() != null) { try { - vkApiClient.messages().send(groupActor).peerId(peerId).stickerId(mailSend.getStickerId()).execute(); + vkApiClient.messages().send(groupActor).peerId(peerId).stickerId(boxAnswer.getStickerId()).execute(); } catch (ApiException | ClientException e) { e.printStackTrace(); } @@ -48,27 +56,6 @@ public class MailSenderVk implements MailSent { return messages; } - @Override - public void send(MailSend mailSend, Integer peerId, Integer userId) { - MessagesSendQuery messages = createMessage(mailSend, peerId); - if (mailSend.getMessage() != null) { - messages.message(vkInsertData.insertWords(mailSend, userId)); - } - sendMessage(messages); - } - - @Override - public void send(MailSend mailSend, Integer peerId, Integer userId, List insertWords) { - MessagesSendQuery messages = createMessage(mailSend, peerId); - if (mailSend.getMessage() != null) { - InsertWords insert = new InsertWords(); - insert.setInText(vkInsertData.insertWords(mailSend, peerId)); - insert.insert(insertWords); - messages.message(insert.getOutText()); - } - sendMessage(messages); - } - private void sendMessage(MessagesSendQuery messages) { try { messages.execute(); @@ -76,5 +63,4 @@ public class MailSenderVk implements MailSent { e.printStackTrace(); } } - } diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSent.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSent.java deleted file mode 100644 index 19bab9f..0000000 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSent.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.sadtech.vkbot.core.sender; - -import org.sadtech.vkbot.core.entity.MailSend; - -import java.util.List; - -public interface MailSent { - - void send(MailSend mailSend, Integer peerId, Integer userId); - - void send(MailSend mailSend, Integer peerId, Integer userId, List insertWords); - -} diff --git a/src/main/java/org/sadtech/vkbot/core/sender/Sent.java b/src/main/java/org/sadtech/vkbot/core/sender/Sent.java new file mode 100644 index 0000000..6ccdd09 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/sender/Sent.java @@ -0,0 +1,11 @@ +package org.sadtech.vkbot.core.sender; + +import org.sadtech.vkbot.core.entity.BoxAnswer; + +public interface Sent { + + void send(Integer idPerson, String message); + + void send(Integer idPerson, BoxAnswer boxAnswer); + +} diff --git a/src/main/java/org/sadtech/vkbot/core/service/distribution/impl/MailServiceImpl.java b/src/main/java/org/sadtech/vkbot/core/service/distribution/impl/MailServiceImpl.java index e589207..7a6560f 100644 --- a/src/main/java/org/sadtech/vkbot/core/service/distribution/impl/MailServiceImpl.java +++ b/src/main/java/org/sadtech/vkbot/core/service/distribution/impl/MailServiceImpl.java @@ -62,7 +62,6 @@ public class MailServiceImpl implements MailService { @Override public List getFirstMailByTime(Integer timeFrom, Integer timeTo) { - log.info("Запрошены сообщения от " + timeFrom + " до " + timeTo); List mails = mailRepository.getMailByTime(timeFrom, timeTo); Set people = new HashSet<>(); List returnMails = new ArrayList<>();