Множественные изменения
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>org.sadtech.vkbot</groupId>
|
||||
<artifactId>vkbot-core</artifactId>
|
||||
<version>0.2.1-SNAPSHOT</version>
|
||||
<version>0.2.1-RELEASE</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<build>
|
||||
@@ -23,9 +23,9 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<bot.core.ver>0.6.2-SNAPSHOT</bot.core.ver>
|
||||
<bot.core.ver>0.6.2-RELEASE</bot.core.ver>
|
||||
|
||||
<vksdk.ver>0.5.13-SNAPSHOT</vksdk.ver>
|
||||
<vksdk.ver>0.5.13-FORK</vksdk.ver>
|
||||
<log4j.ver>1.2.17</log4j.ver>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package org.sadtech.vkbot.core;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class VkConfig {
|
||||
|
||||
private VkConfigGroup configGroup;
|
||||
private VkConfigUser configUser;
|
||||
|
||||
public VkConfigGroup getConfigGroup() {
|
||||
return configGroup;
|
||||
}
|
||||
|
||||
public void setConfigGroup(VkConfigGroup configGroup) {
|
||||
this.configGroup = configGroup;
|
||||
}
|
||||
|
||||
public VkConfigUser getConfigUser() {
|
||||
return configUser;
|
||||
}
|
||||
|
||||
public void setConfigUser(VkConfigUser configUser) {
|
||||
this.configUser = configUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof VkConfig)) return false;
|
||||
VkConfig vkConfig = (VkConfig) o;
|
||||
return Objects.equals(configGroup, vkConfig.configGroup) &&
|
||||
Objects.equals(configUser, vkConfig.configUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(configGroup, configUser);
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
package org.sadtech.vkbot.core;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class VkConfigGroup {
|
||||
|
||||
private String groupToken;
|
||||
private Integer groupId;
|
||||
private String serviceToken;
|
||||
private Integer appId;
|
||||
private String groupSecretKey;
|
||||
private String groupPublicKey;
|
||||
|
||||
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 instanceof VkConfigGroup)) return false;
|
||||
VkConfigGroup that = (VkConfigGroup) o;
|
||||
return Objects.equals(groupToken, that.groupToken) &&
|
||||
Objects.equals(groupId, that.groupId) &&
|
||||
Objects.equals(serviceToken, that.serviceToken) &&
|
||||
Objects.equals(appId, that.appId) &&
|
||||
Objects.equals(groupSecretKey, that.groupSecretKey) &&
|
||||
Objects.equals(groupPublicKey, that.groupPublicKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(groupToken, groupId, serviceToken, appId, groupSecretKey, groupPublicKey);
|
||||
}
|
||||
}
|
||||
75
src/main/java/org/sadtech/vkbot/core/config/VkConfig.java
Normal file
75
src/main/java/org/sadtech/vkbot/core/config/VkConfig.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package org.sadtech.vkbot.core.config;
|
||||
|
||||
import org.sadtech.vkbot.core.exception.ConfigException;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
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("Конфигурация сервиса не найдена"));
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new VkConfig().new Builder();
|
||||
}
|
||||
|
||||
public class Builder {
|
||||
private Builder() {
|
||||
|
||||
}
|
||||
|
||||
public Builder configGroup(VkConfigGroup configGroup) {
|
||||
VkConfig.this.configGroup = configGroup;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder configUser(VkConfigUser configUser) {
|
||||
VkConfig.this.configUser = configUser;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder configService(VkConfigService configService) {
|
||||
VkConfig.this.configService = configService;
|
||||
return this;
|
||||
}
|
||||
|
||||
public VkConfig build() {
|
||||
return VkConfig.this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof VkConfig)) return false;
|
||||
VkConfig vkConfig = (VkConfig) o;
|
||||
return Objects.equals(configGroup, vkConfig.configGroup) &&
|
||||
Objects.equals(configUser, vkConfig.configUser) &&
|
||||
Objects.equals(configService, vkConfig.configService);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(configGroup, configUser, configService);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.sadtech.vkbot.core.config;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class VkConfigGroup {
|
||||
|
||||
private String groupToken;
|
||||
private Integer groupId;
|
||||
|
||||
public String getGroupToken() {
|
||||
return groupToken;
|
||||
}
|
||||
|
||||
public Integer getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new VkConfigGroup().new Builder();
|
||||
}
|
||||
|
||||
public class Builder {
|
||||
private Builder() {
|
||||
|
||||
}
|
||||
|
||||
public Builder groupId(Integer groupId) {
|
||||
VkConfigGroup.this.groupId = groupId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder groupToken(String groupToken) {
|
||||
VkConfigGroup.this.groupToken = groupToken;
|
||||
return this;
|
||||
}
|
||||
|
||||
public VkConfigGroup build() {
|
||||
return VkConfigGroup.this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof VkConfigGroup)) return false;
|
||||
VkConfigGroup that = (VkConfigGroup) o;
|
||||
return Objects.equals(groupToken, that.groupToken) &&
|
||||
Objects.equals(groupId, that.groupId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(groupToken, groupId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.sadtech.vkbot.core.config;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class VkConfigService {
|
||||
|
||||
private String serviceToken;
|
||||
private Integer appId;
|
||||
|
||||
public String getServiceToken() {
|
||||
return serviceToken;
|
||||
}
|
||||
|
||||
public Integer getAppId() {
|
||||
return appId;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new VkConfigService().new Builder();
|
||||
}
|
||||
|
||||
public class Builder {
|
||||
private Builder() {
|
||||
|
||||
}
|
||||
|
||||
public Builder serviceToken(String serviceToken) {
|
||||
VkConfigService.this.serviceToken = serviceToken;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder appId(Integer appId) {
|
||||
VkConfigService.this.appId = appId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public VkConfigService build() {
|
||||
return VkConfigService.this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof VkConfigService)) return false;
|
||||
VkConfigService that = (VkConfigService) o;
|
||||
return Objects.equals(serviceToken, that.serviceToken) &&
|
||||
Objects.equals(appId, that.appId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(serviceToken, appId);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.sadtech.vkbot.core;
|
||||
package org.sadtech.vkbot.core.config;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -7,22 +7,44 @@ public class VkConfigUser {
|
||||
private Integer userId;
|
||||
private String token;
|
||||
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
private VkConfigUser() {
|
||||
|
||||
}
|
||||
|
||||
public void setUserId(Integer userId) {
|
||||
this.userId = userId;
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
public static Builder builder() {
|
||||
return new VkConfigUser().new Builder();
|
||||
}
|
||||
|
||||
public class Builder {
|
||||
private Builder() {
|
||||
|
||||
}
|
||||
|
||||
public Builder userId(Integer userId) {
|
||||
VkConfigUser.this.userId = userId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder token(String token) {
|
||||
VkConfigUser.this.token = token;
|
||||
return this;
|
||||
}
|
||||
|
||||
public VkConfigUser build() {
|
||||
return VkConfigUser.this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.sadtech.vkbot.core;
|
||||
package org.sadtech.vkbot.core.config;
|
||||
|
||||
import com.vk.api.sdk.client.VkApiClient;
|
||||
import com.vk.api.sdk.client.actors.GroupActor;
|
||||
@@ -68,6 +68,6 @@ public class VkConnect {
|
||||
}
|
||||
|
||||
private void initServiceActor() {
|
||||
serviceActor = new ServiceActor(vkConfig.getConfigGroup().getAppId(), vkConfig.getConfigGroup().getServiceToken());
|
||||
serviceActor = new ServiceActor(vkConfig.getConfigService().getAppId(), vkConfig.getConfigService().getServiceToken());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.sadtech.vkbot.core.convert;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Convert<T, C> {
|
||||
|
||||
C converting(T target);
|
||||
|
||||
@@ -5,17 +5,23 @@ 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 org.sadtech.bot.core.domain.keyboard.button.KeyBoardButtonAccount;
|
||||
import org.sadtech.bot.core.domain.keyboard.button.KeyBoardButtonText;
|
||||
import org.sadtech.vkbot.core.config.VkConnect;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class KeyBoardConvert {
|
||||
public class KeyBoardConvert {
|
||||
|
||||
private KeyBoardConvert() {
|
||||
private final VkConnect vkConnect;
|
||||
|
||||
public KeyBoardConvert(VkConnect vkConnect) {
|
||||
this.vkConnect = vkConnect;
|
||||
}
|
||||
|
||||
public static Keyboard convertKeyboard(KeyBoard keyboard) {
|
||||
|
||||
public Keyboard convertKeyboard(KeyBoard keyboard) {
|
||||
Keyboard keyboardVk = new Keyboard();
|
||||
keyboardVk.setOneTime(keyboard.isOneTime());
|
||||
|
||||
@@ -32,20 +38,58 @@ public final class KeyBoardConvert {
|
||||
return keyboardVk;
|
||||
}
|
||||
|
||||
private static KeyboardButton convertButton(KeyBoardButton button) {
|
||||
private 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());
|
||||
switch (button.getType()) {
|
||||
case TEXT:
|
||||
KeyBoardButtonText buttonText = (KeyBoardButtonText) button;
|
||||
buttonVk.setColor(convertColor(buttonText.getColor()));
|
||||
break;
|
||||
}
|
||||
|
||||
KeyboardButtonAction buttonActionVk = createActionButton(button);
|
||||
buttonVk.setAction(buttonActionVk);
|
||||
return buttonVk;
|
||||
}
|
||||
|
||||
|
||||
private KeyboardButtonAction createActionButton(KeyBoardButton button) {
|
||||
KeyboardButtonAction keyboardButtonAction = new KeyboardButtonAction();
|
||||
switch (button.getType()) {
|
||||
case TEXT:
|
||||
KeyBoardButtonText buttonText = (KeyBoardButtonText) button;
|
||||
keyboardButtonAction.setType(KeyboardButtonActionType.TEXT);
|
||||
keyboardButtonAction.setLabel(buttonText.getLabel());
|
||||
break;
|
||||
case ACCOUNT:
|
||||
KeyBoardButtonAccount buttonAccount = (KeyBoardButtonAccount) button;
|
||||
keyboardButtonAction.setType(KeyboardButtonActionType.VKPAY);
|
||||
keyboardButtonAction.setHash(createHash(buttonAccount));
|
||||
break;
|
||||
}
|
||||
return keyboardButtonAction;
|
||||
}
|
||||
|
||||
private String createHash(KeyBoardButtonAccount button) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
if (button.getAmount()!=null) {
|
||||
stringBuilder
|
||||
.append("action=pay-to-group&amount=")
|
||||
.append(button.getAmount()).append("&group_id=")
|
||||
.append(vkConnect.getGroupActor().getGroupId());
|
||||
} else {
|
||||
stringBuilder
|
||||
.append("action=transfer-to-group")
|
||||
.append(button.getAmount()).append("&group_id=")
|
||||
.append(vkConnect.getGroupActor().getGroupId());
|
||||
}
|
||||
if (button.getAccountId()!=null) {
|
||||
stringBuilder.append("&description=").append(button.getAccountId());
|
||||
}
|
||||
return stringBuilder.append("&aid=").append(vkConnect.getServiceActor().getId()).toString();
|
||||
}
|
||||
|
||||
private static KeyboardButtonColor convertColor(ButtonColor color) {
|
||||
KeyboardButtonColor buttonColorVk;
|
||||
switch (color) {
|
||||
|
||||
@@ -35,6 +35,7 @@ public class MessageMailConvert implements Convert<Message, Mail> {
|
||||
return Geo.builder()
|
||||
.coordinate(geoVk.getCoordinates().getLatitude(),
|
||||
geoVk.getCoordinates().getLongitude())
|
||||
.city(geoVk.getPlace().getCity())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ public class TopicCommentToBoardComment implements Convert<TopicComment, BoardCo
|
||||
@Override
|
||||
public BoardComment converting(TopicComment target) {
|
||||
BoardComment boardComment = new BoardComment();
|
||||
boardComment.setTopicId(target.getTopicId());
|
||||
boardComment.setContentId(target.getTopicId());
|
||||
boardComment.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(target.getDate()), TimeZone.getDefault().toZoneId()));
|
||||
boardComment.setMessage(target.getText());
|
||||
boardComment.setPersonId(target.getFromId());
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.sadtech.vkbot.core.distribution;
|
||||
import org.sadtech.vkbot.core.convert.Convert;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public abstract class AbstractBasketSubscribe<S, C> {
|
||||
|
||||
@@ -14,6 +15,33 @@ public abstract class AbstractBasketSubscribe<S, C> {
|
||||
convert = (object) -> (C) object;
|
||||
}
|
||||
|
||||
protected abstract boolean check(S object);
|
||||
|
||||
public void update(S object) {
|
||||
C newObject = convert.converting(object);
|
||||
if (!goNextSubscribe(newObject)) {
|
||||
processing(newObject);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean goNextSubscribe(C object) {
|
||||
AtomicBoolean flag = new AtomicBoolean(false);
|
||||
if (prioritySubscribe != null && prioritySubscribe.check(object)) {
|
||||
prioritySubscribe.update(object);
|
||||
flag.set(true);
|
||||
} else if (basketSubscribes != null) {
|
||||
basketSubscribes.stream()
|
||||
.filter(basketSubscribe -> basketSubscribe.check(object))
|
||||
.forEach(basketSubscribe -> {
|
||||
basketSubscribe.update(object);
|
||||
flag.set(true);
|
||||
});
|
||||
}
|
||||
return flag.get();
|
||||
}
|
||||
|
||||
public abstract void processing(C object);
|
||||
|
||||
public Set<AbstractBasketSubscribe> getBasketSubscribes() {
|
||||
return basketSubscribes;
|
||||
}
|
||||
@@ -38,30 +66,4 @@ public abstract class AbstractBasketSubscribe<S, C> {
|
||||
this.convert = convert;
|
||||
}
|
||||
|
||||
protected abstract boolean check(S object);
|
||||
|
||||
private boolean goNextSubscribe(C object) {
|
||||
boolean flag = false;
|
||||
if (prioritySubscribe != null && prioritySubscribe.check(object)) {
|
||||
prioritySubscribe.update(object);
|
||||
flag = true;
|
||||
} else if (basketSubscribes != null) {
|
||||
for (AbstractBasketSubscribe basketSubscribe : basketSubscribes) {
|
||||
if (basketSubscribe.check(object)) {
|
||||
basketSubscribe.update(object);
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public void update(S object) {
|
||||
C newObject = convert.converting(object);
|
||||
if (!goNextSubscribe(newObject)) {
|
||||
processing(newObject);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void processing(C object);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.sadtech.vkbot.core.distribution;
|
||||
|
||||
import com.vk.api.sdk.objects.messages.Message;
|
||||
import com.vk.api.sdk.objects.messages.MessageAttachmentType;
|
||||
import org.sadtech.bot.core.domain.BoxAnswer;
|
||||
import org.sadtech.bot.core.exception.PaymentException;
|
||||
import org.sadtech.bot.core.service.AccountService;
|
||||
@@ -21,24 +22,18 @@ public class AccountSubscribe extends AbstractBasketSubscribe<Message, Message>
|
||||
this.sent = sent;
|
||||
}
|
||||
|
||||
public BoxAnswer getAnswerSuccessfulPayment() {
|
||||
return answerSuccessfulPayment;
|
||||
}
|
||||
|
||||
public void setAnswerSuccessfulPayment(BoxAnswer answerSuccessfulPayment) {
|
||||
this.answerSuccessfulPayment = answerSuccessfulPayment;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean check(Message userMessage) {
|
||||
return userMessage.getAttachments().size() > 0 && "Денежный перевод".equals(userMessage.getAttachments().get(0).getLink().getCaption());
|
||||
return userMessage.getAttachments().size() > 0
|
||||
&& MessageAttachmentType.LINK.equals(userMessage.getAttachments().get(0).getType())
|
||||
&& "Payment awaiting acceptance".equals(userMessage.getAttachments().get(0).getLink().getCaption());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processing(Message message) {
|
||||
if (message.getText() != null) {
|
||||
try {
|
||||
Double valueSum = Double.valueOf(message.getAttachments().get(0).getLink().getTitle().split(" ")[0]);
|
||||
Integer valueSum = Integer.valueOf(message.getAttachments().get(0).getLink().getTitle().split(" ")[0]);
|
||||
if (accountService.pay(Integer.valueOf(message.getText()), message.getPeerId(), valueSum) && answerSuccessfulPayment != null) {
|
||||
sent.send(message.getPeerId(), answerSuccessfulPayment);
|
||||
}
|
||||
@@ -48,4 +43,12 @@ public class AccountSubscribe extends AbstractBasketSubscribe<Message, Message>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BoxAnswer getAnswerSuccessfulPayment() {
|
||||
return answerSuccessfulPayment;
|
||||
}
|
||||
|
||||
public void setAnswerSuccessfulPayment(BoxAnswer answerSuccessfulPayment) {
|
||||
this.answerSuccessfulPayment = answerSuccessfulPayment;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,9 +55,16 @@ public class BoardCommentSubscribe extends AbstractBasketSubscribe<JsonObject, T
|
||||
@Override
|
||||
protected boolean check(JsonObject object) {
|
||||
String type = object.get("type").getAsString();
|
||||
String message = object.getAsJsonObject("object").get("text").getAsString();
|
||||
Integer groupId = object.get("group_id").getAsInt();
|
||||
return "board_post_new".equals(type) && checkRespondAppeal(message, groupId);
|
||||
return "board_post_new".equals(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processing(TopicComment object) {
|
||||
if (checkPerson(object.getFromId())
|
||||
&& checkTopic(object.getTopicId())
|
||||
&& checkRespondAppeal(object.getText(), object.getFromId())) {
|
||||
boardCommentService.add(topicConvert.converting(object));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkRespondAppeal(String message, Integer groupId) {
|
||||
@@ -86,11 +93,4 @@ public class BoardCommentSubscribe extends AbstractBasketSubscribe<JsonObject, T
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void processing(TopicComment object) {
|
||||
if (checkPerson(object.getFromId()) && checkTopic(object.getTopicId())) {
|
||||
boardCommentService.add(topicConvert.converting(object));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.sadtech.vkbot.core.exception;
|
||||
|
||||
public class ConfigException extends RuntimeException {
|
||||
|
||||
public ConfigException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,17 @@
|
||||
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.callback.longpoll.responses.GetLongPollEventsResponse;
|
||||
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;
|
||||
import org.sadtech.bot.core.service.impl.RawEventServiceImpl;
|
||||
import org.sadtech.vkbot.core.VkConnect;
|
||||
import org.sadtech.vkbot.core.config.VkConnect;
|
||||
|
||||
public class EventListenerVk implements Runnable {
|
||||
|
||||
@@ -20,9 +19,7 @@ public class EventListenerVk implements Runnable {
|
||||
|
||||
private final VkApiClient vk;
|
||||
private final GroupActor actor;
|
||||
|
||||
private static final Integer DEFAULT_WAIT_TIME = 25;
|
||||
|
||||
private final RawEventService rawEventService;
|
||||
|
||||
public EventListenerVk(VkConnect vkConnect) {
|
||||
@@ -32,31 +29,30 @@ public class EventListenerVk implements Runnable {
|
||||
}
|
||||
|
||||
public EventListenerVk(VkConnect vkConnect, RawEventService rawEventService) {
|
||||
this.vk = vkConnect.getVkApiClient();
|
||||
this.actor = vkConnect.getGroupActor();
|
||||
vk = vkConnect.getVkApiClient();
|
||||
actor = vkConnect.getGroupActor();
|
||||
this.rawEventService = rawEventService;
|
||||
}
|
||||
|
||||
public RawEventService getRawEventService() {
|
||||
return rawEventService;
|
||||
}
|
||||
|
||||
public void listen() throws ClientException, ApiException {
|
||||
LongPollServer longPollServer = getLongPollServer();
|
||||
int lastTimeStamp = longPollServer.getTs();
|
||||
int lastTimeStamp = Integer.parseInt(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);
|
||||
}
|
||||
GetLongPollEventsResponse eventsResponse = vk.longPoll()
|
||||
.getEvents(longPollServer.getServer(), longPollServer.getKey(), lastTimeStamp)
|
||||
.waitTime(DEFAULT_WAIT_TIME)
|
||||
.execute();
|
||||
eventsResponse.getUpdates().parallelStream().forEach(object -> {
|
||||
log.info("Новое событие от LongPoll\n" + object);
|
||||
rawEventService.add(object);
|
||||
});
|
||||
lastTimeStamp = eventsResponse.getTs();
|
||||
} catch (LongPollServerKeyExpiredException e) {
|
||||
log.error(e.getStackTrace());
|
||||
log.error(e.getMessage());
|
||||
longPollServer = getLongPollServer();
|
||||
} catch (Exception e) {
|
||||
log.error(e.getStackTrace());
|
||||
log.error(e.getMessage());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -76,8 +72,12 @@ public class EventListenerVk implements Runnable {
|
||||
try {
|
||||
listen();
|
||||
} catch (ClientException | ApiException e) {
|
||||
log.error(e);
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public RawEventService getRawEventService() {
|
||||
return rawEventService;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,10 +6,13 @@ import com.vk.api.sdk.client.actors.UserActor;
|
||||
import com.vk.api.sdk.exceptions.ApiException;
|
||||
import com.vk.api.sdk.exceptions.ClientException;
|
||||
import org.sadtech.bot.core.domain.BoxAnswer;
|
||||
import org.sadtech.bot.core.domain.keyboard.KeyBoardButton;
|
||||
import org.sadtech.bot.core.domain.keyboard.KeyBoardLine;
|
||||
import org.sadtech.bot.core.domain.keyboard.button.KeyBoardButtonText;
|
||||
import org.sadtech.bot.core.exception.MailSendException;
|
||||
import org.sadtech.bot.core.service.sender.Sent;
|
||||
import org.sadtech.vkbot.core.VkConnect;
|
||||
import org.sadtech.vkbot.core.VkInsertData;
|
||||
import org.sadtech.vkbot.core.config.VkConnect;
|
||||
import org.sadtech.vkbot.core.utils.VkInsertData;
|
||||
|
||||
public class BoardCommentSenderVk implements Sent {
|
||||
|
||||
@@ -33,7 +36,22 @@ public class BoardCommentSenderVk implements Sent {
|
||||
@Override
|
||||
public void send(Integer contentId, Integer personId, BoxAnswer boxAnswer) {
|
||||
try {
|
||||
vkApiClient.board().createComment(userActor, groupActor.getGroupId(), contentId).message(vkInsertData.insertWords(boxAnswer.getMessage(), personId)).fromGroup(true).execute();
|
||||
StringBuilder insertAnswer = new StringBuilder(vkInsertData.insertWords(boxAnswer.getMessage(), personId));
|
||||
if (boxAnswer.getKeyboard() != null) {
|
||||
insertAnswer.append("\n\nМеню:\n");
|
||||
for (KeyBoardLine keyBoardLine : boxAnswer.getKeyboard().getKeyBoardLines()) {
|
||||
for (KeyBoardButton keyBoardButton : keyBoardLine.getKeyBoardButtons()) {
|
||||
switch (keyBoardButton.getType()) {
|
||||
case TEXT:
|
||||
insertAnswer.append("- ").append(((KeyBoardButtonText)keyBoardButton).getLabel()).append("\n");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vkApiClient.board().createComment(userActor, groupActor.getGroupId(), contentId)
|
||||
.message(insertAnswer.toString()).fromGroup(true).execute();
|
||||
} catch (ApiException | ClientException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
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;
|
||||
@@ -9,10 +8,11 @@ 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.domain.keyboard.KeyBoard;
|
||||
import org.sadtech.bot.core.service.sender.Sent;
|
||||
import org.sadtech.vkbot.core.VkConnect;
|
||||
import org.sadtech.vkbot.core.VkInsertData;
|
||||
import org.sadtech.vkbot.core.config.VkConnect;
|
||||
import org.sadtech.vkbot.core.convert.KeyBoardConvert;
|
||||
import org.sadtech.vkbot.core.utils.VkInsertData;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
@@ -23,14 +23,14 @@ public class MailSenderVk implements Sent {
|
||||
|
||||
private final VkApiClient vkApiClient;
|
||||
private final GroupActor groupActor;
|
||||
|
||||
private final VkInsertData vkInsertData;
|
||||
private final Gson gson = new Gson();
|
||||
private final KeyBoardConvert keyBoardConvert;
|
||||
|
||||
public MailSenderVk(VkConnect vkConnect) {
|
||||
this.vkApiClient = vkConnect.getVkApiClient();
|
||||
this.groupActor = vkConnect.getGroupActor();
|
||||
this.vkInsertData = new VkInsertData(vkConnect);
|
||||
keyBoardConvert = new KeyBoardConvert(vkConnect);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -39,34 +39,37 @@ public class MailSenderVk implements Sent {
|
||||
sendMessage(messagesSendQuery);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Integer integer, Integer integer1, BoxAnswer boxAnswer) {
|
||||
|
||||
}
|
||||
|
||||
private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Integer 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(KeyBoardConvert.convertKeyboard(boxAnswer.getKeyboard()));
|
||||
} else {
|
||||
Keyboard keyboard = new Keyboard();
|
||||
keyboard.setOneTime(true);
|
||||
keyboard.setButtons(Collections.EMPTY_LIST);
|
||||
messages.keyboard(keyboard);
|
||||
}
|
||||
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId)
|
||||
.message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId))
|
||||
.randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE));
|
||||
messages.keyboard(convertKeyBoard(boxAnswer.getKeyboard()));
|
||||
|
||||
if (boxAnswer.getCoordinates() != null) {
|
||||
messages.lat(boxAnswer.getCoordinates().getLatitude()).lng(boxAnswer.getCoordinates().getLongitude());
|
||||
}
|
||||
if (boxAnswer.getStickerId() != null) {
|
||||
try {
|
||||
vkApiClient.messages().send(groupActor).peerId(peerId).stickerId(boxAnswer.getStickerId()).execute();
|
||||
vkApiClient.messages().send(groupActor).peerId(peerId).stickerId(boxAnswer.getStickerId())
|
||||
.randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE)).execute();
|
||||
} catch (ApiException | ClientException e) {
|
||||
log.error(e);
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
private Keyboard convertKeyBoard(KeyBoard keyboard) {
|
||||
if (keyboard != null) {
|
||||
return keyBoardConvert.convertKeyboard(keyboard);
|
||||
} else {
|
||||
Keyboard keyboardVk = new Keyboard();
|
||||
keyboardVk.setOneTime(true);
|
||||
keyboardVk.setButtons(Collections.EMPTY_LIST);
|
||||
return keyboardVk;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void sendMessage(MessagesSendQuery messages) {
|
||||
try {
|
||||
@@ -76,5 +79,9 @@ public class MailSenderVk implements Sent {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Integer integer, Integer integer1, BoxAnswer boxAnswer) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
package org.sadtech.vkbot.core;
|
||||
package org.sadtech.vkbot.core.utils;
|
||||
|
||||
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.client.actors.ServiceActor;
|
||||
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 org.apache.log4j.Logger;
|
||||
import org.sadtech.vkbot.core.config.VkConnect;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -19,11 +20,11 @@ public class VkApi {
|
||||
private static final Logger log = Logger.getLogger(String.valueOf(VkApi.class));
|
||||
|
||||
private final VkApiClient vk;
|
||||
private final GroupActor actor;
|
||||
private final ServiceActor actor;
|
||||
|
||||
public VkApi(VkConnect vkConnect) {
|
||||
vk = vkConnect.getVkApiClient();
|
||||
actor = vkConnect.getGroupActor();
|
||||
actor = vkConnect.getServiceActor();
|
||||
}
|
||||
|
||||
public UserMin getUserMini(Integer id) {
|
||||
@@ -1,4 +1,6 @@
|
||||
package org.sadtech.vkbot.core;
|
||||
package org.sadtech.vkbot.core.utils;
|
||||
|
||||
import org.sadtech.vkbot.core.config.VkConnect;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -11,12 +13,12 @@ public class VkInsertData {
|
||||
this.vkApi = new VkApi(vkConnect);
|
||||
}
|
||||
|
||||
public String insertWords(String message, Integer idUser) {
|
||||
public String insertWords(String message, Integer personId) {
|
||||
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.appendReplacement(result, insert(m.group(0), personId));
|
||||
}
|
||||
m.appendTail(result);
|
||||
return result.toString();
|
||||
Reference in New Issue
Block a user