Без понятия

This commit is contained in:
Mark Struchkov 2019-04-29 12:13:19 +03:00
parent 57ac698715
commit 028ab802af
48 changed files with 90 additions and 1203 deletions

15
pom.xml
View File

@ -4,17 +4,19 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.sadtech.vkbot</groupId> <groupId>org.sadtech.bot</groupId>
<artifactId>core</artifactId> <artifactId>bot-core</artifactId>
<version>0.4.3-SNAPSHOT</version> <version>0.4.3-SNAPSHOT</version>
<packaging>jar</packaging>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>7</source> <source>8</source>
<target>7</target> <target>8</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
@ -40,11 +42,6 @@
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>${gson.ver}</version> <version>${gson.ver}</version>
</dependency> </dependency>
<dependency>
<groupId>com.vk.api</groupId>
<artifactId>sdk</artifactId>
<version>${vksdk.ver}</version>
</dependency>
<dependency> <dependency>
<groupId>log4j</groupId> <groupId>log4j</groupId>
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>

View File

@ -1,4 +1,4 @@
package org.sadtech.vkbot.core.entity; package org.sadtech.bot.core.domain;
import java.util.Objects; import java.util.Objects;

View File

@ -1,4 +1,4 @@
package org.sadtech.vkbot.core.entity; package org.sadtech.bot.core.domain;
//@TODO: Дописать класс //@TODO: Дописать класс
public class Comment { public class Comment {

View File

@ -1,4 +1,4 @@
package org.sadtech.vkbot.core.entity; package org.sadtech.bot.core.domain;
import java.util.Objects; import java.util.Objects;
@ -6,7 +6,6 @@ public class Mail {
private Integer id; private Integer id;
private Integer date; private Integer date;
private Person person;
private Integer peerId; private Integer peerId;
private String body; private String body;
@ -14,13 +13,6 @@ public class Mail {
} }
public Mail(Integer id, Integer date, Person userId, String body) {
this.id = id;
this.date = date;
this.person = userId;
this.body = body;
}
public Integer getId() { public Integer getId() {
return id; return id;
} }
@ -37,13 +29,6 @@ public class Mail {
this.date = date; this.date = date;
} }
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String getBody() { public String getBody() {
return body; return body;
@ -68,12 +53,21 @@ public class Mail {
Mail mail = (Mail) o; Mail mail = (Mail) o;
return Objects.equals(id, mail.id) && return Objects.equals(id, mail.id) &&
Objects.equals(date, mail.date) && Objects.equals(date, mail.date) &&
Objects.equals(person, mail.person) &&
Objects.equals(body, mail.body); Objects.equals(body, mail.body);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, date, person, body); return Objects.hash(id, date, body);
}
@Override
public String toString() {
return "Mail{" +
"id=" + id +
", date=" + date +
", peerId=" + peerId +
", body='" + body + '\'' +
'}';
} }
} }

View File

