Удаление модуля для ВК

Он вынесен в отдельный проект для повторного использования
This commit is contained in:
Mark Struchkov 2019-01-09 18:42:59 +03:00
parent 863229f5b6
commit 1e053c9507
13 changed files with 0 additions and 515 deletions

View File

@ -1,183 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>consultant</artifactId>
<groupId>org.sadtech</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.sadtech</groupId>
<artifactId>vk-bot</artifactId>
<version>0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<vk.api.ver>0.5.13-SNAPSHOT</vk.api.ver>
<spring.core>5.1.3.RELEASE</spring.core>
<spring.context>5.1.3.RELEASE</spring.context>
<spring.web>5.1.3.RELEASE</spring.web>
<spring.webmvc>5.1.3.RELEASE</spring.webmvc>
<spring.context>5.1.3.RELEASE</spring.context>
<spring.context>5.1.3.RELEASE</spring.context>
<spring.data>2.1.3.RELEASE</spring.data>
<javax.servlet>3.1.0</javax.servlet>
<javax.persistance>2.2</javax.persistance>
<hibernate.entitymanager>5.4.0.Final</hibernate.entitymanager>
<hibernate.core>5.2.10.Final</hibernate.core>
<mysql.connector>8.0.13</mysql.connector>
<lombok>1.18.4</lombok>
<log4j>1.2.17</log4j>
<json>20180813</json>
<jetty.util>6.1.25</jetty.util>
</properties>
<dependencies>
<dependency>
<groupId>com.vk.api</groupId>
<artifactId>sdk</artifactId>
<version>${vk.api.ver}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.core}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.context}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.context}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.web}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.webmvc}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty.util}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>${javax.persistance}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.entitymanager}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.core}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>org.sadtech</groupId>
<artifactId>consultant-core</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations-java5</artifactId>
<version>16.0.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,16 +0,0 @@
package org.sadtech.vkbot.core;
public enum SourceMessage {
VK("VK"),
FB("FB");
private final String value;
private SourceMessage(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
}

View File

@ -1,43 +0,0 @@
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.exceptions.ApiException;
import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.objects.users.UserXtrCounters;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Component;
import java.util.List;
@Log4j
@Component
public class VkApi {
private static VkApiClient vk;
private static GroupActor actor;
public VkApi(VkApiClient vk, GroupActor actor) {
this.vk = vk;
this.actor = actor;
}
public static void sendMessage(Integer id, String text) {
try {
vk.messages().send(actor).userId(id).message(text).execute();
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
}
public static UserXtrCounters getUserVk(Integer id) {
List<UserXtrCounters> temp = null;
try {
temp = vk.users().get(actor).userIds(String.valueOf(id)).execute();
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
return temp.get(0);
}
}

View File

@ -1,17 +0,0 @@
package org.sadtech.vkbot.core.data;
import com.google.gson.JsonObject;
import java.util.Queue;
public interface ResponsibleData {
void add(JsonObject jsonObject);
void remove(int id);
void cleanAll();
Queue<JsonObject> getJsonObjects();
}

View File

@ -1,33 +0,0 @@
package org.sadtech.vkbot.core.data.impl;
import com.google.gson.JsonObject;
import org.sadtech.vkbot.core.data.ResponsibleData;
import org.springframework.stereotype.Component;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
@Component
public class ResponseDataVk implements ResponsibleData {
private Queue<JsonObject> jsonObjects = new ConcurrentLinkedQueue<>();
@Override
public void add(JsonObject jsonObject) {
jsonObjects.offer(jsonObject);
}
@Override
public void remove(int id) {
jsonObjects.remove(id);
}
@Override
public void cleanAll() {
jsonObjects.clear();
}
public Queue<JsonObject> getJsonObjects() {
return jsonObjects;
}
}

View File

@ -1,9 +0,0 @@
package org.sadtech.vkbot.core.handlers;
import org.sadtech.vkbot.core.listener.Observer;
public interface Handled {
void sortAndSend() throws Exception;
void registerObserver(Observer o);
}

View File

@ -1,48 +0,0 @@
package org.sadtech.vkbot.core.handlers.impl;
import com.google.gson.JsonObject;
import lombok.extern.log4j.Log4j;
import org.sadtech.vkbot.core.handlers.Handled;
import org.sadtech.vkbot.core.listener.Observable;
import org.sadtech.vkbot.core.data.ResponsibleData;
import org.sadtech.vkbot.core.listener.Observer;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Log4j
@Component("dispatcherHandler")
public class DispatcherHandlerVk implements Observable, Handled {
private ResponsibleData date;
private List<Observer> observers = new ArrayList<>();
private JsonObject event;
public DispatcherHandlerVk(ResponsibleData date) {
this.date = date;
}
@Async
public void sortAndSend() {
while (true) {
if (date.getJsonObjects().peek() != null) {
event = date.getJsonObjects().poll();
notifyObservers();
}
}
}
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
@Override
public void notifyObservers() {
for (Observer observer : observers) {
observer.update(event);
}
}
}

View File

@ -1,65 +0,0 @@
package org.sadtech.vkbot.core.handlers.impl;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.vk.api.sdk.objects.messages.Message;
import com.vk.api.sdk.objects.users.UserXtrCounters;
import lombok.extern.log4j.Log4j;
import org.sadtech.consultant.database.entity.Mail;
import org.sadtech.consultant.database.entity.Person;
import org.sadtech.consultant.processing.MessageLogicService;
import org.sadtech.consultant.processing.PersonLogicService;
import org.sadtech.vkbot.core.SourceMessage;
import org.sadtech.vkbot.core.VkApi;
import org.sadtech.vkbot.core.handlers.Handled;
import org.sadtech.vkbot.core.listener.Observer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Log4j
@Component
public class MessageHandlerVk implements Observer {
private MessageLogicService messageLogicService;
private PersonLogicService personLogicService;
private Message userMessage;
public MessageHandlerVk(@Qualifier("dispatcherHandler") Handled dispatcherHandler, PersonLogicService personLogicService, MessageLogicService messageLogicService) {
this.messageLogicService = messageLogicService;
this.personLogicService = personLogicService;
dispatcherHandler.registerObserver(this);
}
@Override
public void update(JsonObject object) {
if (object.get("type").toString().equals("\"message_new\"")) {
Gson gson = new Gson();
userMessage = gson.fromJson(object.getAsJsonObject("object"), com.vk.api.sdk.objects.messages.Message.class);
sortAndSend();
}
}
public void sortAndSend() {
Person user;
Integer userVkId = userMessage.getUserId();
if (personLogicService.checkPersonBySocialNetworksId(SourceMessage.VK.name(), userVkId)) {
user = personLogicService.getUserBySocialId(SourceMessage.VK.name(), userMessage.getUserId());
} else {
user = new Person();
UserXtrCounters userXtrCounters = VkApi.getUserVk(userVkId);
user.setCity("city");
user.setName(userXtrCounters.getFirstName());
user.setLastName(userXtrCounters.getLastName());
user.getSocialNetworks().put(String.valueOf(SourceMessage.VK), userMessage.getUserId());
personLogicService.addUser(user);
}
Mail message = new Mail();
message.setPerson(user);
message.setText(userMessage.getBody());
message.setDate(Long.valueOf(userMessage.getDate()));
message.setSourceMessage(SourceMessage.VK.name());
log.info(message);
messageLogicService.addMessage(message);
}
}

View File

@ -1,5 +0,0 @@
package org.sadtech.vkbot.core.listener;
public interface EventListenable {
void listen() throws Exception;
}

View File

@ -1,8 +0,0 @@
package org.sadtech.vkbot.core.listener;
public interface Observable {
// void removeObserver(Observer o);
void notifyObservers();
}

View File

@ -1,7 +0,0 @@
package org.sadtech.vkbot.core.listener;
import com.google.gson.JsonObject;
public interface Observer {
void update(JsonObject object);
}

View File

@ -1,61 +0,0 @@
package org.sadtech.vkbot.core.listener.impl;
import com.vk.api.sdk.actions.LongPoll;
import com.vk.api.sdk.callback.longpoll.queries.GetLongPollEventsQuery;
import com.vk.api.sdk.callback.longpoll.responses.GetLongPollEventsResponse;
import com.vk.api.sdk.client.VkApiClient;
import com.vk.api.sdk.client.actors.GroupActor;
import com.vk.api.sdk.exceptions.ApiException;
import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse;
import lombok.extern.log4j.Log4j;
import org.sadtech.vkbot.core.listener.EventListenable;
import org.sadtech.vkbot.core.data.ResponsibleData;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Log4j
@Component
public class EventListenerVk implements EventListenable {
private VkApiClient vk;
private GroupActor actor;
private ResponsibleData responseData;
private GetLongPollEventsQuery longPollEventsQuery;
private LongPoll longPoll;
private GetLongPollServerResponse server;
public EventListenerVk(VkApiClient vk, GroupActor actor, ResponsibleData responseData) {
this.vk = vk;
this.actor = actor;
this.responseData = responseData;
longPoll = new LongPoll(vk);
}
private void initServer() throws ClientException, ApiException {
server = vk.groups().getLongPollServer(actor).execute();
String key = server.getKey();
String serverUrl = server.getServer();
Integer ts = server.getTs();
longPoll = new LongPoll(vk);
longPollEventsQuery = longPoll.getEvents(serverUrl, key, ts).waitTime(20);
}
@Async
public void listen() throws Exception {
initServer();
do {
GetLongPollEventsResponse eventsResponse;
eventsResponse = longPollEventsQuery.execute();
if (eventsResponse.getUpdates().toArray().length != 0) {
responseData.add(eventsResponse.getUpdates().get(0));
log.info(eventsResponse.getUpdates());
// String test = "{\"one_time\":false,\"buttons\":[[{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"1\\\"}\",\"label\":\"Red2\"},\"color\":\"negative\"},{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"2\\\"}\",\"label\":\"Green\"},\"color\":\"positive\"}],[{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"3\\\"}\",\"label\":\"White\"},\"color\":\"default\"},{\"action\":{\"type\":\"text\",\"payload\":\"{\\\"button\\\": \\\"4\\\"}\",\"label\":\"Blue\"},\"color\":\"primary\"}]]}";
}
longPollEventsQuery = longPoll.getEvents(server.getServer(), server.getKey(), eventsResponse.getTs()).waitTime(20);
} while (true);
}
}

View File

@ -1,20 +0,0 @@
package org.sadtech.vkbot.core.sendler;
import org.sadtech.consultant.MessageSender;
import org.sadtech.consultant.processing.MessageHandler;
import org.sadtech.vkbot.core.SourceMessage;
import org.sadtech.vkbot.core.VkApi;
import org.springframework.stereotype.Component;
@Component
public class MessageSendVk implements MessageSender {
public MessageSendVk(MessageHandler messageHandler) {
messageHandler.addMessageSendler(SourceMessage.VK.name(), this);
}
@Override
public void send(Integer idNetSoc, String text) {
VkApi.sendMessage(idNetSoc, text);
}
}