Merge branch 'release/release-0.2.0'

This commit is contained in:
Mark Struchkov 2019-05-18 13:19:06 +03:00
commit 1dfa323507
14 changed files with 139 additions and 328 deletions

View File

@ -6,7 +6,7 @@
<groupId>org.sadtech.vkbot</groupId>
<artifactId>vkbot-core</artifactId>
<version>0.1.0-RELEASE</version>
<version>0.2.0-RELEASE</version>
<packaging>jar</packaging>
<build>
@ -23,6 +23,7 @@
</build>
<properties>
<bot.core.ver>0.6.0-RELEASE</bot.core.ver>
<vksdk.ver>0.5.13-SNAPSHOT</vksdk.ver>
<log4j.ver>1.2.17</log4j.ver>
</properties>
@ -43,8 +44,9 @@
<dependency>
<groupId>org.sadtech.bot</groupId>
<artifactId>bot-core</artifactId>
<version>0.4.3-SNAPSHOT</version>
<version>${bot.core.ver}</version>
</dependency>
</dependencies>
<developers>

View File

@ -7,9 +7,9 @@ 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.Fields;
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;
@ -32,9 +32,7 @@ public class VkApi {
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"));
JsonObject object = parser.parse(temp.get(0).toString()).getAsJsonObject();
userMin = gson.fromJson(object, UserMin.class);
} catch (ApiException | ClientException e) {
log.error(e);
@ -45,7 +43,7 @@ public class VkApi {
public String getUserUniver(Integer id) {
List<UserXtrCounters> temp = null;
try {
temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(UserField.UNIVERSITIES).execute();
temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(Fields.UNIVERSITIES).execute();
} catch (ApiException | ClientException e) {
log.error(e);
}
@ -55,7 +53,7 @@ public class VkApi {
public String getUserCity(Integer id) {
List<UserXtrCounters> temp = null;
try {
temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(UserField.CITY).execute();
temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(Fields.CITY).execute();
} catch (ApiException | ClientException e) {
log.error(e);
}

View File

@ -0,0 +1,70 @@
package org.sadtech.vkbot.core.convert;
import com.vk.api.sdk.objects.messages.*;
import org.sadtech.bot.core.domain.keyboard.ButtonColor;
import org.sadtech.bot.core.domain.keyboard.KeyBoard;
import org.sadtech.bot.core.domain.keyboard.KeyBoardButton;
import org.sadtech.bot.core.domain.keyboard.KeyBoardLine;
import java.util.ArrayList;
import java.util.List;
public final class KeyBoardConvert {
private KeyBoardConvert() {
}
public static Keyboard convertKeyboard(KeyBoard keyboard) {
Keyboard keyboardVk = new Keyboard();
keyboardVk.setOneTime(keyboard.isOneTime());
List<List<KeyboardButton>> listButton = new ArrayList<>();
for (KeyBoardLine keyBoardLine : keyboard.getKeyBoardLines()) {
List<KeyboardButton> buttonList = new ArrayList<>();
for (KeyBoardButton button : keyBoardLine.getKeyBoardButtons()) {
buttonList.add(convertButton(button));
}
listButton.add(buttonList);
}
keyboardVk.setButtons(listButton);
return keyboardVk;
}
private static KeyboardButton convertButton(KeyBoardButton button) {
KeyboardButton buttonVk = new KeyboardButton();
buttonVk.setColor(convertColor(button.getColor()));
KeyboardButtonAction buttonActionVk = new KeyboardButtonAction();
buttonActionVk.setLabel(button.getLabel());
buttonActionVk.setType(KeyboardButtonActionType.TEXT);
buttonActionVk.setPayload(button.getPayload());
buttonVk.setAction(buttonActionVk);
return buttonVk;
}
private static KeyboardButtonColor convertColor(ButtonColor color) {
KeyboardButtonColor buttonColorVk;
switch (color) {
case DEFAULT:
buttonColorVk = KeyboardButtonColor.DEFAULT;
break;
case PRIMARY:
buttonColorVk = KeyboardButtonColor.PRIMARY;
break;
case NEGATIVE:
buttonColorVk = KeyboardButtonColor.NEGATIVE;
break;
case POSITIVE:
buttonColorVk = KeyboardButtonColor.POSITIVE;
break;
default:
buttonColorVk = KeyboardButtonColor.DEFAULT;
}
return buttonColorVk;
}
}

View File

@ -3,14 +3,20 @@ package org.sadtech.vkbot.core.distribution;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.vk.api.sdk.objects.messages.Message;
import com.vk.api.sdk.objects.messages.MessageAttachment;
import com.vk.api.sdk.objects.messages.MessageAttachmentType;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.domain.Mail;
import org.sadtech.vkbot.core.service.distribution.MailService;
import org.sadtech.bot.core.domain.attachment.Attachment;
import org.sadtech.bot.core.domain.attachment.AudioMessage;
import org.sadtech.bot.core.service.MailService;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.*;
public class MailSubscriber implements EventSubscribe<JsonObject> {
@ -37,12 +43,14 @@ public class MailSubscriber implements EventSubscribe<JsonObject> {
log.info("Дистрибьютор получил событие - сообщение");
Gson gson = new Gson();
Message userMessage = gson.fromJson(object, Message.class);
log.info(userMessage);
if (userMessage.getPeerId() > 2000000000) {
if (eventDistributionMap.containsKey("chat")) {
eventDistributionMap.get("chat").update(userMessage);
}
} else {
if (admins.contains(userMessage.getUserId()) && eventDistributionMap.containsKey("terminal")) {
if (admins.contains(userMessage.getPeerId()) && eventDistributionMap.containsKey("terminal")) {
log.info("Сообщение отправлено в репозиторий команд");
eventDistributionMap.get("terminal").update(userMessage);
} else {
@ -52,12 +60,34 @@ public class MailSubscriber implements EventSubscribe<JsonObject> {
}
}
public byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
private Mail createMaail(Message message) {
Mail mail = new Mail();
mail.setMessage(message.getBody());
mail.setDate(message.getDate());
mail.setMessage(message.getText());
mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId()));
mail.setId(message.getId());
mail.setPersonId(message.getPeerId());
List<Attachment> attachments = new ArrayList<>();
for (MessageAttachment attachment : message.getAttachments()) {
if (MessageAttachmentType.AUDIO_MESSAGE.equals(attachment.getType())) {
AudioMessage audioMessage = new AudioMessage();
audioMessage.setLinkOdd(attachment.getAudioMessage().getLinkOgg());
attachments.add(audioMessage);
}
}
mail.setAttachments(attachments);
return mail;
}

View File

@ -1,67 +0,0 @@
package org.sadtech.vkbot.core.keyboard;
import com.google.gson.JsonObject;
public class ButtonKeyBoard {
private String type;
private String payload;
private String label;
private ColorButton color;
private ButtonKeyBoard() {
throw new IllegalStateException();
}
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 color(ColorButton color) {
ButtonKeyBoard.this.color = color;
return this;
}
public Builder label(String label) {
ButtonKeyBoard.this.label = label;
return this;
}
public Builder payload(String payload) {
ButtonKeyBoard.this.payload = payload;
return this;
}
public Builder type(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,61 +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;
private boolean oneTime;
private KeyBoard() {
lineKeyBoards = new ArrayList<>();
throw new IllegalStateException();
}
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 static Builder builder() {
return new KeyBoard().new Builder();
}
public void addLine(LineKeyBoard lineKeyBoard) {
lineKeyBoards.add(lineKeyBoard);
}
public class Builder {
private Builder() {
}
public Builder lineKeyBoard(LineKeyBoard lineKeyBoard) {
KeyBoard.this.lineKeyBoards.add(lineKeyBoard);
return this;
}
public Builder oneTime(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 {
private KeyBoardService() {
throw new IllegalStateException();
}
public static KeyBoard keyBoardYesNo() {
ButtonKeyBoard yesButton = ButtonKeyBoard.builder().color(ColorButton.POSITIVE).label("Да").payload("{\"button\": \"yes\"}").build();
ButtonKeyBoard noButton = ButtonKeyBoard.builder().color(ColorButton.NEGATIVE).label("Нет").payload("{\"button\": \"no\"}").build();
LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build();
return KeyBoard.builder().lineKeyBoard(lineKeyBoard).oneTime(true).build();
}
public static KeyBoard verticalMenuString(List<String> labelButtons) {
KeyBoard keyBoard = KeyBoard.builder().oneTime(true).build();
for (String labelButton : labelButtons) {
ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().label(labelButton).type("text").payload("{\"button\": \"" + labelButton + "\"}").build();
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
}
return keyBoard;
}
public static KeyBoard verticalMenuButton(List<ButtonKeyBoard> buttonKeyBoards) {
KeyBoard keyBoard = KeyBoard.builder().oneTime(true).build();
for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) {
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
}
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

@ -7,7 +7,7 @@ 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 com.vk.api.sdk.objects.groups.LongPollServer;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.repository.impl.EventRepositoryQueue;
import org.sadtech.bot.core.service.RawEventService;
@ -42,7 +42,7 @@ public class EventListenerVk implements Runnable {
}
public void listen() throws ClientException, ApiException {
GetLongPollServerResponse longPollServer = getLongPollServer();
LongPollServer longPollServer = getLongPollServer();
int lastTimeStamp = longPollServer.getTs();
while (true) {
try {
@ -62,10 +62,10 @@ public class EventListenerVk implements Runnable {
}
}
private GetLongPollServerResponse getLongPollServer() throws ClientException, ApiException {
private LongPollServer getLongPollServer() throws ClientException, ApiException {
log.info("LongPoll сервер инициализирован");
if (actor != null) {
return vk.groups().getLongPollServer(actor).execute();
return vk.groups().getLongPollServer(actor, actor.getGroupId()).execute();
} else {
throw new NullPointerException("Group actor");
}

View File

@ -1,15 +1,21 @@
package org.sadtech.vkbot.core.sender;
import com.google.gson.Gson;
import com.vk.api.sdk.client.VkApiClient;
import com.vk.api.sdk.client.actors.GroupActor;
import com.vk.api.sdk.exceptions.ApiException;
import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.objects.messages.Keyboard;
import com.vk.api.sdk.queries.messages.MessagesSendQuery;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.domain.BoxAnswer;
import org.sadtech.bot.core.sender.Sent;
import org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.VkInsertData;
import org.sadtech.vkbot.core.convert.KeyBoardConvert;
import java.util.Collections;
import java.util.concurrent.ThreadLocalRandom;
public class MailSenderVk implements Sent {
@ -19,6 +25,7 @@ public class MailSenderVk implements Sent {
private final GroupActor groupActor;
private final VkInsertData vkInsertData;
private final Gson gson = new Gson();
public MailSenderVk(VkConnect vkConnect) {
this.vkApiClient = vkConnect.getVkApiClient();
@ -28,7 +35,7 @@ public class MailSenderVk implements Sent {
@Override
public void send(Integer idPerson, String message) {
sendMessage(vkApiClient.messages().send(groupActor).peerId(idPerson).message(message));
sendMessage(vkApiClient.messages().send(groupActor).peerId(idPerson).message(message).randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE)));
}
@Override
@ -38,11 +45,14 @@ public class MailSenderVk implements Sent {
}
private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Integer peerId) {
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId).message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId));
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId).message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId)).randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE));
if (boxAnswer.getKeyboard() != null) {
messages.keyboard(boxAnswer.getKeyboard());
messages.keyboard(KeyBoardConvert.convertKeyboard(boxAnswer.getKeyboard()));
} else {
messages.keyboard("{\"buttons\":[],\"one_time\":true}");
Keyboard keyboard = new Keyboard();
keyboard.setOneTime(true);
keyboard.setButtons(Collections.EMPTY_LIST);
messages.keyboard(keyboard);
}
if (boxAnswer.getLat() != null && boxAnswer.getaLong() != null) {
messages.lat(boxAnswer.getLat()).lng(boxAnswer.getaLong());
@ -57,6 +67,9 @@ public class MailSenderVk implements Sent {
return messages;
}
private void sendMessage(MessagesSendQuery messages) {
try {
messages.execute();
@ -64,4 +77,6 @@ public class MailSenderVk implements Sent {
log.error(e);
}
}
}

View File

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

View File

@ -1,8 +0,0 @@
package org.sadtech.vkbot.core.service.distribution;
import org.sadtech.bot.core.domain.Mail;
import org.sadtech.bot.core.service.EventService;
public interface MailService extends EventService<Mail> {
}

View File

@ -1,70 +0,0 @@
package org.sadtech.vkbot.core.service.distribution.impl;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.domain.Mail;
import org.sadtech.bot.core.repository.MailRepository;
import org.sadtech.bot.core.repository.impl.MailRepositoryList;
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 {
private static final Logger log = Logger.getLogger(MailServiceImpl.class);
private final MailRepository mailRepository;
public MailServiceImpl() {
this.mailRepository = new MailRepositoryList();
}
public MailServiceImpl(MailRepository mailRepository) {
this.mailRepository = mailRepository;
}
@Override
public void add(Mail mail) {
mailRepository.add(mail);
log.info("Сообщение добавлено в репозиторий");
log.info(mail);
}
@Override
public List<Mail> getFirstEventByTime(Integer timeFrom, Integer timeTo) {
log.info("Запрошены сообщения " + timeFrom + "-" + timeTo);
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
Set<Integer> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>();
for (int i = mails.size() - 1; i >= 0; i--) {
if (!people.contains(mails.get(i).getPersonId())) {
returnMails.add(mails.get(i));
people.add(mails.get(i).getPersonId());
}
}
return returnMails;
}
@Override
public List<Mail> getLastEventByTime(Integer timeFrom, Integer timeTo) {
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
Set<Integer> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>();
for (Mail mail : mails) {
if (!people.contains(mail.getPersonId())) {
returnMails.add(mail);
people.add(mail.getPersonId());
}
}
return returnMails;
}
@Override
public List<Mail> getEvent(Integer timeFrom, Integer timeTo) {
log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo);
return mailRepository.getMailByTime(timeFrom, timeTo);
}
}