Архитектурные изменения

This commit is contained in:
Mark Struchkov 2019-02-21 21:27:38 +03:00
parent dd8c3e977a
commit 57ac698715
8 changed files with 124 additions and 53 deletions

17
pom.xml
View File

@ -50,6 +50,23 @@
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>${log4j.ver}</version> <version>${log4j.ver}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>

View File

@ -0,0 +1,69 @@
package org.sadtech.vkbot.core.entity;
import java.util.Objects;
public class BoxAnswer {
private String message;
private String keyboard;
private Float lat;
private Float aLong;
private Integer stickerId;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getKeyboard() {
return keyboard;
}
public void setKeyboard(String keyboard) {
this.keyboard = keyboard;
}
public Float getLat() {
return lat;
}
public void setLat(Float lat) {
this.lat = lat;
}
public Float getaLong() {
return aLong;
}
public void setaLong(Float aLong) {
this.aLong = aLong;
}
public Integer getStickerId() {
return stickerId;
}
public void setStickerId(Integer stickerId) {
this.stickerId = stickerId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BoxAnswer boxAnswer = (BoxAnswer) o;
return Objects.equals(message, boxAnswer.message) &&
Objects.equals(keyboard, boxAnswer.keyboard) &&
Objects.equals(lat, boxAnswer.lat) &&
Objects.equals(aLong, boxAnswer.aLong) &&
Objects.equals(stickerId, boxAnswer.stickerId);
}
@Override
public int hashCode() {
return Objects.hash(message, keyboard, lat, aLong, stickerId);
}
}

View File

