diff --git a/pom.xml b/pom.xml index 9b8e494..b8866a5 100644 --- a/pom.xml +++ b/pom.xml @@ -23,9 +23,9 @@ - 0.6.1-SNAPSHOT + 0.6.2-SNAPSHOT - 1.0.2 + 0.5.13-SNAPSHOT 1.2.17 diff --git a/src/main/java/org/sadtech/vkbot/core/VkConfig.java b/src/main/java/org/sadtech/vkbot/core/VkConfig.java index c0adbe5..3b0bd52 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkConfig.java +++ b/src/main/java/org/sadtech/vkbot/core/VkConfig.java @@ -4,76 +4,36 @@ 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; + private VkConfigGroup configGroup; + private VkConfigUser configUser; - public String getGroupToken() { - return groupToken; + public VkConfigGroup getConfigGroup() { + return configGroup; } - public void setGroupToken(String groupToken) { - this.groupToken = groupToken; + public void setConfigGroup(VkConfigGroup configGroup) { + this.configGroup = configGroup; } - public Integer getGroupId() { - return groupId; + public VkConfigUser getConfigUser() { + return configUser; } - 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; + public void setConfigUser(VkConfigUser configUser) { + this.configUser = configUser; } @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (!(o instanceof VkConfig)) 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); + return Objects.equals(configGroup, vkConfig.configGroup) && + Objects.equals(configUser, vkConfig.configUser); } @Override public int hashCode() { - return Objects.hash(groupToken, groupId, serviceToken, appId, groupSecretKey, groupPublicKey); + return Objects.hash(configGroup, configUser); } } diff --git a/src/main/java/org/sadtech/vkbot/core/VkConfigGroup.java b/src/main/java/org/sadtech/vkbot/core/VkConfigGroup.java new file mode 100644 index 0000000..083d914 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/VkConfigGroup.java @@ -0,0 +1,79 @@ +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); + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/VkConfigUser.java b/src/main/java/org/sadtech/vkbot/core/VkConfigUser.java new file mode 100644 index 0000000..7912c09 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/VkConfigUser.java @@ -0,0 +1,39 @@ +package org.sadtech.vkbot.core; + +import java.util.Objects; + +public class VkConfigUser { + + private Integer userId; + private String token; + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof VkConfigUser)) return false; + VkConfigUser that = (VkConfigUser) o; + return Objects.equals(userId, that.userId) && + Objects.equals(token, that.token); + } + + @Override + public int hashCode() { + return Objects.hash(userId, token); + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/VkConnect.java b/src/main/java/org/sadtech/vkbot/core/VkConnect.java index d4c90ce..12e0579 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkConnect.java +++ b/src/main/java/org/sadtech/vkbot/core/VkConnect.java @@ -3,6 +3,7 @@ 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.client.actors.UserActor; import com.vk.api.sdk.httpclient.HttpTransportClient; public class VkConnect { @@ -12,6 +13,7 @@ public class VkConnect { private VkApiClient vkApiClient; private GroupActor groupActor; private ServiceActor serviceActor; + private UserActor userActor; public VkConnect(VkConfig vkConfig) { this.vkConfig = vkConfig; @@ -35,6 +37,19 @@ public class VkConnect { } } + public UserActor getUserActor() { + if (userActor != null) { + return userActor; + } else { + initUserActor(); + return userActor; + } + } + + private void initUserActor() { + userActor = new UserActor(vkConfig.getConfigUser().getUserId(), vkConfig.getConfigUser().getToken()); + } + public ServiceActor getServiceActor() { if (serviceActor != null) { return serviceActor; @@ -49,10 +64,10 @@ public class VkConnect { } private void initGroupActor() { - groupActor = new GroupActor(vkConfig.getGroupId(), vkConfig.getGroupToken()); + groupActor = new GroupActor(vkConfig.getConfigGroup().getGroupId(), vkConfig.getConfigGroup().getGroupToken()); } private void initServiceActor() { - serviceActor = new ServiceActor(vkConfig.getAppId(), vkConfig.getServiceToken()); + serviceActor = new ServiceActor(vkConfig.getConfigGroup().getAppId(), vkConfig.getConfigGroup().getServiceToken()); } } diff --git a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java index a09b446..5ef5511 100644 --- a/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java +++ b/src/main/java/org/sadtech/vkbot/core/convert/MessageMailConvert.java @@ -2,10 +2,10 @@ package org.sadtech.vkbot.core.convert; import com.vk.api.sdk.objects.messages.Message; import com.vk.api.sdk.objects.messages.MessageAttachment; -import org.sadtech.bot.core.domain.attachment.Attachment; -import org.sadtech.bot.core.domain.attachment.AudioMessage; -import org.sadtech.bot.core.domain.attachment.Geo; import org.sadtech.bot.core.domain.content.Mail; +import org.sadtech.bot.core.domain.content.attachment.Attachment; +import org.sadtech.bot.core.domain.content.attachment.AudioMessage; +import org.sadtech.bot.core.domain.content.attachment.Geo; import java.time.Instant; import java.time.LocalDateTime; @@ -18,14 +18,16 @@ public class MessageMailConvert implements Convert { public Mail converting(Message message) { Mail mail = new Mail(); mail.setMessage(message.getText()); - mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); + mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); mail.setId(message.getId()); mail.setPersonId(message.getPeerId()); mail.setAttachments(message.getAttachments() .stream() .map(this::convertAttachment) .collect(Collectors.toList())); - mail.getAttachments().add(convertGeo(message.getGeo())); + if (message.getGeo()!=null) { + mail.getAttachments().add(convertGeo(message.getGeo())); + } return mail; } diff --git a/src/main/java/org/sadtech/vkbot/core/convert/TopicCommentToBoardComment.java b/src/main/java/org/sadtech/vkbot/core/convert/TopicCommentToBoardComment.java new file mode 100644 index 0000000..f3a9d3e --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/convert/TopicCommentToBoardComment.java @@ -0,0 +1,23 @@ +package org.sadtech.vkbot.core.convert; + +import com.vk.api.sdk.objects.board.TopicComment; +import org.sadtech.bot.core.domain.content.BoardComment; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.util.TimeZone; + +public class TopicCommentToBoardComment implements Convert { + @Override + public BoardComment converting(TopicComment target) { + BoardComment boardComment = new BoardComment(); + boardComment.setTopicId(target.getTopicId()); + boardComment.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(target.getDate()), TimeZone.getDefault().toZoneId())); + boardComment.setMessage(target.getText()); + boardComment.setPersonId(target.getFromId()); + + + System.out.println(target); + return boardComment; + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java index d100355..d65c753 100644 --- a/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java +++ b/src/main/java/org/sadtech/vkbot/core/distribution/AccountSubscribe.java @@ -15,7 +15,6 @@ public class AccountSubscribe extends AbstractBasketSubscribe private final AccountService accountService; private final Sent sent; private BoxAnswer answerSuccessfulPayment; - private BoxAnswer answerFailPayment; public AccountSubscribe(AccountService accountService, Sent sent) { this.accountService = accountService; @@ -30,14 +29,6 @@ public class AccountSubscribe extends AbstractBasketSubscribe this.answerSuccessfulPayment = answerSuccessfulPayment; } - public BoxAnswer getAnswerFailPayment() { - return answerFailPayment; - } - - public void setAnswerFailPayment(BoxAnswer answerFailPayment) { - this.answerFailPayment = answerFailPayment; - } - @Override protected boolean check(Message userMessage) { return userMessage.getAttachments().size() > 0 && "Денежный перевод".equals(userMessage.getAttachments().get(0).getLink().getCaption()); @@ -47,10 +38,12 @@ public class AccountSubscribe extends AbstractBasketSubscribe public void processing(Message message) { if (message.getText() != null) { try { - if (accountService.pay(Integer.valueOf(message.getText()), message.getPeerId(), Double.valueOf(message.getAttachments().get(0).getLink().getTitle().split(" ")[0]))) { + Double valueSum = Double.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); } } catch (PaymentException e) { + log.error(e.getMessage()); sent.send(message.getPeerId(), BoxAnswer.builder().message(e.getDescription()).build()); } } diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java b/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java new file mode 100644 index 0000000..6b918b8 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/distribution/BoardCommentSubscribe.java @@ -0,0 +1,96 @@ +package org.sadtech.vkbot.core.distribution; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.vk.api.sdk.objects.board.TopicComment; +import org.sadtech.bot.core.domain.content.BoardComment; +import org.sadtech.bot.core.service.BoardCommentService; +import org.sadtech.vkbot.core.convert.Convert; +import org.sadtech.vkbot.core.convert.TopicCommentToBoardComment; + +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class BoardCommentSubscribe extends AbstractBasketSubscribe { + + private final BoardCommentService boardCommentService; + private final Convert topicConvert = new TopicCommentToBoardComment(); + private Set answerTopicsId; + private Set noAnswerPersonId; + private Boolean respondAppeal = true; + + public BoardCommentSubscribe(BoardCommentService boardCommentService) { + this.boardCommentService = boardCommentService; + this.convert = (object) -> { + Gson gson = new Gson(); + return gson.fromJson(object.getAsJsonObject("object"), TopicComment.class); + }; + } + + public Set getAnswerTopicsId() { + return answerTopicsId; + } + + public void setAnswerTopicsId(Set answerTopicsId) { + this.answerTopicsId = answerTopicsId; + } + + public Set getNoAnswerPersonId() { + return noAnswerPersonId; + } + + public void setNoAnswerPersonId(Set noAnswerPersonId) { + this.noAnswerPersonId = noAnswerPersonId; + } + + public Boolean getRespondAppeal() { + return respondAppeal; + } + + public void setRespondAppeal(Boolean respondAppeal) { + this.respondAppeal = respondAppeal; + } + + @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); + } + + private boolean checkRespondAppeal(String message, Integer groupId) { + if (respondAppeal) { + Pattern pattern = Pattern.compile("\\[club" + groupId); + Matcher m = pattern.matcher(message); + return m.find(); + } else { + return true; + } + } + + private boolean checkTopic(Integer topicId) { + if (answerTopicsId == null) { + return true; + } else { + return answerTopicsId.contains(topicId); + } + } + + private boolean checkPerson(Integer personId) { + if (noAnswerPersonId == null) { + return true; + } else { + return !noAnswerPersonId.contains(personId); + } + } + + + @Override + public void processing(TopicComment object) { + if (checkPerson(object.getFromId()) && checkTopic(object.getTopicId())) { + boardCommentService.add(topicConvert.converting(object)); + } + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java new file mode 100644 index 0000000..133f10f --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/sender/BoardCommentSenderVk.java @@ -0,0 +1,41 @@ +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.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.exception.MailSendException; +import org.sadtech.bot.core.service.sender.Sent; +import org.sadtech.vkbot.core.VkConnect; +import org.sadtech.vkbot.core.VkInsertData; + +public class BoardCommentSenderVk implements Sent { + + private final VkApiClient vkApiClient; + private final GroupActor groupActor; + private final UserActor userActor; + private final VkInsertData vkInsertData; + + public BoardCommentSenderVk(VkConnect vkConnect) { + this.vkApiClient = vkConnect.getVkApiClient(); + this.groupActor = vkConnect.getGroupActor(); + this.vkInsertData = new VkInsertData(vkConnect); + this.userActor = vkConnect.getUserActor(); + } + + @Override + public void send(Integer integer, BoxAnswer boxAnswer) { + throw new MailSendException(); + } + + @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(); + } catch (ApiException | ClientException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java index 0af6de0..07e6c80 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSenderVk.java @@ -34,14 +34,14 @@ public class MailSenderVk implements Sent { } @Override - public void send(Integer idPerson, String message) { - sendMessage(vkApiClient.messages().send(groupActor).peerId(idPerson).message(message).randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE))); + public void send(Integer personId, BoxAnswer boxAnswer) { + MessagesSendQuery messagesSendQuery = createMessage(boxAnswer, personId); + sendMessage(messagesSendQuery); } @Override - public void send(Integer idPerson, BoxAnswer boxAnswer) { - MessagesSendQuery messagesSendQuery = createMessage(boxAnswer, idPerson); - sendMessage(messagesSendQuery); + public void send(Integer integer, Integer integer1, BoxAnswer boxAnswer) { + } private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Integer peerId) { @@ -55,7 +55,7 @@ public class MailSenderVk implements Sent { messages.keyboard(keyboard); } if (boxAnswer.getCoordinates() != null) { - messages.lat(boxAnswer.getCoordinates().getLat()).lng(boxAnswer.getCoordinates().getaLong()); + messages.lat(boxAnswer.getCoordinates().getLatitude()).lng(boxAnswer.getCoordinates().getLongitude()); } if (boxAnswer.getStickerId() != null) { try {