@ -1,4 +1,4 @@
package org.sadtech.vkbot.core.entity; package org.sadtech.bot.core.domain;
public class MailSend { public class MailSend {

View File

@ -1,4 +1,4 @@
package org.sadtech.vkbot.core.entity; package org.sadtech.bot.core.domain;
import java.util.Objects; import java.util.Objects;

View File

@ -1,4 +1,4 @@
package org.sadtech.vkbot.core.insert; package org.sadtech.bot.core.insert;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;

View File

@ -0,0 +1,9 @@
package org.sadtech.bot.core.repository;
import org.sadtech.bot.core.domain.Comment;
public interface CommentRepository {
void add(Comment comment);
}

View File

@ -1,4 +1,4 @@
package org.sadtech.vkbot.core.repository; package org.sadtech.bot.core.repository;
import java.util.Queue; import java.util.Queue;

View File

@ -1,6 +1,6 @@
package org.sadtech.vkbot.core.repository; package org.sadtech.bot.core.repository;
import org.sadtech.vkbot.core.entity.Mail; import org.sadtech.bot.core.domain.Mail;
import java.util.List; import java.util.List;

View File

@ -1,6 +1,6 @@
package org.sadtech.vkbot.core.repository; package org.sadtech.bot.core.repository;
import org.sadtech.vkbot.core.entity.Person; import org.sadtech.bot.core.domain.Person;
public interface PersonRepository { public interface PersonRepository {

View File

@ -1,7 +1,7 @@
package org.sadtech.vkbot.core.repository.impl; package org.sadtech.bot.core.repository.impl;
import org.sadtech.vkbot.core.entity.Comment; import org.sadtech.bot.core.domain.Comment;
import org.sadtech.vkbot.core.repository.CommentRepository; import org.sadtech.bot.core.repository.CommentRepository;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -1,7 +1,7 @@
package org.sadtech.vkbot.core.repository.impl; package org.sadtech.bot.core.repository.impl;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.sadtech.vkbot.core.repository.EventRepository; import org.sadtech.bot.core.repository.EventRepository;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;

View File

@ -1,8 +1,8 @@
package org.sadtech.vkbot.core.repository.impl; package org.sadtech.bot.core.repository.impl;
import org.sadtech.vkbot.core.entity.Mail; import org.sadtech.bot.core.domain.Mail;
import org.sadtech.vkbot.core.repository.EventRepository; import org.sadtech.bot.core.repository.EventRepository;
import org.sadtech.vkbot.core.repository.MailRepository; import org.sadtech.bot.core.repository.MailRepository;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -1,8 +1,8 @@
package org.sadtech.vkbot.core.repository.impl; package org.sadtech.bot.core.repository.impl;
import org.sadtech.vkbot.core.entity.Person; import org.sadtech.bot.core.domain.Person;
import org.sadtech.vkbot.core.repository.EventRepository; import org.sadtech.bot.core.repository.EventRepository;
import org.sadtech.vkbot.core.repository.PersonRepository; import org.sadtech.bot.core.repository.PersonRepository;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@ -1,7 +1,7 @@
package org.sadtech.vkbot.core.repository.impl; package org.sadtech.bot.core.repository.impl;
import org.sadtech.vkbot.core.entity.Mail; import org.sadtech.bot.core.domain.Mail;
import org.sadtech.vkbot.core.repository.EventRepository; import org.sadtech.bot.core.repository.EventRepository;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;

View File

@ -1,6 +1,6 @@
package org.sadtech.vkbot.core.sender; package org.sadtech.bot.core.sender;
import org.sadtech.vkbot.core.entity.BoxAnswer; import org.sadtech.bot.core.domain.BoxAnswer;
public interface Sent { public interface Sent {

View File

@ -0,0 +1,15 @@
package org.sadtech.bot.core.service;
import java.util.List;
public interface EventService<T> {
void add(T event);
List<T> getEvent(Integer timeFrom, Integer timeTo);
List<T> getFirstEventByTime(Integer timeFrom, Integer timeTo);
List<T> getLastEventByTime(Integer timeFrom, Integer timeTo);
}

View File

@ -0,0 +1,15 @@
package org.sadtech.bot.core.service;
import org.sadtech.bot.core.domain.Person;
public interface PersonService {
void add(Person person);
Person get(Integer id);
boolean checkPerson(Integer idPerson);
Person createPerson(Integer userId);
}

View File

@ -1,7 +1,7 @@
package org.sadtech.vkbot.core.service; package org.sadtech.bot.core.service;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.sadtech.vkbot.core.repository.EventRepository; import org.sadtech.bot.core.repository.EventRepository;
import java.util.Queue; import java.util.Queue;

View File

@ -1,9 +1,9 @@
package org.sadtech.vkbot.core.service.impl; package org.sadtech.bot.core.service.impl;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.repository.EventRepository; import org.sadtech.bot.core.repository.EventRepository;
import org.sadtech.vkbot.core.service.RawEventService; import org.sadtech.bot.core.service.RawEventService;
import java.util.Queue; import java.util.Queue;

View File

@ -1,71 +0,0 @@
package org.sadtech.vkbot.core;
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.exceptions.ApiException;
import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.objects.users.UserMin;
import com.vk.api.sdk.objects.users.UserXtrCounters;
import com.vk.api.sdk.queries.users.UserField;
import org.apache.log4j.Logger;
import java.util.List;
public class VkApi {
public static final Logger log = Logger.getLogger(String.valueOf(VkApi.class));
private VkApiClient vk;
private GroupActor actor;
public VkApi(VkConnect vkConnect) {
vk = vkConnect.getVkApiClient();
actor = vkConnect.getGroupActor();
}
public UserMin getUserMini(Integer id) {
UserMin userMin = null;
Gson gson = new Gson();
try {
List<UserXtrCounters> temp = vk.users().get(actor).userIds(String.valueOf(id)).execute();
JsonParser parser = new JsonParser();
JsonObject object = parser.parse(temp.get(0).toString().substring(15)).getAsJsonObject();
object.add("last_name", object.get("lastName"));
object.add("first_name", object.get("firstName"));
userMin = gson.fromJson(object, UserMin.class);
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
return userMin;
}
public String getUserUniver(Integer id) {
List<UserXtrCounters> temp = null;
try {
temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(UserField.UNIVERSITIES).execute();
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
return temp != null ? temp.get(0).getUniversities().get(0).getName() : null;
}
public String getUserCity(Integer id) {
List<UserXtrCounters> temp = null;
try {
temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(UserField.CITY).execute();
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
if (temp != null) {
if (temp.get(0).getCity() != null) {
log.info("Запрошен город пользователя: " + temp.get(0).getCity().getTitle());
return temp.get(0).getCity().getTitle();
}
}
return null;
}
}

View File

@ -1,83 +0,0 @@
package org.sadtech.vkbot.core;
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;
public VkConfig() {
}
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 == null || getClass() != o.getClass()) 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);
}
@Override
public int hashCode() {
return Objects.hash(groupToken, groupId, serviceToken, appId, groupSecretKey, groupPublicKey);
}
}

View File

@ -1,58 +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.client.actors.ServiceActor;
import com.vk.api.sdk.httpclient.HttpTransportClient;
public class VkConnect {
private VkConfig vkConfig;
private VkApiClient vkApiClient;
private GroupActor groupActor;
private ServiceActor serviceActor;
public VkConnect(VkConfig vkConfig) {
this.vkConfig = vkConfig;
}
public VkApiClient getVkApiClient() {
if (vkApiClient != null) {
return vkApiClient;
} else {
initVkApiClient();
return vkApiClient;
}
}
public GroupActor getGroupActor() {
if (groupActor != null) {
return groupActor;
} else {
initGroupActor();
return groupActor;
}
}
public ServiceActor getServiceActor() {
if (serviceActor != null) {
return serviceActor;
} else {
initServiceActor();
return serviceActor;
}
}
private void initVkApiClient() {
vkApiClient = new VkApiClient(HttpTransportClient.getInstance());
}
private void initGroupActor() {
groupActor = new GroupActor(vkConfig.getGroupId(), vkConfig.getGroupToken());
}
private void initServiceActor() {
serviceActor = new ServiceActor(vkConfig.getAppId(), vkConfig.getServiceToken());
}
}

View File

@ -1,9 +0,0 @@
package org.sadtech.vkbot.core.distribution;
public interface EventDistributable {
void distribute() throws Exception;
void registerSubscriber(String key, EventSubscribe o);
}

View File

@ -1,7 +0,0 @@
package org.sadtech.vkbot.core.distribution;
public interface EventSubscribe<T> {
void update(T object);
}

View File

@ -1,29 +0,0 @@
package org.sadtech.vkbot.core.distribution.impl;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.vk.api.sdk.objects.wall.WallComment;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.distribution.EventDistributable;
import org.sadtech.vkbot.core.distribution.EventSubscribe;
import org.sadtech.vkbot.core.service.distribution.CommentService;
public class CommentSubscriber implements EventSubscribe<JsonObject> {
public static final Logger log = Logger.getLogger(CommentSubscriber.class);
private CommentService commentService;
public CommentSubscriber(EventDistributable distributable, CommentService commentService) {
distributable.registerSubscriber("wall_reply_new", this);
this.commentService = commentService;
}
@Override
public void update(JsonObject object) {
Gson gson = new Gson();
WallComment wallComment = gson.fromJson(object, WallComment.class);
log.info(wallComment);
}
}

View File

@ -1,46 +0,0 @@
package org.sadtech.vkbot.core.distribution.impl;
import com.google.gson.JsonObject;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.distribution.EventDistributable;
import org.sadtech.vkbot.core.distribution.EventSubscribe;
import org.sadtech.vkbot.core.service.RawEventService;
import java.util.HashMap;
import java.util.Map;
public class EventDistributorVK implements EventDistributable, Runnable {
public static final Logger log = Logger.getLogger(EventDistributorVK.class);
private RawEventService rawEventService;
private Map<String, EventSubscribe> eventDistributionMap = new HashMap<>();
public EventDistributorVK(RawEventService rawEventService) {
this.rawEventService = rawEventService;
}
@Override
public void distribute() {
while (true) {
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"));
}
}
}
}
@Override
public void registerSubscriber(String key, EventSubscribe o) {
eventDistributionMap.put(key, o);
}
@Override
public void run() {
distribute();
}
}

View File

@ -1,30 +0,0 @@
package org.sadtech.vkbot.core.distribution.impl;
import com.vk.api.sdk.objects.messages.Message;
import org.sadtech.vkbot.core.distribution.EventDistributable;
import org.sadtech.vkbot.core.distribution.EventSubscribe;
import org.sadtech.vkbot.core.service.distribution.MailService;
public class MailChatSubscriber implements EventSubscribe<Message> {
private MailService mailService;
public MailChatSubscriber(EventDistributable eventDistributable, MailService mailService) {
this.mailService = mailService;
eventDistributable.registerSubscriber("chat", this);
}
public MailChatSubscriber(EventDistributable eventDistributable) {
eventDistributable.registerSubscriber("chat", this);
}
public void setMailService(MailService mailService) {
this.mailService = mailService;
}
@Override
public void update(Message message) {
mailService.add(message);
}
}

View File

@ -1,76 +0,0 @@
package org.sadtech.vkbot.core.distribution.impl;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.vk.api.sdk.objects.messages.Message;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.distribution.EventDistributable;
import org.sadtech.vkbot.core.distribution.EventSubscribe;
import org.sadtech.vkbot.core.service.distribution.MailService;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class MailSubscriber implements EventSubscribe<JsonObject>, EventDistributable {
public static final Logger log = Logger.getLogger(MailSubscriber.class);
private MailService mailService;
private Set<Integer> admins = new HashSet<>();
private Map<String, EventSubscribe<Message>> eventDistributionMap = new HashMap<>();
public MailSubscriber(EventDistributable eventDistributable, MailService mailService) {
this.mailService = mailService;
eventDistributable.registerSubscriber("message_new", this);
}
public MailService getMailService() {
return mailService;
}
public void setMailService(MailService mailService) {
this.mailService = mailService;
}
public void setAdmins(Set<Integer> admins) {
this.admins = admins;
}
public Set<Integer> getAdmins() {
return admins;
}
@Override
public void update(JsonObject object) {
log.info("Дистрибьютор получил событие - сообщение");
Gson gson = new Gson();
Message userMessage = gson.fromJson(object, Message.class);
if (userMessage.getPeerId() > 2000000000) {
if (eventDistributionMap.containsKey("chat")) {
eventDistributionMap.get("chat").update(userMessage);
}
} else {
if (admins.contains(userMessage.getUserId()) && eventDistributionMap.containsKey("terminal")) {
log.info("Сообщение отправлено в репозиторий команд");
eventDistributionMap.get("terminal").update(userMessage);
} else {
log.info("Сообщение отправленно на добавление в репозиторий");
mailService.add(userMessage);
}
}
}
@Override
public void distribute() throws Exception {
}
@Override
public void registerSubscriber(String key, EventSubscribe o) {
eventDistributionMap.put(key, o);
}
}

View File

@ -1,33 +0,0 @@
package org.sadtech.vkbot.core.distribution.impl;
import com.vk.api.sdk.objects.messages.Message;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.distribution.EventSubscribe;
import org.sadtech.vkbot.core.service.distribution.MailService;
public class TerminalSubscriber implements EventSubscribe<Message> {
public static final Logger log = Logger.getLogger(TerminalSubscriber.class);
private MailService mailService;
public TerminalSubscriber(MailSubscriber mailSubscriber, MailService mailService) {
this.mailService = mailService;
mailSubscriber.registerSubscriber("terminal", this);
}
public MailService getMailService() {
return mailService;
}
public void setMailService(MailService mailService) {
this.mailService = mailService;
}
@Override
public void update(Message object) {
log.info("Сообщение отправленно на добавление в репозиторий команд");
mailService.add(object);
}
}

View File

@ -1,41 +0,0 @@
package org.sadtech.vkbot.core.insert;
import org.sadtech.vkbot.core.VkApi;
import org.sadtech.vkbot.core.VkConnect;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VkInsertData {
private VkApi vkApi;
public VkInsertData(VkConnect vkConnect) {
this.vkApi = new VkApi(vkConnect);
}
public String insertWords(String message, Integer idUser) {
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.appendTail(result);
return result.toString();
}
private String insert(String key, Integer userId) {
String string = "";
switch (key) {
case "%firstname%":
string = vkApi.getUserMini(userId).getFirstName();
break;
case "%lastname%":
string = vkApi.getUserMini(userId).getLastName();
break;
}
return string;
}
}

View File

@ -1,74 +0,0 @@
package org.sadtech.vkbot.core.keyboard;
import com.google.gson.JsonObject;
public class ButtonKeyBoard {
private String type = "text";
private String payload;
private String label;
private ColorButton color = ColorButton.PRIMARY;
public ButtonKeyBoard() {
}
public ButtonKeyBoard(String type, String payload, String label, ColorButton color) {
this.type = type;
this.payload = payload;
this.label = label;
this.color = color;
}
public JsonObject getButton() {
JsonObject newButton = new JsonObject();
newButton.addProperty("color", color.toString().toLowerCase());
newButton.add("action", generateAction());
return newButton;
}
private JsonObject generateAction() {
JsonObject action = new JsonObject();
action.addProperty("type", type);
action.addProperty("payload", payload);
action.addProperty("label", label);
return action;
}
public static Builder builder() {
return new ButtonKeyBoard().new Builder();
}
public class Builder {
private Builder() {
}
public Builder setColor(ColorButton color) {
ButtonKeyBoard.this.color = color;
return this;
}
public Builder setLabel(String label) {
ButtonKeyBoard.this.label = label;
return this;
}
public Builder setPayload(String payload) {
ButtonKeyBoard.this.payload = payload;
return this;
}
public Builder setType(String type) {
ButtonKeyBoard.this.type = type;
return this;
}
public ButtonKeyBoard build() {
return ButtonKeyBoard.this;
}
}
}

View File

@ -1,7 +0,0 @@
package org.sadtech.vkbot.core.keyboard;
public enum ColorButton {
PRIMARY, DEFAULT, NEGATIVE, POSITIVE
}

View File

@ -1,69 +0,0 @@
package org.sadtech.vkbot.core.keyboard;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.List;
public class KeyBoard {
private List<LineKeyBoard> lineKeyBoards = new ArrayList<>();
private boolean oneTime;
public KeyBoard() {
}
public KeyBoard(List<LineKeyBoard> lineKeyBoards, boolean oneTime) {
this.lineKeyBoards = lineKeyBoards;
this.oneTime = oneTime;
}
public void setOneTime(boolean oneTime) {
this.oneTime = oneTime;
}
public JsonObject getKeyboard() {
JsonObject keyboard = new JsonObject();
keyboard.addProperty("one_time", oneTime);
JsonArray menuLine = new JsonArray();
for (LineKeyBoard lineKeyboard : lineKeyBoards) {
menuLine.add(lineKeyboard.getLine());
}
keyboard.add("buttons", menuLine);
return keyboard;
}
public void addLine(LineKeyBoard lineKeyBoard) {
lineKeyBoards.add(lineKeyBoard);
}
public static Builder builder() {
return new KeyBoard().new Builder();
}
public class Builder {
private Builder() {
}
public Builder setLineKeyBoard(LineKeyBoard lineKeyBoard) {
KeyBoard.this.lineKeyBoards.add(lineKeyBoard);
return this;
}
public Builder setOneTime(boolean oneTime) {
KeyBoard.this.oneTime = oneTime;
return this;
}
public KeyBoard build() {
return KeyBoard.this;
}
}
}

View File

@ -1,34 +0,0 @@
package org.sadtech.vkbot.core.keyboard;
import java.util.List;
public class KeyBoardService {
public static KeyBoard keyBoardYesNo() {
ButtonKeyBoard yesButton = ButtonKeyBoard.builder().setColor(ColorButton.POSITIVE).setLabel("Да").setPayload("{\"button\": \"yes\"}").build();
ButtonKeyBoard noButton = ButtonKeyBoard.builder().setColor(ColorButton.NEGATIVE).setLabel("Нет").setPayload("{\"button\": \"no\"}").build();
LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build();
KeyBoard keyBoard = KeyBoard.builder().setLineKeyBoard(lineKeyBoard).build();
keyBoard.setOneTime(true);
return keyBoard;
}
public static KeyBoard verticalMenuString(List<String> labelButtons) {
KeyBoard keyBoard = new KeyBoard();
for (String labelButton : labelButtons) {
ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().setLabel(labelButton).setType("text").setPayload("{\"button\": \"" + labelButton + "\"}").build();
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
}
keyBoard.setOneTime(true);
return keyBoard;
}
public static KeyBoard verticalMenuButton(List<ButtonKeyBoard> buttonKeyBoards) {
KeyBoard keyBoard = new KeyBoard();
for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) {
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
}
keyBoard.setOneTime(true);
return keyBoard;
}
}

View File

@ -1,47 +0,0 @@
package org.sadtech.vkbot.core.keyboard;
import com.google.gson.JsonArray;
import java.util.ArrayList;
import java.util.List;
public class LineKeyBoard {
private List<ButtonKeyBoard> buttonKeyBoards = new ArrayList<>();
public LineKeyBoard() {
}
public LineKeyBoard(List<ButtonKeyBoard> buttonKeyBoards) {
this.buttonKeyBoards = buttonKeyBoards;
}
public JsonArray getLine() {
JsonArray line = new JsonArray();
for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) {
line.add(buttonKeyBoard.getButton());
}
return line;
}
public static Builder builder() {
return new LineKeyBoard().new Builder();
}
public class Builder {
private Builder() {
}
public Builder setButtonKeyBoard(ButtonKeyBoard buttonKeyBoard) {
LineKeyBoard.this.buttonKeyBoards.add(buttonKeyBoard);
return this;
}
public LineKeyBoard build() {
return LineKeyBoard.this;
}
}
}

View File

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

View File

@ -1,80 +0,0 @@
package org.sadtech.vkbot.core.listener;
import com.google.gson.JsonObject;
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.exceptions.LongPollServerKeyExpiredException;
import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.repository.impl.EventRepositoryQueue;
import org.sadtech.vkbot.core.service.RawEventService;
import org.sadtech.vkbot.core.service.impl.RawEventServiceImpl;
public class EventListenerVk implements EventListener, Runnable {
public static final Logger log = Logger.getLogger(EventListenerVk.class);
private VkApiClient vk;
private GroupActor actor;
private static Integer DEFAULT_WAIT_TIME = 10;
private RawEventService rawEventService;
public EventListenerVk(VkConnect vkConnect) {
vk = vkConnect.getVkApiClient();
actor = vkConnect.getGroupActor();
rawEventService = new RawEventServiceImpl(new EventRepositoryQueue());
}
public EventListenerVk(VkConnect vkConnect, RawEventService rawEventService) {
this.vk = vkConnect.getVkApiClient();
this.actor = vkConnect.getGroupActor();
this.rawEventService = rawEventService;
}
public RawEventService getRawEventService() {
return rawEventService;
}
public void listen() throws ClientException, ApiException {
GetLongPollServerResponse longPollServer = getLongPollServer();
int lastTimeStamp = longPollServer.getTs();
while (true) {
try {
GetLongPollEventsResponse eventsResponse = vk.longPoll().getEvents(longPollServer.getServer(), longPollServer.getKey(), lastTimeStamp).waitTime(DEFAULT_WAIT_TIME).execute();
for (JsonObject jsonObject: eventsResponse.getUpdates()) {
log.info("Новое событие от LongPoll\n" + jsonObject);
rawEventService.add(jsonObject);
}
lastTimeStamp = eventsResponse.getTs();
} catch (LongPollServerKeyExpiredException e) {
longPollServer = getLongPollServer();
lastTimeStamp = longPollServer.getTs();
}
}
}
private GetLongPollServerResponse getLongPollServer() throws ClientException, ApiException {
log.info("LongPoll сервер инициализирован");
if (actor != null) {
return vk.groups().getLongPollServer(actor).execute();
}
return null;
}
@Override
public void run() {
try {
listen();
} catch (ClientException | ApiException e) {
e.printStackTrace();
}
}
}

View File

@ -1,9 +0,0 @@
package org.sadtech.vkbot.core.repository;
import org.sadtech.vkbot.core.entity.Comment;
public interface CommentRepository {
void add(Comment comment);
}

View File

@ -1,66 +0,0 @@
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.exceptions.ApiException;
import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.queries.messages.MessagesSendQuery;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.entity.BoxAnswer;
import org.sadtech.vkbot.core.insert.VkInsertData;
public class MailSenderVk implements Sent {
public static final Logger log = Logger.getLogger(MailSenderVk.class);
private VkApiClient vkApiClient;
private GroupActor groupActor;
private VkInsertData vkInsertData;
public MailSenderVk(VkConnect vkConnect) {
this.vkApiClient = vkConnect.getVkApiClient();
this.groupActor = vkConnect.getGroupActor();
this.vkInsertData = new VkInsertData(vkConnect);
}
@Override
public void send(Integer idPerson, String message) {
sendMessage(vkApiClient.messages().send(groupActor).peerId(idPerson).message(message));
}
@Override
public void send(Integer idPerson, BoxAnswer boxAnswer) {
MessagesSendQuery messagesSendQuery = createMessage(boxAnswer, idPerson);
sendMessage(messagesSendQuery);
}
private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Integer peerId) {
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId).message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId));
if (boxAnswer.getKeyboard() != null) {
messages.keyboard(boxAnswer.getKeyboard());
} else {
messages.keyboard("{\"buttons\":[],\"one_time\":true}");
}
if (boxAnswer.getLat() != null && boxAnswer.getaLong() != null) {
messages.lat(boxAnswer.getLat()).lng(boxAnswer.getaLong());
}
if (boxAnswer.getStickerId() != null) {
try {
vkApiClient.messages().send(groupActor).peerId(peerId).stickerId(boxAnswer.getStickerId()).execute();
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
}
return messages;
}
private void sendMessage(MessagesSendQuery messages) {
try {
messages.execute();
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
}
}

