From 4e69f4d56120c1ad2335b689370e25d2dc1fbe4c Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Thu, 23 May 2019 13:26:52 +0300 Subject: [PATCH 1/5] =?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=B5=D0=BD=D0=B8=D0=B5=20Subscribe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +- .../distribution/AbstractBasketSubscribe.java | 53 ++++++++++++++++++ .../core/distribution/AccountSubscribe.java | 23 ++++++++ .../core/distribution/EventDistributor.java | 35 +++++------- .../core/distribution/MailSubscriber.java | 55 ++++--------------- .../vkbot/core/sender/MailSenderVk.java | 4 +- 6 files changed, 106 insertions(+), 68 deletions(-) create mode 100644 src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java create mode 100644 src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java diff --git a/pom.xml b/pom.xml index ce1a15b..b0b0bd0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.vkbot vkbot-core - 0.2.0-RELEASE + 0.2.1-SNAPSHOT jar @@ -23,7 +23,7 @@ - 0.6.0-RELEASE + 0.6.1-SNAPSHOT 0.5.13-SNAPSHOT 1.2.17 diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java new file mode 100644 index 0000000..30947fc --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java @@ -0,0 +1,53 @@ +package org.sadtech.vkbot.core.distribution; + +import java.util.Set; + +public abstract class AbstractBasketSubscribe implements EventSubscribe { + + private Set basketSubscribes; + private AbstractBasketSubscribe prioritySubscribe; + + public Set getBasketSubscribes() { + return basketSubscribes; + } + + public void setBasketSubscribes(Set basketSubscribes) { + this.basketSubscribes = basketSubscribes; + } + + public AbstractBasketSubscribe getPrioritySubscribe() { + return prioritySubscribe; + } + + public void setPrioritySubscribe(AbstractBasketSubscribe prioritySubscribe) { + this.prioritySubscribe = prioritySubscribe; + } + + protected abstract boolean check(S object); + + private boolean goNextSubscribe(S object) { + boolean flag = false; + if (prioritySubscribe != null && prioritySubscribe.check(object)) { + prioritySubscribe.update(object); + flag = true; + } else if (basketSubscribes != null) { + for (AbstractBasketSubscribe basketSubscribe : basketSubscribes) { + if (basketSubscribe.check(object)) { + basketSubscribe.update(object); + } else { + flag = true; + } + } + } + return flag; + } + + @Override + public void update(S object) { + if (!goNextSubscribe(object)) { + processing(object); + } + } + + public abstract void processing(S object); +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java new file mode 100644 index 0000000..790b61f --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java @@ -0,0 +1,23 @@ +package org.sadtech.vkbot.core.distribution; + +import com.vk.api.sdk.objects.messages.Message; +import org.sadtech.bot.core.service.AccountService; + +public class AccountSubscribe extends AbstractBasketSubscribe { + + private final AccountService accountService; + + public AccountSubscribe(AccountService accountService) { + this.accountService = accountService; + } + + @Override + protected boolean check(Message userMessage) { + return userMessage.getAttachments().size() > 0 && "Денежный перевод".equals(userMessage.getAttachments().get(0).getLink().getCaption()); + } + + @Override + public void processing(Message object) { + accountService.pay(0); + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java index 051f3bb..5fbc61b 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java @@ -4,15 +4,11 @@ import com.google.gson.JsonObject; import org.apache.log4j.Logger; import org.sadtech.bot.core.service.RawEventService; -import java.util.HashMap; -import java.util.Map; - -public class EventDistributor implements Runnable { +public class EventDistributor extends AbstractBasketSubscribe implements Runnable { private static final Logger log = Logger.getLogger(EventDistributor.class); private final RawEventService rawEventService; - private final Map eventDistributionMap = new HashMap<>(); public EventDistributor(RawEventService rawEventService) { this.rawEventService = rawEventService; @@ -21,28 +17,27 @@ public class EventDistributor implements Runnable { public void update() { while (true) { - try { - if (rawEventService.getJsonObjects().peek() != null) { - JsonObject event = rawEventService.getJsonObjects().poll(); - log.info("Главный дистрибьютор отправил событие дальше"); - if (eventDistributionMap.containsKey(event.get("type").getAsString())) { - eventDistributionMap.get(event.get("type").getAsString()).update(event.getAsJsonObject("object")); - } - } - } catch (Exception e) { - log.error(e.getStackTrace()); - break; + if (rawEventService.getJsonObjects().peek() != null) { + JsonObject event = rawEventService.getJsonObjects().poll(); + log.info("Главный дистрибьютор отправил событие дальше"); + super.update(event); } } } - public void setEventDistributionMap(String key, EventSubscribe eventSubscribe) { - this.eventDistributionMap.put(key, eventSubscribe); - } - @Override public void run() { log.info("EventDistributor запущен"); update(); } + + @Override + protected boolean check(JsonObject object) { + return false; + } + + @Override + public void processing(JsonObject object) { + + } } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java b/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java index b4aeadf..c582499 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java @@ -11,68 +11,37 @@ import org.sadtech.bot.core.domain.attachment.Attachment; import org.sadtech.bot.core.domain.attachment.AudioMessage; import org.sadtech.bot.core.service.MailService; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; import java.time.Instant; import java.time.LocalDateTime; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.TimeZone; -public class MailSubscriber implements EventSubscribe { +public class MailSubscriber extends AbstractBasketSubscribe { private static final Logger log = Logger.getLogger(MailSubscriber.class); private final MailService mailService; - private Set admins = new HashSet<>(); - private final Map> eventDistributionMap = new HashMap<>(); public MailSubscriber(MailService mailService) { this.mailService = mailService; } - public void setAdmins(Set admins) { - this.admins = admins; - } - - public Set getAdmins() { - return admins; + @Override + protected boolean check(JsonObject object) { + String type = object.get("type").getAsString(); + return "message_new".equals(type); } @Override - public void update(JsonObject object) { - log.info("Дистрибьютор получил событие - сообщение"); + public void processing(JsonObject object) { Gson gson = new Gson(); - Message userMessage = gson.fromJson(object, Message.class); + Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class); log.info(userMessage); - - if (userMessage.getPeerId() > 2000000000) { - if (eventDistributionMap.containsKey("chat")) { - eventDistributionMap.get("chat").update(userMessage); - } - } else { - if (admins.contains(userMessage.getPeerId()) && eventDistributionMap.containsKey("terminal")) { - log.info("Сообщение отправлено в репозиторий команд"); - eventDistributionMap.get("terminal").update(userMessage); - } else { - log.info("Сообщение отправленно на добавление в репозиторий"); - mailService.add(createMaail(userMessage)); - } - } + mailService.add(createMail(userMessage)); } - public byte[] getBytes(InputStream inputStream) throws IOException { - ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); - int bufferSize = 1024; - byte[] buffer = new byte[bufferSize]; - - int len = 0; - while ((len = inputStream.read(buffer)) != -1) { - byteBuffer.write(buffer, 0, len); - } - return byteBuffer.toByteArray(); - } - - private Mail createMaail(Message message) { + private Mail createMail(Message message) { Mail mail = new Mail(); mail.setMessage(message.getText()); mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); 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 6b3e8d9..09bb80b 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java @@ -9,7 +9,7 @@ import com.vk.api.sdk.objects.messages.Keyboard; 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.sender.Sent; +import org.sadtech.bot.core.service.sender.Sent; import org.sadtech.vkbot.core.VkConnect; import org.sadtech.vkbot.core.VkInsertData; import org.sadtech.vkbot.core.convert.KeyBoardConvert; @@ -68,8 +68,6 @@ public class MailSenderVk implements Sent { } - - private void sendMessage(MessagesSendQuery messages) { try { messages.execute(); From d40a33bf8092d49705532c1f7e96fa0c450b38e0 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Thu, 23 May 2019 14:31:52 +0300 Subject: [PATCH 2/5] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9=20Subscribe,?= =?UTF-8?q?=20=D0=BA=D0=B0=D0=BA-=D1=82=D0=BE=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../distribution/AbstractBasketSubscribe.java | 26 ++++++++++++++----- .../core/distribution/AccountSubscribe.java | 2 +- .../vkbot/core/distribution/Convert.java | 8 ++++++ .../core/distribution/EventDistributor.java | 12 +++------ .../core/distribution/EventSubscribe.java | 7 ----- .../core/distribution/MailSubscriber.java | 13 +++++----- 6 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 src/main/java/org/sadtech/vkbot/core/distribution/Convert.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/distribution/EventSubscribe.java diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java index 30947fc..f136494 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java @@ -2,10 +2,15 @@ package org.sadtech.vkbot.core.distribution; import java.util.Set; -public abstract class AbstractBasketSubscribe implements EventSubscribe { +public abstract class AbstractBasketSubscribe { private Set basketSubscribes; private AbstractBasketSubscribe prioritySubscribe; + protected Convert convert; + + public AbstractBasketSubscribe() { + convert = (object) -> (C) object; + } public Set getBasketSubscribes() { return basketSubscribes; @@ -23,9 +28,17 @@ public abstract class AbstractBasketSubscribe implements EventSubscribe { this.prioritySubscribe = prioritySubscribe; } + public Convert getConvert() { + return convert; + } + + public void setConvert(Convert convert) { + this.convert = convert; + } + protected abstract boolean check(S object); - private boolean goNextSubscribe(S object) { + private boolean goNextSubscribe(C object) { boolean flag = false; if (prioritySubscribe != null && prioritySubscribe.check(object)) { prioritySubscribe.update(object); @@ -34,7 +47,6 @@ public abstract class AbstractBasketSubscribe implements EventSubscribe { for (AbstractBasketSubscribe basketSubscribe : basketSubscribes) { if (basketSubscribe.check(object)) { basketSubscribe.update(object); - } else { flag = true; } } @@ -42,12 +54,12 @@ public abstract class AbstractBasketSubscribe implements EventSubscribe { return flag; } - @Override public void update(S object) { - if (!goNextSubscribe(object)) { - processing(object); + C newObject = convert.converting(object); + if (!goNextSubscribe(newObject)) { + processing(newObject); } } - public abstract void processing(S object); + public abstract void processing(C object); } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java index 790b61f..aba0a81 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java @@ -3,7 +3,7 @@ package org.sadtech.vkbot.core.distribution; import com.vk.api.sdk.objects.messages.Message; import org.sadtech.bot.core.service.AccountService; -public class AccountSubscribe extends AbstractBasketSubscribe { +public class AccountSubscribe extends AbstractBasketSubscribe { private final AccountService accountService; diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/Convert.java b/src/main/java/org/sadtech/vkbot/core/distribution/Convert.java new file mode 100644 index 0000000..6f2e201 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/Convert.java @@ -0,0 +1,8 @@ +package org.sadtech.vkbot.core.distribution; + +@FunctionalInterface +public interface Convert { + + C converting(T target); + +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java index 5fbc61b..f5e250f 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java @@ -4,7 +4,7 @@ import com.google.gson.JsonObject; import org.apache.log4j.Logger; import org.sadtech.bot.core.service.RawEventService; -public class EventDistributor extends AbstractBasketSubscribe implements Runnable { +public class EventDistributor extends AbstractBasketSubscribe implements Runnable { private static final Logger log = Logger.getLogger(EventDistributor.class); @@ -15,22 +15,16 @@ public class EventDistributor extends AbstractBasketSubscribe implem log.info("EventDistributor инициализирован"); } - public void update() { + @Override + public void run() { while (true) { if (rawEventService.getJsonObjects().peek() != null) { JsonObject event = rawEventService.getJsonObjects().poll(); - log.info("Главный дистрибьютор отправил событие дальше"); super.update(event); } } } - @Override - public void run() { - log.info("EventDistributor запущен"); - update(); - } - @Override protected boolean check(JsonObject object) { return false; diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/EventSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/EventSubscribe.java deleted file mode 100644 index bd8dead..0000000 --- a/src/main/java/org/sadtech/vkbot/core/distribution/EventSubscribe.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.sadtech.vkbot.core.distribution; - -public interface EventSubscribe { - - void update(T object); - -} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java b/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java index c582499..267c363 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java @@ -17,7 +17,7 @@ import java.util.ArrayList; import java.util.List; import java.util.TimeZone; -public class MailSubscriber extends AbstractBasketSubscribe { +public class MailSubscriber extends AbstractBasketSubscribe { private static final Logger log = Logger.getLogger(MailSubscriber.class); @@ -25,6 +25,10 @@ public class MailSubscriber extends AbstractBasketSubscribe { public MailSubscriber(MailService mailService) { this.mailService = mailService; + this.convert = (object) -> { + Gson gson = new Gson(); + return gson.fromJson(object.getAsJsonObject("object"), Message.class); + }; } @Override @@ -34,11 +38,8 @@ public class MailSubscriber extends AbstractBasketSubscribe { } @Override - public void processing(JsonObject object) { - Gson gson = new Gson(); - Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class); - log.info(userMessage); - mailService.add(createMail(userMessage)); + public void processing(Message object) { + mailService.add(createMail(object)); } private Mail createMail(Message message) { From 74d02993420a64e535d2ac40a297554d9e45d096 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Fri, 24 May 2019 15:47:33 +0300 Subject: [PATCH 3/5] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3.=20=D0=A3=D0=B4=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BD=D0=B5=D0=BD=D1=83=D0=B6=D0=BD=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 3 +- .../{distribution => convert}/Convert.java | 4 +- .../core/convert/MessageMailConvert.java | 50 +++++++++++++ .../distribution/AbstractBasketSubscribe.java | 2 + .../core/distribution/AccountSubscribe.java | 43 ++++++++++- .../core/distribution/EventDistributor.java | 1 + .../core/distribution/MailSubscriber.java | 39 ++-------- .../vkbot/core/sender/MailSenderVk.java | 4 +- .../vkbot/core/service/PersonServiceImpl.java | 74 ------------------- 9 files changed, 103 insertions(+), 117 deletions(-) rename src/main/java/org/sadtech/vkbot/core/{distribution => convert}/Convert.java (50%) create mode 100644 src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/service/PersonServiceImpl.java diff --git a/pom.xml b/pom.xml index b0b0bd0..9b8e494 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,8 @@ 0.6.1-SNAPSHOT - 0.5.13-SNAPSHOT + + 1.0.2 1.2.17 diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/Convert.java b/src/main/java/org/sadtech/vkbot/core/convert/Convert.java similarity index 50% rename from src/main/java/org/sadtech/vkbot/core/distribution/Convert.java rename to src/main/java/org/sadtech/vkbot/core/convert/Convert.java index 6f2e201..792bd25 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/Convert.java +++ b/src/main/java/org/sadtech/vkbot/core/convert/Convert.java @@ -1,8 +1,8 @@ -package org.sadtech.vkbot.core.distribution; +package org.sadtech.vkbot.core.convert; -@FunctionalInterface public interface Convert { C converting(T target); + } diff --git a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java new file mode 100644 index 0000000..a09b446 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java @@ -0,0 +1,50 @@ +package org.sadtech.vkbot.core.convert; + +import com.vk.api.sdk.objects.messages.Message; +import com.vk.api.sdk.objects.messages.MessageAttachment; +import org.sadtech.bot.core.domain.attachment.Attachment; +import org.sadtech.bot.core.domain.attachment.AudioMessage; +import org.sadtech.bot.core.domain.attachment.Geo; +import org.sadtech.bot.core.domain.content.Mail; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.util.TimeZone; +import java.util.stream.Collectors; + +public class MessageMailConvert implements Convert { + + @Override + public Mail converting(Message message) { + Mail mail = new Mail(); + mail.setMessage(message.getText()); + mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); + mail.setId(message.getId()); + mail.setPersonId(message.getPeerId()); + mail.setAttachments(message.getAttachments() + .stream() + .map(this::convertAttachment) + .collect(Collectors.toList())); + mail.getAttachments().add(convertGeo(message.getGeo())); + return mail; + } + + private Geo convertGeo(com.vk.api.sdk.objects.base.Geo geoVk) { + return Geo.builder() + .coordinate(geoVk.getCoordinates().getLatitude(), + geoVk.getCoordinates().getLongitude()) + .build(); + } + + private Attachment convertAttachment(MessageAttachment vkAttachment) { + Attachment attachment = null; + switch (vkAttachment.getType()) { + case AUDIO_MESSAGE: + AudioMessage audioMessage = new AudioMessage(); + audioMessage.setLinkOdd(vkAttachment.getAudioMessage().getLinkOgg()); + attachment = audioMessage; + break; + } + return attachment; + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java index f136494..7d3affe 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AbstractBasketSubscribe.java @@ -1,5 +1,7 @@ package org.sadtech.vkbot.core.distribution; +import org.sadtech.vkbot.core.convert.Convert; + import java.util.Set; public abstract class AbstractBasketSubscribe { diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java index aba0a81..d100355 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java @@ -1,14 +1,41 @@ package org.sadtech.vkbot.core.distribution; import com.vk.api.sdk.objects.messages.Message; +import org.sadtech.bot.core.domain.BoxAnswer; +import org.sadtech.bot.core.exception.PaymentException; import org.sadtech.bot.core.service.AccountService; +import org.sadtech.bot.core.service.sender.Sent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class AccountSubscribe extends AbstractBasketSubscribe { - private final AccountService accountService; + private static final Logger log = LoggerFactory.getLogger(AccountSubscribe.class); - public AccountSubscribe(AccountService accountService) { + private final AccountService accountService; + private final Sent sent; + private BoxAnswer answerSuccessfulPayment; + private BoxAnswer answerFailPayment; + + public AccountSubscribe(AccountService accountService, Sent sent) { this.accountService = accountService; + this.sent = sent; + } + + public BoxAnswer getAnswerSuccessfulPayment() { + return answerSuccessfulPayment; + } + + public void setAnswerSuccessfulPayment(BoxAnswer answerSuccessfulPayment) { + this.answerSuccessfulPayment = answerSuccessfulPayment; + } + + public BoxAnswer getAnswerFailPayment() { + return answerFailPayment; + } + + public void setAnswerFailPayment(BoxAnswer answerFailPayment) { + this.answerFailPayment = answerFailPayment; } @Override @@ -17,7 +44,15 @@ public class AccountSubscribe extends AbstractBasketSubscribe } @Override - public void processing(Message object) { - accountService.pay(0); + public void processing(Message message) { + if (message.getText() != null) { + try { + if (accountService.pay(Integer.valueOf(message.getText()), message.getPeerId(), Double.valueOf(message.getAttachments().get(0).getLink().getTitle().split(" ")[0]))) { + sent.send(message.getPeerId(), answerSuccessfulPayment); + } + } catch (PaymentException e) { + sent.send(message.getPeerId(), BoxAnswer.builder().message(e.getDescription()).build()); + } + } } } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java index f5e250f..731da2e 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java @@ -20,6 +20,7 @@ public class EventDistributor extends AbstractBasketSubscribe { - private static final Logger log = Logger.getLogger(MailSubscriber.class); - private final MailService mailService; + private final Convert mailConvert = new MessageMailConvert(); public MailSubscriber(MailService mailService) { this.mailService = mailService; @@ -39,26 +29,7 @@ public class MailSubscriber extends AbstractBasketSubscribe @Override public void processing(Message object) { - mailService.add(createMail(object)); - } - - private Mail createMail(Message message) { - Mail mail = new Mail(); - mail.setMessage(message.getText()); - mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); - mail.setId(message.getId()); - mail.setPersonId(message.getPeerId()); - - List attachments = new ArrayList<>(); - for (MessageAttachment attachment : message.getAttachments()) { - if (MessageAttachmentType.AUDIO_MESSAGE.equals(attachment.getType())) { - AudioMessage audioMessage = new AudioMessage(); - audioMessage.setLinkOdd(attachment.getAudioMessage().getLinkOgg()); - attachments.add(audioMessage); - } - } - mail.setAttachments(attachments); - return mail; + mailService.add(mailConvert.converting(object)); } } 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 09bb80b..0af6de0 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java @@ -54,8 +54,8 @@ public class MailSenderVk implements Sent { keyboard.setButtons(Collections.EMPTY_LIST); messages.keyboard(keyboard); } - if (boxAnswer.getLat() != null && boxAnswer.getaLong() != null) { - messages.lat(boxAnswer.getLat()).lng(boxAnswer.getaLong()); + if (boxAnswer.getCoordinates() != null) { + messages.lat(boxAnswer.getCoordinates().getLat()).lng(boxAnswer.getCoordinates().getaLong()); } if (boxAnswer.getStickerId() != null) { try { diff --git a/src/main/java/org/sadtech/vkbot/core/service/PersonServiceImpl.java b/src/main/java/org/sadtech/vkbot/core/service/PersonServiceImpl.java deleted file mode 100644 index c14abb0..0000000 --- a/src/main/java/org/sadtech/vkbot/core/service/PersonServiceImpl.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.sadtech.vkbot.core.service; - -import com.vk.api.sdk.objects.users.User; -import com.vk.api.sdk.objects.users.UserMin; -import org.apache.log4j.Logger; -import org.sadtech.bot.core.domain.Person; -import org.sadtech.bot.core.repository.PersonRepository; -import org.sadtech.bot.core.repository.impl.PersonRepositoryMap; -import org.sadtech.bot.core.service.PersonService; -import org.sadtech.vkbot.core.VkApi; -import org.sadtech.vkbot.core.VkConnect; - -public class PersonServiceImpl implements PersonService { - - private static final Logger log = Logger.getLogger(PersonServiceImpl.class); - - private final PersonRepository personRepository; - private final VkApi vkApi; - - public PersonServiceImpl(VkConnect vkConnect) { - this.personRepository = new PersonRepositoryMap(); - vkApi = new VkApi(vkConnect); - } - - public PersonServiceImpl(PersonRepository personRepository, VkConnect vkConnect) { - this.personRepository = personRepository; - vkApi = new VkApi(vkConnect); - } - - public void add(User user) { - Person person = new Person(); - person.setId(user.getId()); - person.setLastName(user.getLastName()); - person.setFirstName(user.getFirstName()); - personRepository.add(person); - log.info("Пользователь добавлен в репозиторий"); - } - - @Override - public void add(Person person) { - personRepository.add(person); - log.info("Пользователь добавлен в репозиторий"); - } - - public void add(UserMin userMin) { - Person person = new Person(); - person.setId(userMin.getId()); - person.setFirstName(userMin.getFirstName()); - person.setLastName(userMin.getLastName()); - personRepository.add(person); - } - - @Override - public Person get(Integer id) { - return personRepository.get(id); - } - - @Override - public boolean checkPerson(Integer idPerson) { - log.info("Проверка наличия пользователя в репозитории"); - return get(idPerson) != null; - } - - @Override - public Person createPerson(Integer userId) { - Person person = new Person(); - UserMin userMin = vkApi.getUserMini(userId); - person.setId(userMin.getId()); - person.setLastName(userMin.getLastName()); - person.setFirstName(userMin.getFirstName()); - return person; - } - -} From 6cc5daefce05565838a60103d6942ac737f9e3f1 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Thu, 30 May 2019 15:48:58 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=82?= =?UTF-8?q?=D0=B8=D0=BF=20=D0=BA=D0=BE=D0=BD=D1=82=D0=B5=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=20-=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=B2=20=D0=BE=D0=B1=D1=81=D1=83=D0=B6=D0=B4?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +- .../java/org/sadtech/vkbot/core/VkConfig.java | 68 +++---------- .../org/sadtech/vkbot/core/VkConfigGroup.java | 79 +++++++++++++++ .../org/sadtech/vkbot/core/VkConfigUser.java | 39 ++++++++ .../org/sadtech/vkbot/core/VkConnect.java | 19 +++- .../core/convert/MessageMailConvert.java | 12 ++- .../convert/TopicCommentToBoardComment.java | 23 +++++ .../core/distribution/AccountSubscribe.java | 13 +-- .../distribution/BoardCommentSubscribe.java | 96 +++++++++++++++++++ .../core/sender/BoardCommentSenderVk.java | 41 ++++++++ .../vkbot/core/sender/MailSenderVk.java | 12 +-- 11 files changed, 327 insertions(+), 79 deletions(-) create mode 100644 src/main/java/org/sadtech/vkbot/core/VkConfigGroup.java create mode 100644 src/main/java/org/sadtech/vkbot/core/VkConfigUser.java create mode 100644 src/main/java/org/sadtech/vkbot/core/convert/TopicCommentToBoardComment.java create mode 100644 src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java create mode 100644 src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java diff --git a/pom.xml b/pom.xml index 9b8e494..b8866a5 100644 --- a/pom.xml +++ b/pom.xml @@ -23,9 +23,9 @@ - 0.6.1-SNAPSHOT + 0.6.2-SNAPSHOT - 1.0.2 + 0.5.13-SNAPSHOT 1.2.17 diff --git a/src/main/java/org/sadtech/vkbot/core/VkConfig.java b/src/main/java/org/sadtech/vkbot/core/VkConfig.java index c0adbe5..3b0bd52 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkConfig.java +++ b/src/main/java/org/sadtech/vkbot/core/VkConfig.java @@ -4,76 +4,36 @@ import java.util.Objects; public class VkConfig { - private String groupToken; - private Integer groupId; - private String serviceToken; - private Integer appId; - private String groupSecretKey; - private String groupPublicKey; + private VkConfigGroup configGroup; + private VkConfigUser configUser; - public String getGroupToken() { - return groupToken; + public VkConfigGroup getConfigGroup() { + return configGroup; } - public void setGroupToken(String groupToken) { - this.groupToken = groupToken; + public void setConfigGroup(VkConfigGroup configGroup) { + this.configGroup = configGroup; } - public Integer getGroupId() { - return groupId; + public VkConfigUser getConfigUser() { + return configUser; } - public void setGroupId(Integer groupId) { - this.groupId = groupId; - } - - public String getServiceToken() { - return serviceToken; - } - - public void setServiceToken(String serviceToken) { - this.serviceToken = serviceToken; - } - - public Integer getAppId() { - return appId; - } - - public void setAppId(Integer appId) { - this.appId = appId; - } - - public String getGroupSecretKey() { - return groupSecretKey; - } - - public void setGroupSecretKey(String groupSecretKey) { - this.groupSecretKey = groupSecretKey; - } - - public String getGroupPublicKey() { - return groupPublicKey; - } - - public void setGroupPublicKey(String groupPublicKey) { - this.groupPublicKey = groupPublicKey; + public void setConfigUser(VkConfigUser configUser) { + this.configUser = configUser; } @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (!(o instanceof VkConfig)) return false; VkConfig vkConfig = (VkConfig) o; - return Objects.equals(groupToken, vkConfig.groupToken) && - Objects.equals(groupId, vkConfig.groupId) && - Objects.equals(serviceToken, vkConfig.serviceToken) && - Objects.equals(appId, vkConfig.appId) && - Objects.equals(groupSecretKey, vkConfig.groupSecretKey) && - Objects.equals(groupPublicKey, vkConfig.groupPublicKey); + return Objects.equals(configGroup, vkConfig.configGroup) && + Objects.equals(configUser, vkConfig.configUser); } @Override public int hashCode() { - return Objects.hash(groupToken, groupId, serviceToken, appId, groupSecretKey, groupPublicKey); + return Objects.hash(configGroup, configUser); } } diff --git a/src/main/java/org/sadtech/vkbot/core/VkConfigGroup.java b/src/main/java/org/sadtech/vkbot/core/VkConfigGroup.java new file mode 100644 index 0000000..083d914 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/VkConfigGroup.java @@ -0,0 +1,79 @@ +package org.sadtech.vkbot.core; + +import java.util.Objects; + +public class VkConfigGroup { + + private String groupToken; + private Integer groupId; + private String serviceToken; + private Integer appId; + private String groupSecretKey; + private String groupPublicKey; + + public String getGroupToken() { + return groupToken; + } + + public void setGroupToken(String groupToken) { + this.groupToken = groupToken; + } + + public Integer getGroupId() { + return groupId; + } + + public void setGroupId(Integer groupId) { + this.groupId = groupId; + } + + public String getServiceToken() { + return serviceToken; + } + + public void setServiceToken(String serviceToken) { + this.serviceToken = serviceToken; + } + + public Integer getAppId() { + return appId; + } + + public void setAppId(Integer appId) { + this.appId = appId; + } + + public String getGroupSecretKey() { + return groupSecretKey; + } + + public void setGroupSecretKey(String groupSecretKey) { + this.groupSecretKey = groupSecretKey; + } + + public String getGroupPublicKey() { + return groupPublicKey; + } + + public void setGroupPublicKey(String groupPublicKey) { + this.groupPublicKey = groupPublicKey; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof VkConfigGroup)) return false; + VkConfigGroup that = (VkConfigGroup) o; + return Objects.equals(groupToken, that.groupToken) && + Objects.equals(groupId, that.groupId) && + Objects.equals(serviceToken, that.serviceToken) && + Objects.equals(appId, that.appId) && + Objects.equals(groupSecretKey, that.groupSecretKey) && + Objects.equals(groupPublicKey, that.groupPublicKey); + } + + @Override + public int hashCode() { + return Objects.hash(groupToken, groupId, serviceToken, appId, groupSecretKey, groupPublicKey); + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/VkConfigUser.java b/src/main/java/org/sadtech/vkbot/core/VkConfigUser.java new file mode 100644 index 0000000..7912c09 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/VkConfigUser.java @@ -0,0 +1,39 @@ +package org.sadtech.vkbot.core; + +import java.util.Objects; + +public class VkConfigUser { + + private Integer userId; + private String token; + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof VkConfigUser)) return false; + VkConfigUser that = (VkConfigUser) o; + return Objects.equals(userId, that.userId) && + Objects.equals(token, that.token); + } + + @Override + public int hashCode() { + return Objects.hash(userId, token); + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/VkConnect.java b/src/main/java/org/sadtech/vkbot/core/VkConnect.java index d4c90ce..12e0579 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkConnect.java +++ b/src/main/java/org/sadtech/vkbot/core/VkConnect.java @@ -3,6 +3,7 @@ package org.sadtech.vkbot.core; import com.vk.api.sdk.client.VkApiClient; import com.vk.api.sdk.client.actors.GroupActor; import com.vk.api.sdk.client.actors.ServiceActor; +import com.vk.api.sdk.client.actors.UserActor; import com.vk.api.sdk.httpclient.HttpTransportClient; public class VkConnect { @@ -12,6 +13,7 @@ public class VkConnect { private VkApiClient vkApiClient; private GroupActor groupActor; private ServiceActor serviceActor; + private UserActor userActor; public VkConnect(VkConfig vkConfig) { this.vkConfig = vkConfig; @@ -35,6 +37,19 @@ public class VkConnect { } } + public UserActor getUserActor() { + if (userActor != null) { + return userActor; + } else { + initUserActor(); + return userActor; + } + } + + private void initUserActor() { + userActor = new UserActor(vkConfig.getConfigUser().getUserId(), vkConfig.getConfigUser().getToken()); + } + public ServiceActor getServiceActor() { if (serviceActor != null) { return serviceActor; @@ -49,10 +64,10 @@ public class VkConnect { } private void initGroupActor() { - groupActor = new GroupActor(vkConfig.getGroupId(), vkConfig.getGroupToken()); + groupActor = new GroupActor(vkConfig.getConfigGroup().getGroupId(), vkConfig.getConfigGroup().getGroupToken()); } private void initServiceActor() { - serviceActor = new ServiceActor(vkConfig.getAppId(), vkConfig.getServiceToken()); + serviceActor = new ServiceActor(vkConfig.getConfigGroup().getAppId(), vkConfig.getConfigGroup().getServiceToken()); } } diff --git a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java index a09b446..5ef5511 100644 --- a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java +++ b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java @@ -2,10 +2,10 @@ package org.sadtech.vkbot.core.convert; import com.vk.api.sdk.objects.messages.Message; import com.vk.api.sdk.objects.messages.MessageAttachment; -import org.sadtech.bot.core.domain.attachment.Attachment; -import org.sadtech.bot.core.domain.attachment.AudioMessage; -import org.sadtech.bot.core.domain.attachment.Geo; import org.sadtech.bot.core.domain.content.Mail; +import org.sadtech.bot.core.domain.content.attachment.Attachment; +import org.sadtech.bot.core.domain.content.attachment.AudioMessage; +import org.sadtech.bot.core.domain.content.attachment.Geo; import java.time.Instant; import java.time.LocalDateTime; @@ -18,14 +18,16 @@ public class MessageMailConvert implements Convert { public Mail converting(Message message) { Mail mail = new Mail(); mail.setMessage(message.getText()); - mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); + mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); mail.setId(message.getId()); mail.setPersonId(message.getPeerId()); mail.setAttachments(message.getAttachments() .stream() .map(this::convertAttachment) .collect(Collectors.toList())); - mail.getAttachments().add(convertGeo(message.getGeo())); + if (message.getGeo()!=null) { + mail.getAttachments().add(convertGeo(message.getGeo())); + } return mail; } diff --git a/src/main/java/org/sadtech/vkbot/core/convert/TopicCommentToBoardComment.java b/src/main/java/org/sadtech/vkbot/core/convert/TopicCommentToBoardComment.java new file mode 100644 index 0000000..f3a9d3e --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/convert/TopicCommentToBoardComment.java @@ -0,0 +1,23 @@ +package org.sadtech.vkbot.core.convert; + +import com.vk.api.sdk.objects.board.TopicComment; +import org.sadtech.bot.core.domain.content.BoardComment; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.util.TimeZone; + +public class TopicCommentToBoardComment implements Convert { + @Override + public BoardComment converting(TopicComment target) { + BoardComment boardComment = new BoardComment(); + boardComment.setTopicId(target.getTopicId()); + boardComment.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(target.getDate()), TimeZone.getDefault().toZoneId())); + boardComment.setMessage(target.getText()); + boardComment.setPersonId(target.getFromId()); + + + System.out.println(target); + return boardComment; + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java index d100355..d65c753 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java @@ -15,7 +15,6 @@ public class AccountSubscribe extends AbstractBasketSubscribe private final AccountService accountService; private final Sent sent; private BoxAnswer answerSuccessfulPayment; - private BoxAnswer answerFailPayment; public AccountSubscribe(AccountService accountService, Sent sent) { this.accountService = accountService; @@ -30,14 +29,6 @@ public class AccountSubscribe extends AbstractBasketSubscribe this.answerSuccessfulPayment = answerSuccessfulPayment; } - public BoxAnswer getAnswerFailPayment() { - return answerFailPayment; - } - - public void setAnswerFailPayment(BoxAnswer answerFailPayment) { - this.answerFailPayment = answerFailPayment; - } - @Override protected boolean check(Message userMessage) { return userMessage.getAttachments().size() > 0 && "Денежный перевод".equals(userMessage.getAttachments().get(0).getLink().getCaption()); @@ -47,10 +38,12 @@ public class AccountSubscribe extends AbstractBasketSubscribe public void processing(Message message) { if (message.getText() != null) { try { - if (accountService.pay(Integer.valueOf(message.getText()), message.getPeerId(), Double.valueOf(message.getAttachments().get(0).getLink().getTitle().split(" ")[0]))) { + Double valueSum = Double.valueOf(message.getAttachments().get(0).getLink().getTitle().split(" ")[0]); + if (accountService.pay(Integer.valueOf(message.getText()), message.getPeerId(), valueSum) && answerSuccessfulPayment != null) { sent.send(message.getPeerId(), answerSuccessfulPayment); } } catch (PaymentException e) { + log.error(e.getMessage()); sent.send(message.getPeerId(), BoxAnswer.builder().message(e.getDescription()).build()); } } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java new file mode 100644 index 0000000..6b918b8 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java @@ -0,0 +1,96 @@ +package org.sadtech.vkbot.core.distribution; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.vk.api.sdk.objects.board.TopicComment; +import org.sadtech.bot.core.domain.content.BoardComment; +import org.sadtech.bot.core.service.BoardCommentService; +import org.sadtech.vkbot.core.convert.Convert; +import org.sadtech.vkbot.core.convert.TopicCommentToBoardComment; + +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class BoardCommentSubscribe extends AbstractBasketSubscribe { + + private final BoardCommentService boardCommentService; + private final Convert topicConvert = new TopicCommentToBoardComment(); + private Set answerTopicsId; + private Set noAnswerPersonId; + private Boolean respondAppeal = true; + + public BoardCommentSubscribe(BoardCommentService boardCommentService) { + this.boardCommentService = boardCommentService; + this.convert = (object) -> { + Gson gson = new Gson(); + return gson.fromJson(object.getAsJsonObject("object"), TopicComment.class); + }; + } + + public Set getAnswerTopicsId() { + return answerTopicsId; + } + + public void setAnswerTopicsId(Set answerTopicsId) { + this.answerTopicsId = answerTopicsId; + } + + public Set getNoAnswerPersonId() { + return noAnswerPersonId; + } + + public void setNoAnswerPersonId(Set noAnswerPersonId) { + this.noAnswerPersonId = noAnswerPersonId; + } + + public Boolean getRespondAppeal() { + return respondAppeal; + } + + public void setRespondAppeal(Boolean respondAppeal) { + this.respondAppeal = respondAppeal; + } + + @Override + protected boolean check(JsonObject object) { + String type = object.get("type").getAsString(); + String message = object.getAsJsonObject("object").get("text").getAsString(); + Integer groupId = object.get("group_id").getAsInt(); + return "board_post_new".equals(type) && checkRespondAppeal(message, groupId); + } + + private boolean checkRespondAppeal(String message, Integer groupId) { + if (respondAppeal) { + Pattern pattern = Pattern.compile("\\[club" + groupId); + Matcher m = pattern.matcher(message); + return m.find(); + } else { + return true; + } + } + + private boolean checkTopic(Integer topicId) { + if (answerTopicsId == null) { + return true; + } else { + return answerTopicsId.contains(topicId); + } + } + + private boolean checkPerson(Integer personId) { + if (noAnswerPersonId == null) { + return true; + } else { + return !noAnswerPersonId.contains(personId); + } + } + + + @Override + public void processing(TopicComment object) { + if (checkPerson(object.getFromId()) && checkTopic(object.getTopicId())) { + boardCommentService.add(topicConvert.converting(object)); + } + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java new file mode 100644 index 0000000..133f10f --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java @@ -0,0 +1,41 @@ +package org.sadtech.vkbot.core.sender; + +import com.vk.api.sdk.client.VkApiClient; +import com.vk.api.sdk.client.actors.GroupActor; +import com.vk.api.sdk.client.actors.UserActor; +import com.vk.api.sdk.exceptions.ApiException; +import com.vk.api.sdk.exceptions.ClientException; +import org.sadtech.bot.core.domain.BoxAnswer; +import org.sadtech.bot.core.exception.MailSendException; +import org.sadtech.bot.core.service.sender.Sent; +import org.sadtech.vkbot.core.VkConnect; +import org.sadtech.vkbot.core.VkInsertData; + +public class BoardCommentSenderVk implements Sent { + + private final VkApiClient vkApiClient; + private final GroupActor groupActor; + private final UserActor userActor; + private final VkInsertData vkInsertData; + + public BoardCommentSenderVk(VkConnect vkConnect) { + this.vkApiClient = vkConnect.getVkApiClient(); + this.groupActor = vkConnect.getGroupActor(); + this.vkInsertData = new VkInsertData(vkConnect); + this.userActor = vkConnect.getUserActor(); + } + + @Override + public void send(Integer integer, BoxAnswer boxAnswer) { + throw new MailSendException(); + } + + @Override + public void send(Integer contentId, Integer personId, BoxAnswer boxAnswer) { + try { + vkApiClient.board().createComment(userActor, groupActor.getGroupId(), contentId).message(vkInsertData.insertWords(boxAnswer.getMessage(), personId)).fromGroup(true).execute(); + } catch (ApiException | ClientException e) { + e.printStackTrace(); + } + } +} 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 0af6de0..07e6c80 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java @@ -34,14 +34,14 @@ public class MailSenderVk implements Sent { } @Override - public void send(Integer idPerson, String message) { - sendMessage(vkApiClient.messages().send(groupActor).peerId(idPerson).message(message).randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE))); + public void send(Integer personId, BoxAnswer boxAnswer) { + MessagesSendQuery messagesSendQuery = createMessage(boxAnswer, personId); + sendMessage(messagesSendQuery); } @Override - public void send(Integer idPerson, BoxAnswer boxAnswer) { - MessagesSendQuery messagesSendQuery = createMessage(boxAnswer, idPerson); - sendMessage(messagesSendQuery); + public void send(Integer integer, Integer integer1, BoxAnswer boxAnswer) { + } private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Integer peerId) { @@ -55,7 +55,7 @@ public class MailSenderVk implements Sent { messages.keyboard(keyboard); } if (boxAnswer.getCoordinates() != null) { - messages.lat(boxAnswer.getCoordinates().getLat()).lng(boxAnswer.getCoordinates().getaLong()); + messages.lat(boxAnswer.getCoordinates().getLatitude()).lng(boxAnswer.getCoordinates().getLongitude()); } if (boxAnswer.getStickerId() != null) { try { From b9aa8f31711eb8dd9b82c5a7539289d31932689c Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 15 Jun 2019 15:31:58 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=9C=D0=BD=D0=BE=D0=B6=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D0=B5=D0=BD=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 | 6 +- .../java/org/sadtech/vkbot/core/VkConfig.java | 39 --------- .../org/sadtech/vkbot/core/VkConfigGroup.java | 79 ------------------- .../sadtech/vkbot/core/config/VkConfig.java | 75 ++++++++++++++++++ .../vkbot/core/config/VkConfigGroup.java | 56 +++++++++++++ .../vkbot/core/config/VkConfigService.java | 56 +++++++++++++ .../vkbot/core/{ => config}/VkConfigUser.java | 36 +++++++-- .../vkbot/core/{ => config}/VkConnect.java | 4 +- .../sadtech/vkbot/core/convert/Convert.java | 1 + .../vkbot/core/convert/KeyBoardConvert.java | 62 ++++++++++++--- .../core/convert/MessageMailConvert.java | 1 + .../convert/TopicCommentToBoardComment.java | 2 +- .../distribution/AbstractBasketSubscribe.java | 54 +++++++------ .../core/distribution/AccountSubscribe.java | 23 +++--- .../distribution/BoardCommentSubscribe.java | 20 ++--- .../vkbot/core/exception/ConfigException.java | 8 ++ .../vkbot/core/listener/EventListenerVk.java | 40 +++++----- .../core/sender/BoardCommentSenderVk.java | 24 +++++- .../vkbot/core/sender/MailSenderVk.java | 49 +++++++----- .../sadtech/vkbot/core/{ => utils}/VkApi.java | 9 ++- .../vkbot/core/{ => utils}/VkInsertData.java | 8 +- 21 files changed, 415 insertions(+), 237 deletions(-) delete mode 100644 src/main/java/org/sadtech/vkbot/core/VkConfig.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/VkConfigGroup.java create mode 100644 src/main/java/org/sadtech/vkbot/core/config/VkConfig.java create mode 100644 src/main/java/org/sadtech/vkbot/core/config/VkConfigGroup.java create mode 100644 src/main/java/org/sadtech/vkbot/core/config/VkConfigService.java rename src/main/java/org/sadtech/vkbot/core/{ => config}/VkConfigUser.java (52%) rename src/main/java/org/sadtech/vkbot/core/{ => config}/VkConnect.java (91%) create mode 100644 src/main/java/org/sadtech/vkbot/core/exception/ConfigException.java rename src/main/java/org/sadtech/vkbot/core/{ => utils}/VkApi.java (90%) rename src/main/java/org/sadtech/vkbot/core/{ => utils}/VkInsertData.java (80%) diff --git a/pom.xml b/pom.xml index b8866a5..621fbc2 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.vkbot vkbot-core - 0.2.1-SNAPSHOT + 0.2.1-RELEASE jar @@ -23,9 +23,9 @@ - 0.6.2-SNAPSHOT + 0.6.2-RELEASE - 0.5.13-SNAPSHOT + 0.5.13-FORK 1.2.17 diff --git a/src/main/java/org/sadtech/vkbot/core/VkConfig.java b/src/main/java/org/sadtech/vkbot/core/VkConfig.java deleted file mode 100644 index 3b0bd52..0000000 --- a/src/main/java/org/sadtech/vkbot/core/VkConfig.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.sadtech.vkbot.core; - -import java.util.Objects; - -public class VkConfig { - - private VkConfigGroup configGroup; - private VkConfigUser configUser; - - public VkConfigGroup getConfigGroup() { - return configGroup; - } - - public void setConfigGroup(VkConfigGroup configGroup) { - this.configGroup = configGroup; - } - - public VkConfigUser getConfigUser() { - return configUser; - } - - public void setConfigUser(VkConfigUser configUser) { - this.configUser = configUser; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof VkConfig)) return false; - VkConfig vkConfig = (VkConfig) o; - return Objects.equals(configGroup, vkConfig.configGroup) && - Objects.equals(configUser, vkConfig.configUser); - } - - @Override - public int hashCode() { - return Objects.hash(configGroup, configUser); - } -} diff --git a/src/main/java/org/sadtech/vkbot/core/VkConfigGroup.java b/src/main/java/org/sadtech/vkbot/core/VkConfigGroup.java deleted file mode 100644 index 083d914..0000000 --- a/src/main/java/org/sadtech/vkbot/core/VkConfigGroup.java +++ /dev/null @@ -1,79 +0,0 @@ -package org.sadtech.vkbot.core; - -import java.util.Objects; - -public class VkConfigGroup { - - private String groupToken; - private Integer groupId; - private String serviceToken; - private Integer appId; - private String groupSecretKey; - private String groupPublicKey; - - public String getGroupToken() { - return groupToken; - } - - public void setGroupToken(String groupToken) { - this.groupToken = groupToken; - } - - public Integer getGroupId() { - return groupId; - } - - public void setGroupId(Integer groupId) { - this.groupId = groupId; - } - - public String getServiceToken() { - return serviceToken; - } - - public void setServiceToken(String serviceToken) { - this.serviceToken = serviceToken; - } - - public Integer getAppId() { - return appId; - } - - public void setAppId(Integer appId) { - this.appId = appId; - } - - public String getGroupSecretKey() { - return groupSecretKey; - } - - public void setGroupSecretKey(String groupSecretKey) { - this.groupSecretKey = groupSecretKey; - } - - public String getGroupPublicKey() { - return groupPublicKey; - } - - public void setGroupPublicKey(String groupPublicKey) { - this.groupPublicKey = groupPublicKey; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof VkConfigGroup)) return false; - VkConfigGroup that = (VkConfigGroup) o; - return Objects.equals(groupToken, that.groupToken) && - Objects.equals(groupId, that.groupId) && - Objects.equals(serviceToken, that.serviceToken) && - Objects.equals(appId, that.appId) && - Objects.equals(groupSecretKey, that.groupSecretKey) && - Objects.equals(groupPublicKey, that.groupPublicKey); - } - - @Override - public int hashCode() { - return Objects.hash(groupToken, groupId, serviceToken, appId, groupSecretKey, groupPublicKey); - } -} diff --git a/src/main/java/org/sadtech/vkbot/core/config/VkConfig.java b/src/main/java/org/sadtech/vkbot/core/config/VkConfig.java new file mode 100644 index 0000000..bc2e6f5 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/config/VkConfig.java @@ -0,0 +1,75 @@ +package org.sadtech.vkbot.core.config; + +import org.sadtech.vkbot.core.exception.ConfigException; + +import java.util.Objects; +import java.util.Optional; + +public class VkConfig { + + private VkConfigGroup configGroup; + private VkConfigUser configUser; + private VkConfigService configService; + + public VkConfigGroup getConfigGroup() { + return Optional.ofNullable(configGroup) + .orElseThrow(() -> new ConfigException("Конфигурация сервиса для группы найдена")); + + } + + public VkConfigUser getConfigUser() { + return Optional.ofNullable(configUser) + .orElseThrow(() -> new ConfigException("Конфигурация для пользователя не найдена")); + + } + + public VkConfigService getConfigService() { + return Optional.ofNullable(configService) + .orElseThrow(() -> new ConfigException("Конфигурация сервиса не найдена")); + } + + public static Builder builder() { + return new VkConfig().new Builder(); + } + + public class Builder { + private Builder() { + + } + + public Builder configGroup(VkConfigGroup configGroup) { + VkConfig.this.configGroup = configGroup; + return this; + } + + public Builder configUser(VkConfigUser configUser) { + VkConfig.this.configUser = configUser; + return this; + } + + public Builder configService(VkConfigService configService) { + VkConfig.this.configService = configService; + return this; + } + + public VkConfig build() { + return VkConfig.this; + } + + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof VkConfig)) return false; + VkConfig vkConfig = (VkConfig) o; + return Objects.equals(configGroup, vkConfig.configGroup) && + Objects.equals(configUser, vkConfig.configUser) && + Objects.equals(configService, vkConfig.configService); + } + + @Override + public int hashCode() { + return Objects.hash(configGroup, configUser, configService); + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/config/VkConfigGroup.java b/src/main/java/org/sadtech/vkbot/core/config/VkConfigGroup.java new file mode 100644 index 0000000..1fa72f9 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/config/VkConfigGroup.java @@ -0,0 +1,56 @@ +package org.sadtech.vkbot.core.config; + +import java.util.Objects; + +public class VkConfigGroup { + + private String groupToken; + private Integer groupId; + + public String getGroupToken() { + return groupToken; + } + + public Integer getGroupId() { + return groupId; + } + + public static Builder builder() { + return new VkConfigGroup().new Builder(); + } + + public class Builder { + private Builder() { + + } + + public Builder groupId(Integer groupId) { + VkConfigGroup.this.groupId = groupId; + return this; + } + + public Builder groupToken(String groupToken) { + VkConfigGroup.this.groupToken = groupToken; + return this; + } + + public VkConfigGroup build() { + return VkConfigGroup.this; + } + + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof VkConfigGroup)) return false; + VkConfigGroup that = (VkConfigGroup) o; + return Objects.equals(groupToken, that.groupToken) && + Objects.equals(groupId, that.groupId); + } + + @Override + public int hashCode() { + return Objects.hash(groupToken, groupId); + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/config/VkConfigService.java b/src/main/java/org/sadtech/vkbot/core/config/VkConfigService.java new file mode 100644 index 0000000..f8015cd --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/config/VkConfigService.java @@ -0,0 +1,56 @@ +package org.sadtech.vkbot.core.config; + +import java.util.Objects; + +public class VkConfigService { + + private String serviceToken; + private Integer appId; + + public String getServiceToken() { + return serviceToken; + } + + public Integer getAppId() { + return appId; + } + + public static Builder builder() { + return new VkConfigService().new Builder(); + } + + public class Builder { + private Builder() { + + } + + public Builder serviceToken(String serviceToken) { + VkConfigService.this.serviceToken = serviceToken; + return this; + } + + public Builder appId(Integer appId) { + VkConfigService.this.appId = appId; + return this; + } + + public VkConfigService build() { + return VkConfigService.this; + } + + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof VkConfigService)) return false; + VkConfigService that = (VkConfigService) o; + return Objects.equals(serviceToken, that.serviceToken) && + Objects.equals(appId, that.appId); + } + + @Override + public int hashCode() { + return Objects.hash(serviceToken, appId); + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/VkConfigUser.java b/src/main/java/org/sadtech/vkbot/core/config/VkConfigUser.java similarity index 52% rename from src/main/java/org/sadtech/vkbot/core/VkConfigUser.java rename to src/main/java/org/sadtech/vkbot/core/config/VkConfigUser.java index 7912c09..e952dad 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkConfigUser.java +++ b/src/main/java/org/sadtech/vkbot/core/config/VkConfigUser.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core; +package org.sadtech.vkbot.core.config; import java.util.Objects; @@ -7,22 +7,44 @@ public class VkConfigUser { private Integer userId; private String token; - public Integer getUserId() { - return userId; + private VkConfigUser() { + } - public void setUserId(Integer userId) { - this.userId = userId; + public Integer getUserId() { + return userId; } public String getToken() { return token; } - public void setToken(String token) { - this.token = token; + public static Builder builder() { + return new VkConfigUser().new Builder(); } + public class Builder { + private Builder() { + + } + + public Builder userId(Integer userId) { + VkConfigUser.this.userId = userId; + return this; + } + + public Builder token(String token) { + VkConfigUser.this.token = token; + return this; + } + + public VkConfigUser build() { + return VkConfigUser.this; + } + + } + + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/org/sadtech/vkbot/core/VkConnect.java b/src/main/java/org/sadtech/vkbot/core/config/VkConnect.java similarity index 91% rename from src/main/java/org/sadtech/vkbot/core/VkConnect.java rename to src/main/java/org/sadtech/vkbot/core/config/VkConnect.java index 12e0579..3d32c51 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkConnect.java +++ b/src/main/java/org/sadtech/vkbot/core/config/VkConnect.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core; +package org.sadtech.vkbot.core.config; import com.vk.api.sdk.client.VkApiClient; import com.vk.api.sdk.client.actors.GroupActor; @@ -68,6 +68,6 @@ public class VkConnect { } private void initServiceActor() { - serviceActor = new ServiceActor(vkConfig.getConfigGroup().getAppId(), vkConfig.getConfigGroup().getServiceToken()); + serviceActor = new ServiceActor(vkConfig.getConfigService().getAppId(), vkConfig.getConfigService().getServiceToken()); } } diff --git a/src/main/java/org/sadtech/vkbot/core/convert/Convert.java b/src/main/java/org/sadtech/vkbot/core/convert/Convert.java index 792bd25..6b0c915 100644 --- a/src/main/java/org/sadtech/vkbot/core/convert/Convert.java +++ b/src/main/java/org/sadtech/vkbot/core/convert/Convert.java @@ -1,5 +1,6 @@ package org.sadtech.vkbot.core.convert; +@FunctionalInterface public interface Convert { C converting(T target); diff --git a/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java b/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java index 69a9677..0b4c60a 100644 --- a/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java +++ b/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java @@ -5,17 +5,23 @@ 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.domain.keyboard.button.KeyBoardButtonAccount; +import org.sadtech.bot.core.domain.keyboard.button.KeyBoardButtonText; +import org.sadtech.vkbot.core.config.VkConnect; import java.util.ArrayList; import java.util.List; -public final class KeyBoardConvert { +public class KeyBoardConvert { - private KeyBoardConvert() { + private final VkConnect vkConnect; + public KeyBoardConvert(VkConnect vkConnect) { + this.vkConnect = vkConnect; } - public static Keyboard convertKeyboard(KeyBoard keyboard) { + + public Keyboard convertKeyboard(KeyBoard keyboard) { Keyboard keyboardVk = new Keyboard(); keyboardVk.setOneTime(keyboard.isOneTime()); @@ -32,20 +38,58 @@ public final class KeyBoardConvert { return keyboardVk; } - private static KeyboardButton convertButton(KeyBoardButton button) { + private 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()); + switch (button.getType()) { + case TEXT: + KeyBoardButtonText buttonText = (KeyBoardButtonText) button; + buttonVk.setColor(convertColor(buttonText.getColor())); + break; + } + KeyboardButtonAction buttonActionVk = createActionButton(button); buttonVk.setAction(buttonActionVk); return buttonVk; } + private KeyboardButtonAction createActionButton(KeyBoardButton button) { + KeyboardButtonAction keyboardButtonAction = new KeyboardButtonAction(); + switch (button.getType()) { + case TEXT: + KeyBoardButtonText buttonText = (KeyBoardButtonText) button; + keyboardButtonAction.setType(KeyboardButtonActionType.TEXT); + keyboardButtonAction.setLabel(buttonText.getLabel()); + break; + case ACCOUNT: + KeyBoardButtonAccount buttonAccount = (KeyBoardButtonAccount) button; + keyboardButtonAction.setType(KeyboardButtonActionType.VKPAY); + keyboardButtonAction.setHash(createHash(buttonAccount)); + break; + } + return keyboardButtonAction; + } + + private String createHash(KeyBoardButtonAccount button) { + StringBuilder stringBuilder = new StringBuilder(); + if (button.getAmount()!=null) { + stringBuilder + .append("action=pay-to-group&amount=") + .append(button.getAmount()).append("&group_id=") + .append(vkConnect.getGroupActor().getGroupId()); + } else { + stringBuilder + .append("action=transfer-to-group") + .append(button.getAmount()).append("&group_id=") + .append(vkConnect.getGroupActor().getGroupId()); + } + if (button.getAccountId()!=null) { + stringBuilder.append("&description=").append(button.getAccountId()); + } + return stringBuilder.append("&aid=").append(vkConnect.getServiceActor().getId()).toString(); + } + private static KeyboardButtonColor convertColor(ButtonColor color) { KeyboardButtonColor buttonColorVk; switch (color) { diff --git a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java index 5ef5511..19555cf 100644 --- a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java +++ b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java @@ -35,6 +35,7 @@ public class MessageMailConvert implements Convert { return Geo.builder() .coordinate(geoVk.getCoordinates().getLatitude(), geoVk.getCoordinates().getLongitude()) + .city(geoVk.getPlace().getCity()) .build(); } diff --git a/src/main/java/org/sadtech/vkbot/core/convert/TopicCommentToBoardComment.java b/src/main/java/org/sadtech/vkbot/core/convert/TopicCommentToBoardComment.java index f3a9d3e..c3d1187 100644 --- a/src/main/java/org/sadtech/vkbot/core/convert/TopicCommentToBoardComment.java +++ b/src/main/java/org/sadtech/vkbot/core/convert/TopicCommentToBoardComment.java @@ -11,7 +11,7 @@ public class TopicCommentToBoardComment implements Convert { @@ -14,6 +15,33 @@ public abstract class AbstractBasketSubscribe { convert = (object) -> (C) object; } + protected abstract boolean check(S object); + + public void update(S object) { + C newObject = convert.converting(object); + if (!goNextSubscribe(newObject)) { + processing(newObject); + } + } + + private boolean goNextSubscribe(C object) { + AtomicBoolean flag = new AtomicBoolean(false); + if (prioritySubscribe != null && prioritySubscribe.check(object)) { + prioritySubscribe.update(object); + flag.set(true); + } else if (basketSubscribes != null) { + basketSubscribes.stream() + .filter(basketSubscribe -> basketSubscribe.check(object)) + .forEach(basketSubscribe -> { + basketSubscribe.update(object); + flag.set(true); + }); + } + return flag.get(); + } + + public abstract void processing(C object); + public Set getBasketSubscribes() { return basketSubscribes; } @@ -38,30 +66,4 @@ public abstract class AbstractBasketSubscribe { this.convert = convert; } - protected abstract boolean check(S object); - - private boolean goNextSubscribe(C object) { - boolean flag = false; - if (prioritySubscribe != null && prioritySubscribe.check(object)) { - prioritySubscribe.update(object); - flag = true; - } else if (basketSubscribes != null) { - for (AbstractBasketSubscribe basketSubscribe : basketSubscribes) { - if (basketSubscribe.check(object)) { - basketSubscribe.update(object); - flag = true; - } - } - } - return flag; - } - - public void update(S object) { - C newObject = convert.converting(object); - if (!goNextSubscribe(newObject)) { - processing(newObject); - } - } - - public abstract void processing(C object); } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java index d65c753..9f7fe3b 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java @@ -1,6 +1,7 @@ package org.sadtech.vkbot.core.distribution; import com.vk.api.sdk.objects.messages.Message; +import com.vk.api.sdk.objects.messages.MessageAttachmentType; import org.sadtech.bot.core.domain.BoxAnswer; import org.sadtech.bot.core.exception.PaymentException; import org.sadtech.bot.core.service.AccountService; @@ -21,24 +22,18 @@ public class AccountSubscribe extends AbstractBasketSubscribe this.sent = sent; } - public BoxAnswer getAnswerSuccessfulPayment() { - return answerSuccessfulPayment; - } - - public void setAnswerSuccessfulPayment(BoxAnswer answerSuccessfulPayment) { - this.answerSuccessfulPayment = answerSuccessfulPayment; - } - @Override protected boolean check(Message userMessage) { - return userMessage.getAttachments().size() > 0 && "Денежный перевод".equals(userMessage.getAttachments().get(0).getLink().getCaption()); + return userMessage.getAttachments().size() > 0 + && MessageAttachmentType.LINK.equals(userMessage.getAttachments().get(0).getType()) + && "Payment awaiting acceptance".equals(userMessage.getAttachments().get(0).getLink().getCaption()); } @Override public void processing(Message message) { if (message.getText() != null) { try { - Double valueSum = Double.valueOf(message.getAttachments().get(0).getLink().getTitle().split(" ")[0]); + Integer valueSum = Integer.valueOf(message.getAttachments().get(0).getLink().getTitle().split(" ")[0]); if (accountService.pay(Integer.valueOf(message.getText()), message.getPeerId(), valueSum) && answerSuccessfulPayment != null) { sent.send(message.getPeerId(), answerSuccessfulPayment); } @@ -48,4 +43,12 @@ public class AccountSubscribe extends AbstractBasketSubscribe } } } + + public BoxAnswer getAnswerSuccessfulPayment() { + return answerSuccessfulPayment; + } + + public void setAnswerSuccessfulPayment(BoxAnswer answerSuccessfulPayment) { + this.answerSuccessfulPayment = answerSuccessfulPayment; + } } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java index 6b918b8..363473c 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java @@ -55,9 +55,16 @@ public class BoardCommentSubscribe extends AbstractBasketSubscribe { + log.info("Новое событие от LongPoll\n" + object); + rawEventService.add(object); + }); lastTimeStamp = eventsResponse.getTs(); } catch (LongPollServerKeyExpiredException e) { - log.error(e.getStackTrace()); + log.error(e.getMessage()); longPollServer = getLongPollServer(); } catch (Exception e) { - log.error(e.getStackTrace()); + log.error(e.getMessage()); break; } } @@ -76,8 +72,12 @@ public class EventListenerVk implements Runnable { try { listen(); } catch (ClientException | ApiException e) { - log.error(e); + log.error(e.getMessage()); } } + public RawEventService getRawEventService() { + return rawEventService; + } + } diff --git a/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java index 133f10f..076e3e6 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java @@ -6,10 +6,13 @@ import com.vk.api.sdk.client.actors.UserActor; import com.vk.api.sdk.exceptions.ApiException; import com.vk.api.sdk.exceptions.ClientException; import org.sadtech.bot.core.domain.BoxAnswer; +import org.sadtech.bot.core.domain.keyboard.KeyBoardButton; +import org.sadtech.bot.core.domain.keyboard.KeyBoardLine; +import org.sadtech.bot.core.domain.keyboard.button.KeyBoardButtonText; import org.sadtech.bot.core.exception.MailSendException; import org.sadtech.bot.core.service.sender.Sent; -import org.sadtech.vkbot.core.VkConnect; -import org.sadtech.vkbot.core.VkInsertData; +import org.sadtech.vkbot.core.config.VkConnect; +import org.sadtech.vkbot.core.utils.VkInsertData; public class BoardCommentSenderVk implements Sent { @@ -33,7 +36,22 @@ public class BoardCommentSenderVk implements Sent { @Override public void send(Integer contentId, Integer personId, BoxAnswer boxAnswer) { try { - vkApiClient.board().createComment(userActor, groupActor.getGroupId(), contentId).message(vkInsertData.insertWords(boxAnswer.getMessage(), personId)).fromGroup(true).execute(); + StringBuilder insertAnswer = new StringBuilder(vkInsertData.insertWords(boxAnswer.getMessage(), personId)); + if (boxAnswer.getKeyboard() != null) { + insertAnswer.append("\n\nМеню:\n"); + for (KeyBoardLine keyBoardLine : boxAnswer.getKeyboard().getKeyBoardLines()) { + for (KeyBoardButton keyBoardButton : keyBoardLine.getKeyBoardButtons()) { + switch (keyBoardButton.getType()) { + case TEXT: + insertAnswer.append("- ").append(((KeyBoardButtonText)keyBoardButton).getLabel()).append("\n"); + } + + } + } + } + + vkApiClient.board().createComment(userActor, groupActor.getGroupId(), contentId) + .message(insertAnswer.toString()).fromGroup(true).execute(); } catch (ApiException | ClientException e) { e.printStackTrace(); } 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 07e6c80..556ba28 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java @@ -1,6 +1,5 @@ package org.sadtech.vkbot.core.sender; -import com.google.gson.Gson; import com.vk.api.sdk.client.VkApiClient; import com.vk.api.sdk.client.actors.GroupActor; import com.vk.api.sdk.exceptions.ApiException; @@ -9,10 +8,11 @@ import com.vk.api.sdk.objects.messages.Keyboard; 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.KeyBoard; import org.sadtech.bot.core.service.sender.Sent; -import org.sadtech.vkbot.core.VkConnect; -import org.sadtech.vkbot.core.VkInsertData; +import org.sadtech.vkbot.core.config.VkConnect; import org.sadtech.vkbot.core.convert.KeyBoardConvert; +import org.sadtech.vkbot.core.utils.VkInsertData; import java.util.Collections; import java.util.concurrent.ThreadLocalRandom; @@ -23,14 +23,14 @@ public class MailSenderVk implements Sent { private final VkApiClient vkApiClient; private final GroupActor groupActor; - private final VkInsertData vkInsertData; - private final Gson gson = new Gson(); + private final KeyBoardConvert keyBoardConvert; public MailSenderVk(VkConnect vkConnect) { this.vkApiClient = vkConnect.getVkApiClient(); this.groupActor = vkConnect.getGroupActor(); this.vkInsertData = new VkInsertData(vkConnect); + keyBoardConvert = new KeyBoardConvert(vkConnect); } @Override @@ -39,34 +39,37 @@ public class MailSenderVk implements Sent { sendMessage(messagesSendQuery); } - @Override - public void send(Integer integer, Integer integer1, BoxAnswer boxAnswer) { - - } - 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) { - messages.keyboard(KeyBoardConvert.convertKeyboard(boxAnswer.getKeyboard())); - } else { - Keyboard keyboard = new Keyboard(); - keyboard.setOneTime(true); - keyboard.setButtons(Collections.EMPTY_LIST); - messages.keyboard(keyboard); - } + MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId) + .message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId)) + .randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE)); + messages.keyboard(convertKeyBoard(boxAnswer.getKeyboard())); + if (boxAnswer.getCoordinates() != null) { messages.lat(boxAnswer.getCoordinates().getLatitude()).lng(boxAnswer.getCoordinates().getLongitude()); } if (boxAnswer.getStickerId() != null) { try { - vkApiClient.messages().send(groupActor).peerId(peerId).stickerId(boxAnswer.getStickerId()).execute(); + vkApiClient.messages().send(groupActor).peerId(peerId).stickerId(boxAnswer.getStickerId()) + .randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE)).execute(); } catch (ApiException | ClientException e) { - log.error(e); + log.error(e.getMessage()); } } return messages; } + private Keyboard convertKeyBoard(KeyBoard keyboard) { + if (keyboard != null) { + return keyBoardConvert.convertKeyboard(keyboard); + } else { + Keyboard keyboardVk = new Keyboard(); + keyboardVk.setOneTime(true); + keyboardVk.setButtons(Collections.EMPTY_LIST); + return keyboardVk; + } + } + private void sendMessage(MessagesSendQuery messages) { try { @@ -76,5 +79,9 @@ public class MailSenderVk implements Sent { } } + @Override + public void send(Integer integer, Integer integer1, BoxAnswer boxAnswer) { + + } } diff --git a/src/main/java/org/sadtech/vkbot/core/VkApi.java b/src/main/java/org/sadtech/vkbot/core/utils/VkApi.java similarity index 90% rename from src/main/java/org/sadtech/vkbot/core/VkApi.java rename to src/main/java/org/sadtech/vkbot/core/utils/VkApi.java index eebb050..19a4423 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkApi.java +++ b/src/main/java/org/sadtech/vkbot/core/utils/VkApi.java @@ -1,16 +1,17 @@ -package org.sadtech.vkbot.core; +package org.sadtech.vkbot.core.utils; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.vk.api.sdk.client.VkApiClient; -import com.vk.api.sdk.client.actors.GroupActor; +import com.vk.api.sdk.client.actors.ServiceActor; import com.vk.api.sdk.exceptions.ApiException; import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.objects.users.Fields; import com.vk.api.sdk.objects.users.UserMin; import com.vk.api.sdk.objects.users.UserXtrCounters; import org.apache.log4j.Logger; +import org.sadtech.vkbot.core.config.VkConnect; import java.util.List; @@ -19,11 +20,11 @@ public class VkApi { private static final Logger log = Logger.getLogger(String.valueOf(VkApi.class)); private final VkApiClient vk; - private final GroupActor actor; + private final ServiceActor actor; public VkApi(VkConnect vkConnect) { vk = vkConnect.getVkApiClient(); - actor = vkConnect.getGroupActor(); + actor = vkConnect.getServiceActor(); } public UserMin getUserMini(Integer id) { diff --git a/src/main/java/org/sadtech/vkbot/core/VkInsertData.java b/src/main/java/org/sadtech/vkbot/core/utils/VkInsertData.java similarity index 80% rename from src/main/java/org/sadtech/vkbot/core/VkInsertData.java rename to src/main/java/org/sadtech/vkbot/core/utils/VkInsertData.java index bbaa9bd..0ae7c99 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkInsertData.java +++ b/src/main/java/org/sadtech/vkbot/core/utils/VkInsertData.java @@ -1,4 +1,6 @@ -package org.sadtech.vkbot.core; +package org.sadtech.vkbot.core.utils; + +import org.sadtech.vkbot.core.config.VkConnect; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -11,12 +13,12 @@ public class VkInsertData { this.vkApi = new VkApi(vkConnect); } - public String insertWords(String message, Integer idUser) { + public String insertWords(String message, Integer personId) { Pattern pattern = Pattern.compile("%(\\w+)%"); Matcher m = pattern.matcher(message); StringBuffer result = new StringBuffer(); while (m.find()) { - m.appendReplacement(result, insert(m.group(0), idUser)); + m.appendReplacement(result, insert(m.group(0), personId)); } m.appendTail(result); return result.toString();