From 8e8d09350a5e5aaa688734a4e4e0cf2b6ac6a739 Mon Sep 17 00:00:00 2001 From: upagge Date: Sat, 16 May 2020 09:53:28 +0300 Subject: [PATCH] =?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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 33 ++++ pom.xml | 159 ++++++++++++++---- .../bot/sdk}/MessageAutoresponderVk.java | 4 +- .../sadtech/vk/bot/sdk/config/VkConfig.java | 43 +++++ .../vk/bot/sdk/config/VkConfigGroup.java | 19 +++ .../vk/bot/sdk/config/VkConfigService.java | 19 +++ .../vk/bot/sdk/config/VkConfigUser.java | 19 +++ .../sadtech/vk/bot/sdk/config/VkConnect.java | 64 +++++++ .../bot/sdk}/domain/jpa/JsonObjectId.java | 5 +- .../vk/bot/sdk/exception/ConfigException.java | 9 + .../sdk}/repository/RawEventRepository.java | 2 +- .../impl/RawEventRepositorySet.java | 4 +- .../impl/jpa/RawEventRepositoryJpaImpl.java | 8 +- .../repository/jpa/RawEventRepositoryJpa.java | 4 +- .../bot/sdk}/sender/MailSenderVk.java | 20 ++- .../bot/sdk}/service/RawEventService.java | 2 +- .../convert/JsonObjectToMessageConvert.java | 19 +++ .../sdk/service}/convert/KeyBoardConvert.java | 27 +-- .../service}/convert/MessageMailConvert.java | 10 +- .../distribution/EventDistributor.java | 16 +- .../subscriber/AbstractBasketSubscribe.java | 40 +---- .../subscriber/AccountSubscribe.java | 9 +- .../subscriber/BasketSubscribeService.java | 9 + .../subscriber/MailSubscriber.java | 35 ++++ .../service/impl/RawEventServiceImpl.java | 10 +- .../service}/listener/EventListenerVk.java | 20 ++- .../core => vk/bot/sdk}/utils/VkApi.java | 29 ++-- .../vk/bot/sdk/utils/VkInsertData.java | 39 +++++ .../vkbot/core/config/VkBotJpaConfig.java | 15 -- .../subscriber/MailSubscriber.java | 35 ---- 30 files changed, 537 insertions(+), 190 deletions(-) create mode 100644 README.md rename src/main/java/org/sadtech/{vkbot => vk/bot/sdk}/MessageAutoresponderVk.java (89%) create mode 100644 src/main/java/org/sadtech/vk/bot/sdk/config/VkConfig.java create mode 100644 src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigGroup.java create mode 100644 src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigService.java create mode 100644 src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigUser.java create mode 100644 src/main/java/org/sadtech/vk/bot/sdk/config/VkConnect.java rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk}/domain/jpa/JsonObjectId.java (84%) create mode 100644 src/main/java/org/sadtech/vk/bot/sdk/exception/ConfigException.java rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk}/repository/RawEventRepository.java (87%) rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk}/repository/impl/RawEventRepositorySet.java (84%) rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk}/repository/impl/jpa/RawEventRepositoryJpaImpl.java (86%) rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk}/repository/jpa/RawEventRepositoryJpa.java (75%) rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk}/sender/MailSenderVk.java (87%) rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk}/service/RawEventService.java (89%) create mode 100644 src/main/java/org/sadtech/vk/bot/sdk/service/convert/JsonObjectToMessageConvert.java rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk/service}/convert/KeyBoardConvert.java (82%) rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk/service}/convert/MessageMailConvert.java (93%) rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk/service}/distribution/EventDistributor.java (56%) rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk/service}/distribution/subscriber/AbstractBasketSubscribe.java (52%) rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk/service}/distribution/subscriber/AccountSubscribe.java (90%) create mode 100644 src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/BasketSubscribeService.java create mode 100644 src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/MailSubscriber.java rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk}/service/impl/RawEventServiceImpl.java (65%) rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk/service}/listener/EventListenerVk.java (81%) rename src/main/java/org/sadtech/{vkbot/core => vk/bot/sdk}/utils/VkApi.java (71%) create mode 100644 src/main/java/org/sadtech/vk/bot/sdk/utils/VkInsertData.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/config/VkBotJpaConfig.java delete mode 100644 src/main/java/org/sadtech/vkbot/core/distribution/subscriber/MailSubscriber.java diff --git a/README.md b/README.md new file mode 100644 index 0000000..02f8266 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# VK Bot + +С помощью этой библиотеки вы можете написать своего бота для социальной сети ВКонтакте. + +Если вы хотите написать бота для другой социальной сети, воспользуйтесь +[этим проектом](https://github.com/uPagge/social-bot). + +## Dependency + +maven: +``` + + org.sadtech.vkbot + vkbot-sdk + 0.2.2-SNAPSHOT + +``` + +## Примеры ботов + +1. [Простой пример](https://github.com/uPagge/simple-vkbot/tree/master) использования всех юнитов +2. BitbucketBot - позволяет настроить уведомления для пользователей standalone Bitbucket Server. + +## Quick Start + +Для быстрой конфигурации воспользуйтесь проектом [VK BOT QUICK](), который сам создаст все бины, кроме юнитов. +В данном проекте +предлагаются более гибкие настройки. + +## Докуменация + +Рекомендуем ознакомиться с [докуменацией проекта](https://github.com/uPagge/social-bot), на основании которого написан +данный модуль. diff --git a/pom.xml b/pom.xml index fde4df9..d2ff679 100644 --- a/pom.xml +++ b/pom.xml @@ -4,11 +4,133 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - org.sadtech.vkbot - vkbot-core - 0.2.2-SNAPSHOT + org.sadtech.vk.bot + vkbot-sdk + 1.0.0-RELEASE jar + VK Bot + Библиотека для написания ботов для социальной сети ВКонтакте. + https://github.com/uPagge/vk-bot + + + scm:git:https://github.com/uPagge/vk-bot.git + https://github.com/uPagge/vk-bot + scm:git:https://github.com/uPagge/vk-bot.git + + + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + + + + 1.0.0-RELEASE + + 1.0.6 + 2.1.3.RELEASE + 1.18.12 + + + + + com.vk.api + sdk + ${vksdk.ver} + + + org.sadtech.social + social-bot + ${social.bot.ver} + + + org.springframework.data + spring-data-jpa + ${spring.jpa.ver} + + + org.projectlombok + lombok + ${lombock.ver} + provided + + + + + + release + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.7 + true + + ossrh + https://oss.sonatype.org/ + true + + + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.5 + + + sign-artifacts + verify + + sign + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.7 + true + + ossrh + https://oss.sonatype.org/ + true + + + + + + + @@ -22,39 +144,12 @@ - - 0.6.6-SNAPSHOT - 1.0.6 - - - - - com.vk.api - sdk - ${vksdk.ver} - - - org.sadtech.social - social-autoresponder - ${social.autoresponder.ver} - - - org.springframework.data - spring-data-jpa - 2.1.3.RELEASE - - - org.springframework.boot - spring-boot-autoconfigure - 2.1.3.RELEASE - - - - + uPagge Struchkov Mark upagge@mail.ru + SADTECH diff --git a/src/main/java/org/sadtech/vkbot/MessageAutoresponderVk.java b/src/main/java/org/sadtech/vk/bot/sdk/MessageAutoresponderVk.java similarity index 89% rename from src/main/java/org/sadtech/vkbot/MessageAutoresponderVk.java rename to src/main/java/org/sadtech/vk/bot/sdk/MessageAutoresponderVk.java index 2292d7c..eadaf13 100644 --- a/src/main/java/org/sadtech/vkbot/MessageAutoresponderVk.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/MessageAutoresponderVk.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot; +package org.sadtech.vk.bot.sdk; import org.sadtech.autoresponder.repository.UnitPointerRepository; import org.sadtech.social.bot.GeneralAutoResponder; @@ -11,7 +11,7 @@ import java.util.Set; public class MessageAutoresponderVk extends GeneralAutoResponder { - public MessageAutoresponderVk(Set menuUnit, Sending sending, MessageService messageService, UnitPointerRepository unitPointerRepository) { + public MessageAutoresponderVk(Set menuUnit, Sending sending, MessageService messageService, UnitPointerRepository unitPointerRepository) { super(menuUnit, sending, messageService, unitPointerRepository); } diff --git a/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfig.java b/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfig.java new file mode 100644 index 0000000..1165957 --- /dev/null +++ b/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfig.java @@ -0,0 +1,43 @@ +package org.sadtech.vk.bot.sdk.config; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.sadtech.vk.bot.sdk.exception.ConfigException; + +import java.util.Optional; + +/** + * + * Класс для сохранения информации о конфигурации, необходимых для создания {@link VkConnect}. + * + * @author upagge + */ +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +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("Конфигурация сервиса не найдена, создайте класс VkConfigService!")); + } + +} diff --git a/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigGroup.java b/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigGroup.java new file mode 100644 index 0000000..3425c64 --- /dev/null +++ b/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigGroup.java @@ -0,0 +1,19 @@ +package org.sadtech.vk.bot.sdk.config; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Setter +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class VkConfigGroup { + + private String groupToken; + private Integer groupId; + +} diff --git a/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigService.java b/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigService.java new file mode 100644 index 0000000..c015fc3 --- /dev/null +++ b/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigService.java @@ -0,0 +1,19 @@ +package org.sadtech.vk.bot.sdk.config; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Setter +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class VkConfigService { + + private Integer appId; + private String serviceToken; + +} diff --git a/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigUser.java b/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigUser.java new file mode 100644 index 0000000..6027c87 --- /dev/null +++ b/src/main/java/org/sadtech/vk/bot/sdk/config/VkConfigUser.java @@ -0,0 +1,19 @@ +package org.sadtech.vk.bot.sdk.config; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Setter +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class VkConfigUser { + + private Integer userId; + private String token; + +} diff --git a/src/main/java/org/sadtech/vk/bot/sdk/config/VkConnect.java b/src/main/java/org/sadtech/vk/bot/sdk/config/VkConnect.java new file mode 100644 index 0000000..0f859a9 --- /dev/null +++ b/src/main/java/org/sadtech/vk/bot/sdk/config/VkConnect.java @@ -0,0 +1,64 @@ +package org.sadtech.vk.bot.sdk.config; + +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; +import lombok.RequiredArgsConstructor; + +import java.util.Optional; + +/** + * + * Используется для сохранения объектов конфигурации, необходимых для работы SDK VK. + * + * @author upagge + */ +@RequiredArgsConstructor +public class VkConnect { + + private final VkConfig vkConfig; + + private VkApiClient vkApiClient; + private GroupActor groupActor; + private ServiceActor serviceActor; + private UserActor userActor; + + public VkApiClient getVkApiClient() { + return Optional.ofNullable(vkApiClient).orElseGet(this::initVkApiClient); + } + + public GroupActor getGroupActor() { + return Optional.ofNullable(groupActor).orElseGet(this::initGroupActor); + } + + public UserActor getUserActor() { + return Optional.ofNullable(userActor).orElseGet(this::initUserActor); + } + + public ServiceActor getServiceActor() { + return Optional.ofNullable(serviceActor).orElseGet(this::initServiceActor); + } + + private UserActor initUserActor() { + userActor = new UserActor(vkConfig.getConfigUser().getUserId(), vkConfig.getConfigUser().getToken()); + return userActor; + } + + private VkApiClient initVkApiClient() { + vkApiClient = new VkApiClient(HttpTransportClient.getInstance()); + return vkApiClient; + } + + private GroupActor initGroupActor() { + groupActor = new GroupActor(vkConfig.getConfigGroup().getGroupId(), vkConfig.getConfigGroup().getGroupToken()); + return groupActor; + } + + private ServiceActor initServiceActor() { + serviceActor = new ServiceActor(vkConfig.getConfigService().getAppId(), vkConfig.getConfigService().getServiceToken()); + return serviceActor; + } + +} diff --git a/src/main/java/org/sadtech/vkbot/core/domain/jpa/JsonObjectId.java b/src/main/java/org/sadtech/vk/bot/sdk/domain/jpa/JsonObjectId.java similarity index 84% rename from src/main/java/org/sadtech/vkbot/core/domain/jpa/JsonObjectId.java rename to src/main/java/org/sadtech/vk/bot/sdk/domain/jpa/JsonObjectId.java index 0adbae7..5de9619 100644 --- a/src/main/java/org/sadtech/vkbot/core/domain/jpa/JsonObjectId.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/domain/jpa/JsonObjectId.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.domain.jpa; +package org.sadtech.vk.bot.sdk.domain.jpa; import lombok.Data; import lombok.EqualsAndHashCode; @@ -10,8 +10,6 @@ import javax.persistence.Entity; import javax.persistence.Table; /** - * TODO: Добавить описание класса. - * * @author upagge [28/07/2019] */ @Data @@ -27,4 +25,5 @@ public class JsonObjectId extends BasicEntity { public JsonObjectId(String json) { this.json = json; } + } diff --git a/src/main/java/org/sadtech/vk/bot/sdk/exception/ConfigException.java b/src/main/java/org/sadtech/vk/bot/sdk/exception/ConfigException.java new file mode 100644 index 0000000..b677650 --- /dev/null +++ b/src/main/java/org/sadtech/vk/bot/sdk/exception/ConfigException.java @@ -0,0 +1,9 @@ +package org.sadtech.vk.bot.sdk.exception; + +public class ConfigException extends RuntimeException { + + public ConfigException(String message) { + super(message); + } + +} diff --git a/src/main/java/org/sadtech/vkbot/core/repository/RawEventRepository.java b/src/main/java/org/sadtech/vk/bot/sdk/repository/RawEventRepository.java similarity index 87% rename from src/main/java/org/sadtech/vkbot/core/repository/RawEventRepository.java rename to src/main/java/org/sadtech/vk/bot/sdk/repository/RawEventRepository.java index 7c5b316..b4fd951 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/RawEventRepository.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/repository/RawEventRepository.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.repository; +package org.sadtech.vk.bot.sdk.repository; import com.google.gson.JsonObject; diff --git a/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositorySet.java b/src/main/java/org/sadtech/vk/bot/sdk/repository/impl/RawEventRepositorySet.java similarity index 84% rename from src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositorySet.java rename to src/main/java/org/sadtech/vk/bot/sdk/repository/impl/RawEventRepositorySet.java index a3d271a..6622550 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/impl/RawEventRepositorySet.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/repository/impl/RawEventRepositorySet.java @@ -1,7 +1,7 @@ -package org.sadtech.vkbot.core.repository.impl; +package org.sadtech.vk.bot.sdk.repository.impl; import com.google.gson.JsonObject; -import org.sadtech.vkbot.core.repository.RawEventRepository; +import org.sadtech.vk.bot.sdk.repository.RawEventRepository; import java.util.HashSet; import java.util.Set; diff --git a/src/main/java/org/sadtech/vkbot/core/repository/impl/jpa/RawEventRepositoryJpaImpl.java b/src/main/java/org/sadtech/vk/bot/sdk/repository/impl/jpa/RawEventRepositoryJpaImpl.java similarity index 86% rename from src/main/java/org/sadtech/vkbot/core/repository/impl/jpa/RawEventRepositoryJpaImpl.java rename to src/main/java/org/sadtech/vk/bot/sdk/repository/impl/jpa/RawEventRepositoryJpaImpl.java index cea6a23..f0d31c3 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/impl/jpa/RawEventRepositoryJpaImpl.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/repository/impl/jpa/RawEventRepositoryJpaImpl.java @@ -1,10 +1,10 @@ -package org.sadtech.vkbot.core.repository.impl.jpa; +package org.sadtech.vk.bot.sdk.repository.impl.jpa; import com.google.gson.Gson; import com.google.gson.JsonObject; -import org.sadtech.vkbot.core.domain.jpa.JsonObjectId; -import org.sadtech.vkbot.core.repository.RawEventRepository; -import org.sadtech.vkbot.core.repository.jpa.RawEventRepositoryJpa; +import org.sadtech.vk.bot.sdk.domain.jpa.JsonObjectId; +import org.sadtech.vk.bot.sdk.repository.RawEventRepository; +import org.sadtech.vk.bot.sdk.repository.jpa.RawEventRepositoryJpa; import java.util.List; import java.util.Set; diff --git a/src/main/java/org/sadtech/vkbot/core/repository/jpa/RawEventRepositoryJpa.java b/src/main/java/org/sadtech/vk/bot/sdk/repository/jpa/RawEventRepositoryJpa.java similarity index 75% rename from src/main/java/org/sadtech/vkbot/core/repository/jpa/RawEventRepositoryJpa.java rename to src/main/java/org/sadtech/vk/bot/sdk/repository/jpa/RawEventRepositoryJpa.java index 669f28e..86c44ab 100644 --- a/src/main/java/org/sadtech/vkbot/core/repository/jpa/RawEventRepositoryJpa.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/repository/jpa/RawEventRepositoryJpa.java @@ -1,6 +1,6 @@ -package org.sadtech.vkbot.core.repository.jpa; +package org.sadtech.vk.bot.sdk.repository.jpa; -import org.sadtech.vkbot.core.domain.jpa.JsonObjectId; +import org.sadtech.vk.bot.sdk.domain.jpa.JsonObjectId; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java b/src/main/java/org/sadtech/vk/bot/sdk/sender/MailSenderVk.java similarity index 87% rename from src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java rename to src/main/java/org/sadtech/vk/bot/sdk/sender/MailSenderVk.java index 1fff4d4..336764c 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/sender/MailSenderVk.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.sender; +package org.sadtech.vk.bot.sdk.sender; import com.vk.api.sdk.client.VkApiClient; import com.vk.api.sdk.client.actors.GroupActor; @@ -6,14 +6,15 @@ import com.vk.api.sdk.exceptions.ApiException; import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.objects.messages.Keyboard; import com.vk.api.sdk.queries.messages.MessagesSendQuery; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.sadtech.social.core.domain.BoxAnswer; import org.sadtech.social.core.domain.keyboard.KeyBoard; import org.sadtech.social.core.service.sender.SendType; import org.sadtech.social.core.service.sender.Sending; -import org.sadtech.vkbot.config.VkConnect; -import org.sadtech.vkbot.core.convert.KeyBoardConvert; -import org.sadtech.vkbot.utils.VkInsertData; +import org.sadtech.vk.bot.sdk.config.VkConnect; +import org.sadtech.vk.bot.sdk.utils.VkInsertData; +import org.springframework.core.convert.ConversionService; import java.util.Collections; import java.util.concurrent.ThreadLocalRandom; @@ -24,24 +25,25 @@ public class MailSenderVk implements Sending { private final VkApiClient vkApiClient; private final GroupActor groupActor; private final VkInsertData vkInsertData; - private final KeyBoardConvert keyBoardConvert; + + @Setter + private ConversionService conversionService; public MailSenderVk(VkConnect vkConnect) { this.vkApiClient = vkConnect.getVkApiClient(); this.groupActor = vkConnect.getGroupActor(); this.vkInsertData = new VkInsertData(vkConnect); - keyBoardConvert = new KeyBoardConvert(vkConnect); } @Override public void send(Long personId, BoxAnswer boxAnswer) { - MessagesSendQuery messagesSendQuery = createMessage(boxAnswer, personId.longValue()); + MessagesSendQuery messagesSendQuery = createMessage(boxAnswer, personId); sendMessage(messagesSendQuery); } private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Long peerId) { MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId.intValue()) - .message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId.intValue())) + .message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId)) .randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE)); messages.keyboard(convertKeyBoard(boxAnswer.getKeyBoard())); @@ -61,7 +63,7 @@ public class MailSenderVk implements Sending { private Keyboard convertKeyBoard(KeyBoard keyboard) { if (keyboard != null) { - return keyBoardConvert.convertKeyboard(keyboard); + return conversionService.convert(keyboard, Keyboard.class); } else { Keyboard keyboardVk = new Keyboard(); keyboardVk.setOneTime(true); diff --git a/src/main/java/org/sadtech/vkbot/core/service/RawEventService.java b/src/main/java/org/sadtech/vk/bot/sdk/service/RawEventService.java similarity index 89% rename from src/main/java/org/sadtech/vkbot/core/service/RawEventService.java rename to src/main/java/org/sadtech/vk/bot/sdk/service/RawEventService.java index 27d8279..b0097a7 100644 --- a/src/main/java/org/sadtech/vkbot/core/service/RawEventService.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/service/RawEventService.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.service; +package org.sadtech.vk.bot.sdk.service; import com.google.gson.JsonObject; diff --git a/src/main/java/org/sadtech/vk/bot/sdk/service/convert/JsonObjectToMessageConvert.java b/src/main/java/org/sadtech/vk/bot/sdk/service/convert/JsonObjectToMessageConvert.java new file mode 100644 index 0000000..2cb11ed --- /dev/null +++ b/src/main/java/org/sadtech/vk/bot/sdk/service/convert/JsonObjectToMessageConvert.java @@ -0,0 +1,19 @@ +package org.sadtech.vk.bot.sdk.service.convert; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.vk.api.sdk.objects.messages.Message; +import org.springframework.core.convert.converter.Converter; +import org.springframework.stereotype.Component; + +@Component +public class JsonObjectToMessageConvert implements Converter { + + private final Gson gson = new Gson(); + + @Override + public Message convert(JsonObject source) { + return gson.fromJson(source.get("object"), Message.class); + } + +} diff --git a/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java b/src/main/java/org/sadtech/vk/bot/sdk/service/convert/KeyBoardConvert.java similarity index 82% rename from src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java rename to src/main/java/org/sadtech/vk/bot/sdk/service/convert/KeyBoardConvert.java index 2426fd0..cb06609 100644 --- a/src/main/java/org/sadtech/vkbot/core/convert/KeyBoardConvert.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/service/convert/KeyBoardConvert.java @@ -1,5 +1,7 @@ -package org.sadtech.vkbot.core.convert; +package org.sadtech.vk.bot.sdk.service.convert; +import com.vk.api.sdk.client.actors.GroupActor; +import com.vk.api.sdk.client.actors.ServiceActor; import com.vk.api.sdk.objects.messages.Keyboard; import com.vk.api.sdk.objects.messages.KeyboardButton; import com.vk.api.sdk.objects.messages.KeyboardButtonAction; @@ -11,21 +13,26 @@ import org.sadtech.social.core.domain.keyboard.KeyBoardButton; import org.sadtech.social.core.domain.keyboard.KeyBoardLine; import org.sadtech.social.core.domain.keyboard.button.KeyBoardButtonAccount; import org.sadtech.social.core.domain.keyboard.button.KeyBoardButtonText; -import org.sadtech.vkbot.config.VkConnect; +import org.sadtech.vk.bot.sdk.config.VkConnect; +import org.springframework.core.convert.converter.Converter; +import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; -public class KeyBoardConvert { +@Component +public class KeyBoardConvert implements Converter { - private final VkConnect vkConnect; + private final GroupActor groupActor; + private final ServiceActor serviceActor; public KeyBoardConvert(VkConnect vkConnect) { - this.vkConnect = vkConnect; + this.groupActor = vkConnect.getGroupActor(); + this.serviceActor = vkConnect.getServiceActor(); } - - public Keyboard convertKeyboard(KeyBoard keyboard) { + @Override + public Keyboard convert(KeyBoard keyboard) { Keyboard keyboardVk = new Keyboard(); keyboardVk.setOneTime(keyboard.isOneTime()); @@ -81,17 +88,17 @@ public class KeyBoardConvert { stringBuilder .append("action=pay-to-group&amount=") .append(button.getAmount()).append("&group_id=") - .append(vkConnect.getGroupActor().getGroupId()); + .append(groupActor.getGroupId()); } else { stringBuilder .append("action=transfer-to-group") .append(button.getAmount()).append("&group_id=") - .append(vkConnect.getGroupActor().getGroupId()); + .append(groupActor.getGroupId()); } if (button.getAccountId() != null) { stringBuilder.append("&description=").append(button.getAccountId()); } - return stringBuilder.append("&aid=").append(vkConnect.getServiceActor().getId()).toString(); + return stringBuilder.append("&aid=").append(serviceActor.getId()).toString(); } private static KeyboardButtonColor convertColor(ButtonColor color) { diff --git a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java b/src/main/java/org/sadtech/vk/bot/sdk/service/convert/MessageMailConvert.java similarity index 93% rename from src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java rename to src/main/java/org/sadtech/vk/bot/sdk/service/convert/MessageMailConvert.java index c5a1c2f..f858235 100644 --- a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/service/convert/MessageMailConvert.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.convert; +package org.sadtech.vk.bot.sdk.service.convert; import com.vk.api.sdk.objects.messages.ForeignMessage; import com.vk.api.sdk.objects.messages.Message; @@ -7,7 +7,8 @@ import org.sadtech.social.core.domain.content.Mail; import org.sadtech.social.core.domain.content.attachment.Attachment; import org.sadtech.social.core.domain.content.attachment.AudioMessage; import org.sadtech.social.core.domain.content.attachment.Geo; -import org.sadtech.vkbot.convert.Convert; +import org.springframework.core.convert.converter.Converter; +import org.springframework.stereotype.Component; import java.time.Instant; import java.time.LocalDateTime; @@ -15,10 +16,11 @@ import java.util.List; import java.util.TimeZone; import java.util.stream.Collectors; -public class MessageMailConvert implements Convert { +@Component +public class MessageMailConvert implements Converter { @Override - public Mail converting(Message message) { + public Mail convert(Message message) { Mail mail = new Mail(); mail.setText(message.getText()); mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java b/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/EventDistributor.java similarity index 56% rename from src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java rename to src/main/java/org/sadtech/vk/bot/sdk/service/distribution/EventDistributor.java index 44c4343..939352a 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/EventDistributor.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/EventDistributor.java @@ -1,27 +1,22 @@ -package org.sadtech.vkbot.core.distribution; +package org.sadtech.vk.bot.sdk.service.distribution; import com.google.gson.JsonObject; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.sadtech.vkbot.core.distribution.subscriber.AbstractBasketSubscribe; -import org.sadtech.vkbot.core.service.RawEventService; +import org.sadtech.vk.bot.sdk.service.RawEventService; +import org.sadtech.vk.bot.sdk.service.distribution.subscriber.AbstractBasketSubscribe; -import java.util.Optional; import java.util.Set; @Slf4j @RequiredArgsConstructor -public class EventDistributor implements Runnable { +public class EventDistributor { private final RawEventService rawEventService; private final Set> basketSubscribes; - @Override public void run() { - while (true) { - Optional.ofNullable(rawEventService.getNewEvent()) - .ifPresent(events -> events.forEach(this::goNextSubscribe)); - } + rawEventService.getNewEvent().forEach(this::goNextSubscribe); } private void goNextSubscribe(JsonObject object) { @@ -29,4 +24,5 @@ public class EventDistributor implements Runnable { .filter(basketSubscribe -> basketSubscribe.check(object)) .forEach(basketSubscribe -> basketSubscribe.update(object)); } + } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AbstractBasketSubscribe.java b/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/AbstractBasketSubscribe.java similarity index 52% rename from src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AbstractBasketSubscribe.java rename to src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/AbstractBasketSubscribe.java index b17fe33..de889bd 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AbstractBasketSubscribe.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/AbstractBasketSubscribe.java @@ -1,29 +1,27 @@ -package org.sadtech.vkbot.core.distribution.subscriber; +package org.sadtech.vk.bot.sdk.service.distribution.subscriber; -import org.sadtech.vkbot.convert.Convert; +import lombok.Setter; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; -public abstract class AbstractBasketSubscribe { +@Setter +public abstract class AbstractBasketSubscribe implements BasketSubscribeService { private Set basketSubscribes; private AbstractBasketSubscribe prioritySubscribe; - protected Convert convert; - - public AbstractBasketSubscribe() { - convert = (object) -> (C) object; - } public abstract boolean check(S object); public void update(S object) { - C newObject = convert.converting(object); + C newObject = convert(object); if (!goNextSubscribe(newObject)) { processing(newObject); } } + public abstract C convert(S object); + private boolean goNextSubscribe(C object) { AtomicBoolean flag = new AtomicBoolean(false); if (prioritySubscribe != null && prioritySubscribe.check(object)) { @@ -42,28 +40,4 @@ public abstract class AbstractBasketSubscribe { public abstract void processing(C object); - 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; - } - - public Convert getConvert() { - return convert; - } - - public void setConvert(Convert convert) { - this.convert = convert; - } - } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AccountSubscribe.java b/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/AccountSubscribe.java similarity index 90% rename from src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AccountSubscribe.java rename to src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/AccountSubscribe.java index 0743a5e..f16d280 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/AccountSubscribe.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/AccountSubscribe.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.distribution.subscriber; +package org.sadtech.vk.bot.sdk.service.distribution.subscriber; import com.vk.api.sdk.objects.messages.Message; import com.vk.api.sdk.objects.messages.MessageAttachmentType; @@ -24,6 +24,11 @@ public class AccountSubscribe extends AbstractBasketSubscribe && "Payment awaiting acceptance".equals(userMessage.getAttachments().get(0).getLink().getCaption()); } + @Override + public Message convert(Message object) { + return object; + } + @Override public void processing(Message message) { if (message.getText() != null) { @@ -34,7 +39,7 @@ public class AccountSubscribe extends AbstractBasketSubscribe } } catch (PaymentException e) { log.error(e.getMessage()); - sending.send(message.getPeerId().longValue(), BoxAnswer.builder().message(e.getDescription()).build()); + sending.send(message.getPeerId().longValue(), BoxAnswer.builder().message(e.getMessage()).build()); } } } diff --git a/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/BasketSubscribeService.java b/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/BasketSubscribeService.java new file mode 100644 index 0000000..b1fd1ee --- /dev/null +++ b/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/BasketSubscribeService.java @@ -0,0 +1,9 @@ +package org.sadtech.vk.bot.sdk.service.distribution.subscriber; + +public interface BasketSubscribeService { + + void update(S object); + + void processing(C object); + +} diff --git a/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/MailSubscriber.java b/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/MailSubscriber.java new file mode 100644 index 0000000..c6630f0 --- /dev/null +++ b/src/main/java/org/sadtech/vk/bot/sdk/service/distribution/subscriber/MailSubscriber.java @@ -0,0 +1,35 @@ +package org.sadtech.vk.bot.sdk.service.distribution.subscriber; + +import com.google.gson.JsonObject; +import com.vk.api.sdk.objects.messages.Message; +import org.sadtech.social.core.domain.content.Mail; +import org.sadtech.social.core.service.MailService; +import org.springframework.core.convert.ConversionService; + +public class MailSubscriber extends AbstractBasketSubscribe { + + private final MailService mailService; + private final ConversionService conversionService; + + public MailSubscriber(MailService mailService, ConversionService conversionService) { + this.mailService = mailService; + this.conversionService = conversionService; + } + + @Override + public boolean check(JsonObject object) { + String type = object.get("type").getAsString(); + return "message_new".equals(type); + } + + @Override + public Message convert(JsonObject object) { + return conversionService.convert(object, Message.class); + } + + @Override + public void processing(Message message) { + mailService.add(conversionService.convert(message, Mail.class)); + } + +} diff --git a/src/main/java/org/sadtech/vkbot/core/service/impl/RawEventServiceImpl.java b/src/main/java/org/sadtech/vk/bot/sdk/service/impl/RawEventServiceImpl.java similarity index 65% rename from src/main/java/org/sadtech/vkbot/core/service/impl/RawEventServiceImpl.java rename to src/main/java/org/sadtech/vk/bot/sdk/service/impl/RawEventServiceImpl.java index c4b1ca4..8aa73c9 100644 --- a/src/main/java/org/sadtech/vkbot/core/service/impl/RawEventServiceImpl.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/service/impl/RawEventServiceImpl.java @@ -1,10 +1,10 @@ -package org.sadtech.vkbot.core.service.impl; +package org.sadtech.vk.bot.sdk.service.impl; import com.google.gson.JsonObject; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.sadtech.vkbot.core.repository.RawEventRepository; -import org.sadtech.vkbot.core.service.RawEventService; +import org.sadtech.vk.bot.sdk.repository.RawEventRepository; +import org.sadtech.vk.bot.sdk.service.RawEventService; import java.util.Set; @@ -17,13 +17,13 @@ public class RawEventServiceImpl implements RawEventService { @Override public void cleanAll() { rawEventRepository.cleanAll(); - log.info("Репозиторий событий очищен"); + log.trace("Репозиторий событий очищен"); } @Override public void add(JsonObject jsonObject) { rawEventRepository.add(jsonObject); - log.info("Событие отправленно в репозиторий"); + log.trace("Событие отправленно в репозиторий"); } @Override diff --git a/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java b/src/main/java/org/sadtech/vk/bot/sdk/service/listener/EventListenerVk.java similarity index 81% rename from src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java rename to src/main/java/org/sadtech/vk/bot/sdk/service/listener/EventListenerVk.java index 58fa5ee..aec7e25 100644 --- a/src/main/java/org/sadtech/vkbot/core/listener/EventListenerVk.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/service/listener/EventListenerVk.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.listener; +package org.sadtech.vk.bot.sdk.service.listener; import com.vk.api.sdk.client.VkApiClient; import com.vk.api.sdk.client.actors.GroupActor; @@ -8,9 +8,15 @@ import com.vk.api.sdk.exceptions.LongPollServerKeyExpiredException; import com.vk.api.sdk.objects.callback.longpoll.responses.GetLongPollEventsResponse; import com.vk.api.sdk.objects.groups.LongPollServer; import lombok.extern.slf4j.Slf4j; -import org.sadtech.vkbot.config.VkConnect; -import org.sadtech.vkbot.core.service.RawEventService; +import org.sadtech.vk.bot.sdk.config.VkConnect; +import org.sadtech.vk.bot.sdk.service.RawEventService; +/** + * + * Слушает и сохраняет события, которые происодят во ВКонтакте, основываясь на LongPoll API. + * + * @author upagge + */ @Slf4j public class EventListenerVk implements Runnable { @@ -35,7 +41,7 @@ public class EventListenerVk implements Runnable { .waitTime(DEFAULT_WAIT_TIME) .execute(); eventsResponse.getUpdates().parallelStream().forEach(object -> { - log.info("Новое событие от LongPoll\n" + object); + log.trace("Новое событие от LongPoll\n" + object); rawEventService.add(object); }); lastTimeStamp = eventsResponse.getTs(); @@ -52,7 +58,7 @@ public class EventListenerVk implements Runnable { private LongPollServer getLongPollServer() throws ClientException, ApiException { log.debug("LongPoll сервер инициализирован"); if (actor != null) { - return vk.groups().getLongPollServer(actor, actor.getGroupId()).execute(); + return vk.groupsLongPoll().getLongPollServer(actor, actor.getGroupId()).execute(); } else { throw new NullPointerException("Group actor"); } @@ -67,8 +73,4 @@ public class EventListenerVk implements Runnable { } } - public RawEventService getRawEventService() { - return rawEventService; - } - } diff --git a/src/main/java/org/sadtech/vkbot/core/utils/VkApi.java b/src/main/java/org/sadtech/vk/bot/sdk/utils/VkApi.java similarity index 71% rename from src/main/java/org/sadtech/vkbot/core/utils/VkApi.java rename to src/main/java/org/sadtech/vk/bot/sdk/utils/VkApi.java index 6f44596..08bd53d 100644 --- a/src/main/java/org/sadtech/vkbot/core/utils/VkApi.java +++ b/src/main/java/org/sadtech/vk/bot/sdk/utils/VkApi.java @@ -1,4 +1,4 @@ -package org.sadtech.vkbot.core.utils; +package org.sadtech.vk.bot.sdk.utils; import com.google.gson.Gson; import com.google.gson.JsonObject; @@ -11,10 +11,17 @@ 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 lombok.extern.slf4j.Slf4j; -import org.sadtech.vkbot.config.VkConnect; +import org.sadtech.vk.bot.sdk.config.VkConnect; import java.util.List; +import java.util.Optional; +/** + * + * Надстройка над API VK, для упрощеного доступа. + * + * @author upagge + */ @Slf4j public class VkApi { @@ -22,11 +29,11 @@ public class VkApi { private final ServiceActor actor; public VkApi(VkConnect vkConnect) { - vk = vkConnect.getVkApiClient(); - actor = vkConnect.getServiceActor(); + this.vk = vkConnect.getVkApiClient(); + this.actor = vkConnect.getServiceActor(); } - public UserMin getUserMini(Integer id) { + public Optional getUserMini(Integer id) { UserMin userMin = null; Gson gson = new Gson(); try { @@ -37,20 +44,20 @@ public class VkApi { } catch (ApiException | ClientException e) { log.error(e.getMessage()); } - return userMin; + return Optional.ofNullable(userMin); } - public String getUserUniver(Integer id) { + public Optional getUserUniver(Integer id) { List temp = null; try { temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(Fields.UNIVERSITIES).execute(); } catch (ApiException | ClientException e) { log.error(e.getMessage()); } - return temp != null ? temp.get(0).getUniversities().get(0).getName() : null; + return temp != null ? Optional.of(temp.get(0).getUniversities().get(0).getName()) : Optional.empty(); } - public String getUserCity(Integer id) { + public Optional getUserCity(Integer id) { List temp = null; try { temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(Fields.CITY).execute(); @@ -58,9 +65,9 @@ public class VkApi { log.error(e.getMessage()); } if (temp != null && checkCity(temp)) { - return temp.get(0).getCity().getTitle(); + return Optional.of(temp.get(0).getCity().getTitle()); } - return null; + return Optional.empty(); } private boolean checkCity(List temp) { diff --git a/src/main/java/org/sadtech/vk/bot/sdk/utils/VkInsertData.java b/src/main/java/org/sadtech/vk/bot/sdk/utils/VkInsertData.java new file mode 100644 index 0000000..60e9946 --- /dev/null +++ b/src/main/java/org/sadtech/vk/bot/sdk/utils/VkInsertData.java @@ -0,0 +1,39 @@ +package org.sadtech.vk.bot.sdk.utils; + +import com.vk.api.sdk.objects.users.UserMin; +import org.sadtech.vk.bot.sdk.config.VkConnect; + +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class VkInsertData { + + private final VkApi vkApi; + + public VkInsertData(VkConnect vkConnect) { + this.vkApi = new VkApi(vkConnect); + } + + public String insertWords(String message, Long personId) { + Pattern pattern = Pattern.compile("%(\\w+)%"); + Matcher m = pattern.matcher(message); + StringBuffer result = new StringBuffer(); + while (m.find()) { + insert(m.group(0), personId.intValue()).ifPresent(s -> m.appendReplacement(result, s)); + } + m.appendTail(result); + return result.toString(); + } + + private Optional insert(String key, Integer userId) { + switch (key) { + case "%firstname%": + return vkApi.getUserMini(userId).map(UserMin::getFirstName); + case "%lastname%": + return vkApi.getUserMini(userId).map(UserMin::getLastName); + } + return Optional.empty(); + } + +} diff --git a/src/main/java/org/sadtech/vkbot/core/config/VkBotJpaConfig.java b/src/main/java/org/sadtech/vkbot/core/config/VkBotJpaConfig.java deleted file mode 100644 index 0dad2e0..0000000 --- a/src/main/java/org/sadtech/vkbot/core/config/VkBotJpaConfig.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.sadtech.vkbot.core.config; - -import org.springframework.boot.autoconfigure.domain.EntityScan; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; - -/** - * TODO: Добавить описание класса. - * - * @author upagge [28/07/2019] - */ -@EnableJpaRepositories(basePackages = "org.sadtech.vkbot.core.repository.jpa") -@EntityScan(basePackages = "org.sadtech.vkbot.core.domain.jpa") -public class VkBotJpaConfig { - -} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/MailSubscriber.java b/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/MailSubscriber.java deleted file mode 100644 index 62eab67..0000000 --- a/src/main/java/org/sadtech/vkbot/core/distribution/subscriber/MailSubscriber.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.sadtech.vkbot.core.distribution.subscriber; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.vk.api.sdk.objects.messages.Message; -import org.sadtech.social.core.domain.content.Mail; -import org.sadtech.social.core.service.MailService; -import org.sadtech.vkbot.convert.Convert; -import org.sadtech.vkbot.core.convert.MessageMailConvert; - -public class MailSubscriber extends AbstractBasketSubscribe { - - private final MailService mailService; - private final Convert mailConvert = new MessageMailConvert(); - - public MailSubscriber(MailService mailService) { - this.mailService = mailService; - this.convert = (object) -> { - Gson gson = new Gson(); - return gson.fromJson(object.getAsJsonObject("object"), Message.class); - }; - } - - @Override - public boolean check(JsonObject object) { - String type = object.get("type").getAsString(); - return "message_new".equals(type); - } - - @Override - public void processing(Message object) { - mailService.add(mailConvert.converting(object)); - } - -}