View File

@ -1,21 +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.sadtech.vkbot.core.entity.Person;
public interface PersonService {
void add(User person);
void add(Person person);
void add(UserMin userMin);
Person get(Integer id);
boolean checkPerson(Integer idPerson);
Person createPerson(Integer userId);
}

View File

@ -1,10 +0,0 @@
package org.sadtech.vkbot.core.service.distribution;
import org.sadtech.vkbot.core.entity.Comment;
import org.sadtech.vkbot.core.service.distribution.impl.EventService;
//@TODO: Дописать класс
public interface CommentService extends EventService<Comment> {
}

View File

@ -1,11 +0,0 @@
package org.sadtech.vkbot.core.service.distribution;
import com.vk.api.sdk.objects.messages.Message;
import org.sadtech.vkbot.core.entity.Mail;
import org.sadtech.vkbot.core.service.distribution.impl.EventService;
public interface MailService extends EventService<Mail> {
void add(Message message);
}

View File

@ -1,37 +0,0 @@
package org.sadtech.vkbot.core.service.distribution.impl;
import org.sadtech.vkbot.core.entity.Comment;
import org.sadtech.vkbot.core.repository.CommentRepository;
import org.sadtech.vkbot.core.service.distribution.CommentService;
import java.util.List;
public class CommentServiceImpl implements CommentService {
private CommentRepository commentRepository;
public CommentServiceImpl(CommentRepository commentRepository) {
this.commentRepository = commentRepository;
}
@Override
public void add(Comment comment) {
commentRepository.add(comment);
}
@Override
public List<Comment> getEvent(Integer timeFrom, Integer timeTo) {
return null;
}
@Override
public List<Comment> getFirstMailByTime(Integer timeFrom, Integer timeTo) {
return null;
}
@Override
public List<Comment> getLastMailByTime(Integer timeFrom, Integer timeTo) {
return null;
}
}