@ -2,7 +2,6 @@ package org.sadtech.vkbot.core.insert;
import org.sadtech.vkbot.core.VkApi; import org.sadtech.vkbot.core.VkApi;
import org.sadtech.vkbot.core.VkConnect; import org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.entity.MailSend;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -15,9 +14,9 @@ public class VkInsertData {
this.vkApi = new VkApi(vkConnect); this.vkApi = new VkApi(vkConnect);
} }
public String insertWords(MailSend mailSend, Integer idUser) { public String insertWords(String message, Integer idUser) {
Pattern pattern = Pattern.compile("%(\\w+)%"); Pattern pattern = Pattern.compile("%(\\w+)%");
Matcher m = pattern.matcher(mailSend.getMessage()); Matcher m = pattern.matcher(message);
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
while (m.find()) { while (m.find()) {
m.appendReplacement(result, insert(m.group(0), idUser)); m.appendReplacement(result, insert(m.group(0), idUser));

View File

@ -21,6 +21,8 @@ public class EventListenerVk implements EventListener, Runnable {
private VkApiClient vk; private VkApiClient vk;
private GroupActor actor; private GroupActor actor;
private static Integer DEFAULT_WAIT_TIME = 10;
private RawEventService rawEventService; private RawEventService rawEventService;
public EventListenerVk(VkConnect vkConnect) { public EventListenerVk(VkConnect vkConnect) {
@ -44,7 +46,7 @@ public class EventListenerVk implements EventListener, Runnable {
int lastTimeStamp = longPollServer.getTs(); int lastTimeStamp = longPollServer.getTs();
while (true) { while (true) {
try { try {
GetLongPollEventsResponse eventsResponse = vk.longPoll().getEvents(longPollServer.getServer(), longPollServer.getKey(), lastTimeStamp).waitTime(25).execute(); GetLongPollEventsResponse eventsResponse = vk.longPoll().getEvents(longPollServer.getServer(), longPollServer.getKey(), lastTimeStamp).waitTime(DEFAULT_WAIT_TIME).execute();
for (JsonObject jsonObject: eventsResponse.getUpdates()) { for (JsonObject jsonObject: eventsResponse.getUpdates()) {
log.info("Новое событие от LongPoll\n" + jsonObject); log.info("Новое событие от LongPoll\n" + jsonObject);
rawEventService.add(jsonObject); rawEventService.add(jsonObject);
@ -52,6 +54,7 @@ public class EventListenerVk implements EventListener, Runnable {
lastTimeStamp = eventsResponse.getTs(); lastTimeStamp = eventsResponse.getTs();
} catch (LongPollServerKeyExpiredException e) { } catch (LongPollServerKeyExpiredException e) {
longPollServer = getLongPollServer(); longPollServer = getLongPollServer();
lastTimeStamp = longPollServer.getTs();
} }
} }

View File

@ -7,13 +7,10 @@ import com.vk.api.sdk.exceptions.ClientException;
import com.vk.api.sdk.queries.messages.MessagesSendQuery; import com.vk.api.sdk.queries.messages.MessagesSendQuery;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.sadtech.vkbot.core.VkConnect; import org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.entity.BoxAnswer;
import org.sadtech.vkbot.core.insert.VkInsertData; import org.sadtech.vkbot.core.insert.VkInsertData;
import org.sadtech.vkbot.core.entity.MailSend;
import org.sadtech.vkbot.core.insert.InsertWords;
import java.util.List; public class MailSenderVk implements Sent {
public class MailSenderVk implements MailSent {
public static final Logger log = Logger.getLogger(MailSenderVk.class); public static final Logger log = Logger.getLogger(MailSenderVk.class);
@ -28,19 +25,30 @@ public class MailSenderVk implements MailSent {
this.vkInsertData = new VkInsertData(vkConnect); this.vkInsertData = new VkInsertData(vkConnect);
} }
private MessagesSendQuery createMessage(MailSend mailSend, Integer peerId) { @Override
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId); public void send(Integer idPerson, String message) {
if (mailSend.getKeyboard() != null) { sendMessage(vkApiClient.messages().send(groupActor).peerId(idPerson).message(message));
messages.keyboard(mailSend.getKeyboard()); }
@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 { } else {
messages.keyboard("{\"buttons\":[],\"one_time\":true}"); messages.keyboard("{\"buttons\":[],\"one_time\":true}");
} }
if (mailSend.getLat() != null && mailSend.getaLong() != null) { if (boxAnswer.getLat() != null && boxAnswer.getaLong() != null) {
messages.lat(mailSend.getLat()).lng(mailSend.getaLong()); messages.lat(boxAnswer.getLat()).lng(boxAnswer.getaLong());
} }
if (mailSend.getStickerId() != null) { if (boxAnswer.getStickerId() != null) {
try { try {
vkApiClient.messages().send(groupActor).peerId(peerId).stickerId(mailSend.getStickerId()).execute(); vkApiClient.messages().send(groupActor).peerId(peerId).stickerId(boxAnswer.getStickerId()).execute();
} catch (ApiException | ClientException e) { } catch (ApiException | ClientException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -48,27 +56,6 @@ public class MailSenderVk implements MailSent {
return messages; return messages;
} }
@Override
public void send(MailSend mailSend, Integer peerId, Integer userId) {
MessagesSendQuery messages = createMessage(mailSend, peerId);
if (mailSend.getMessage() != null) {
messages.message(vkInsertData.insertWords(mailSend, userId));
}
sendMessage(messages);
}
@Override
public void send(MailSend mailSend, Integer peerId, Integer userId, List<String> insertWords) {
MessagesSendQuery messages = createMessage(mailSend, peerId);
if (mailSend.getMessage() != null) {
InsertWords insert = new InsertWords();
insert.setInText(vkInsertData.insertWords(mailSend, peerId));
insert.insert(insertWords);
messages.message(insert.getOutText());
}
sendMessage(messages);
}
private void sendMessage(MessagesSendQuery messages) { private void sendMessage(MessagesSendQuery messages) {
try { try {
messages.execute(); messages.execute();
@ -76,5 +63,4 @@ public class MailSenderVk implements MailSent {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@ -1,13 +0,0 @@
package org.sadtech.vkbot.core.sender;
import org.sadtech.vkbot.core.entity.MailSend;
import java.util.List;
public interface MailSent {
void send(MailSend mailSend, Integer peerId, Integer userId);
void send(MailSend mailSend, Integer peerId, Integer userId, List<String> insertWords);
}

View File

@ -0,0 +1,11 @@
package org.sadtech.vkbot.core.sender;
import org.sadtech.vkbot.core.entity.BoxAnswer;
public interface Sent {
void send(Integer idPerson, String message);
void send(Integer idPerson, BoxAnswer boxAnswer);
}

View File

@ -62,7 +62,6 @@ public class MailServiceImpl implements MailService {
@Override @Override
public List<Mail> getFirstMailByTime(Integer timeFrom, Integer timeTo) { public List<Mail> getFirstMailByTime(Integer timeFrom, Integer timeTo) {
log.info("Запрошены сообщения от " + timeFrom + " до " + timeTo);
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo); List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
Set<Person> people = new HashSet<>(); Set<Person> people = new HashSet<>();
List<Mail> returnMails = new ArrayList<>(); List<Mail> returnMails = new ArrayList<>();