View File

@ -1,15 +0,0 @@
package org.sadtech.vkbot.core.service.distribution.impl;
import java.util.List;
public interface EventService<T> {
void add(T event);
List<T> getEvent(Integer timeFrom, Integer timeTo);
List<T> getFirstMailByTime(Integer timeFrom, Integer timeTo);
List<T> getLastMailByTime(Integer timeFrom, Integer timeTo);
}

View File

@ -1,97 +0,0 @@
package org.sadtech.vkbot.core.service.distribution.impl;
import com.vk.api.sdk.objects.messages.Message;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.entity.Mail;
import org.sadtech.vkbot.core.entity.Person;
import org.sadtech.vkbot.core.repository.MailRepository;
import org.sadtech.vkbot.core.repository.impl.MailRepositoryList;
import org.sadtech.vkbot.core.service.PersonService;
import org.sadtech.vkbot.core.service.distribution.MailService;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class MailServiceImpl implements MailService {
public static final Logger log = Logger.getLogger(MailServiceImpl.class);
private MailRepository mailRepository;
private PersonService personService;
public MailServiceImpl(PersonService personService) {
this.mailRepository = new MailRepositoryList();
this.personService = personService;
}
public MailServiceImpl(MailRepository mailRepository, PersonService personService) {
this.mailRepository = mailRepository;
this.personService = personService;
}
@Override
public void add(Mail mail) {
mailRepository.add(mail);
log.info("Сообщение добавлено в репозиторий");
}
@Override
public void add(Message message) {
Mail mail = new Mail();
mail.setDate(message.getDate());
if (!message.getFromId().equals(message.getPeerId())) {
mail.setBody(message.getBody().replaceFirst("\\[club([0-9]+)\\|\\@\\w+][ ,.]", ""));
} else {
mail.setBody(message.getBody());
}
mail.setId(message.getId());
mail.setPeerId(message.getPeerId());
if (!personService.checkPerson(message.getFromId())) {
personService.add(personService.createPerson(message.getFromId()));
}
mail.setPerson(personService.get(message.getFromId()));
log.info("Сообщение добавлено в репозиторий");
mailRepository.add(mail);
}
private void clean() {
}
@Override
public List<Mail> getFirstMailByTime(Integer timeFrom, Integer timeTo) {
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
Set<Person> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>();
for (int i = mails.size() - 1; i >= 0; i--) {
if (!people.contains(mails.get(i).getPerson())) {
returnMails.add(mails.get(i));
people.add(mails.get(i).getPerson());
}
}
return returnMails;
}
@Override
public List<Mail> getLastMailByTime(Integer timeFrom, Integer timeTo) {
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
Set<Person> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>();
for (Mail mail : mails) {
if (!people.contains(mail.getPerson())) {
returnMails.add(mail);
people.add(mail.getPerson());
}
}
return returnMails;
}
@Override
public List<Mail> getEvent(Integer timeFrom, Integer timeTo) {
log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo);
return mailRepository.getMailByTime(timeFrom, timeTo);
}
}

View File

@ -1,76 +0,0 @@
package org.sadtech.vkbot.core.service.impl;
import com.vk.api.sdk.objects.users.User;
import com.vk.api.sdk.objects.users.UserMin;
import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.VkApi;
import org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.entity.Person;
import org.sadtech.vkbot.core.repository.PersonRepository;
import org.sadtech.vkbot.core.repository.impl.PersonRepositoryMap;
import org.sadtech.vkbot.core.service.PersonService;
public class PersonServiceImpl implements PersonService {
public static final Logger log = Logger.getLogger(PersonServiceImpl.class);
private PersonRepository personRepository;
private 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);
}
@Override
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("Пользователь добавлен в репозиторий");
}
@Override
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;
}
}