Новый тип контента - комментарии в обсуждениях

This commit is contained in:
Mark Struchkov 2019-05-30 15:48:58 +03:00
parent 74d0299342
commit 6cc5daefce
11 changed files with 327 additions and 79 deletions

View File

@ -23,9 +23,9 @@
</build> </build>
<properties> <properties>
<bot.core.ver>0.6.1-SNAPSHOT</bot.core.ver> <bot.core.ver>0.6.2-SNAPSHOT</bot.core.ver>
<vksdk.ver>1.0.2</vksdk.ver> <vksdk.ver>0.5.13-SNAPSHOT</vksdk.ver>
<log4j.ver>1.2.17</log4j.ver> <log4j.ver>1.2.17</log4j.ver>
</properties> </properties>

View File

@ -4,76 +4,36 @@ import java.util.Objects;
public class VkConfig { public class VkConfig {
private String groupToken; private VkConfigGroup configGroup;
private Integer groupId; private VkConfigUser configUser;
private String serviceToken;
private Integer appId;
private String groupSecretKey;
private String groupPublicKey;
public String getGroupToken() { public VkConfigGroup getConfigGroup() {
return groupToken; return configGroup;
} }
public void setGroupToken(String groupToken) { public void setConfigGroup(VkConfigGroup configGroup) {
this.groupToken = groupToken; this.configGroup = configGroup;
} }
public Integer getGroupId() { public VkConfigUser getConfigUser() {
return groupId; return configUser;
} }
public void setGroupId(Integer groupId) { public void setConfigUser(VkConfigUser configUser) {
this.groupId = groupId; this.configUser = configUser;
}
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 @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (!(o instanceof VkConfig)) return false;
VkConfig vkConfig = (VkConfig) o; VkConfig vkConfig = (VkConfig) o;
return Objects.equals(groupToken, vkConfig.groupToken) && return Objects.equals(configGroup, vkConfig.configGroup) &&
Objects.equals(groupId, vkConfig.groupId) && Objects.equals(configUser, vkConfig.configUser);
Objects.equals(serviceToken, vkConfig.serviceToken) &&
Objects.equals(appId, vkConfig.appId) &&
Objects.equals(groupSecretKey, vkConfig.groupSecretKey) &&
Objects.equals(groupPublicKey, vkConfig.groupPublicKey);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(groupToken, groupId, serviceToken, appId, groupSecretKey, groupPublicKey); return Objects.hash(configGroup, configUser);
} }
} }

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -3,6 +3,7 @@ package org.sadtech.vkbot.core;
import com.vk.api.sdk.client.VkApiClient; import com.vk.api.sdk.client.VkApiClient;
import com.vk.api.sdk.client.actors.GroupActor; import com.vk.api.sdk.client.actors.GroupActor;
import com.vk.api.sdk.client.actors.ServiceActor; import com.vk.api.sdk.client.actors.ServiceActor;
import com.vk.api.sdk.client.actors.UserActor;
import com.vk.api.sdk.httpclient.HttpTransportClient; import com.vk.api.sdk.httpclient.HttpTransportClient;
public class VkConnect { public class VkConnect {
@ -12,6 +13,7 @@ public class VkConnect {
private VkApiClient vkApiClient; private VkApiClient vkApiClient;
private GroupActor groupActor; private GroupActor groupActor;
private ServiceActor serviceActor; private ServiceActor serviceActor;
private UserActor userActor;
public VkConnect(VkConfig vkConfig) { public VkConnect(VkConfig vkConfig) {
this.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() { public ServiceActor getServiceActor() {
if (serviceActor != null) { if (serviceActor != null) {
return serviceActor; return serviceActor;
@ -49,10 +64,10 @@ public class VkConnect {
} }
private void initGroupActor() { private void initGroupActor() {
groupActor = new GroupActor(vkConfig.getGroupId(), vkConfig.getGroupToken()); groupActor = new GroupActor(vkConfig.getConfigGroup().getGroupId(), vkConfig.getConfigGroup().getGroupToken());
} }
private void initServiceActor() { private void initServiceActor() {
serviceActor = new ServiceActor(vkConfig.getAppId(), vkConfig.getServiceToken()); serviceActor = new ServiceActor(vkConfig.getConfigGroup().getAppId(), vkConfig.getConfigGroup().getServiceToken());
} }
} }

View File

@ -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.Message;
import com.vk.api.sdk.objects.messages.MessageAttachment; 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.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.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -18,14 +18,16 @@ public class MessageMailConvert implements Convert<Message, Mail> {
public Mail converting(Message message) { public Mail converting(Message message) {
Mail mail = new Mail(); Mail mail = new Mail();
mail.setMessage(message.getText()); 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.setId(message.getId());
mail.setPersonId(message.getPeerId()); mail.setPersonId(message.getPeerId());
mail.setAttachments(message.getAttachments() mail.setAttachments(message.getAttachments()
.stream() .stream()
.map(this::convertAttachment) .map(this::convertAttachment)
.collect(Collectors.toList())); .collect(Collectors.toList()));
if (message.getGeo()!=null) {
mail.getAttachments().add(convertGeo(message.getGeo())); mail.getAttachments().add(convertGeo(message.getGeo()));
}
return mail; return mail;
} }

View File

@ -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<TopicComment, BoardComment> {
@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;
}
}

View File

@ -15,7 +15,6 @@ public class AccountSubscribe extends AbstractBasketSubscribe<Message, Message>
private final AccountService accountService; private final AccountService accountService;
private final Sent sent; private final Sent sent;
private BoxAnswer answerSuccessfulPayment; private BoxAnswer answerSuccessfulPayment;
private BoxAnswer answerFailPayment;
public AccountSubscribe(AccountService accountService, Sent sent) { public AccountSubscribe(AccountService accountService, Sent sent) {
this.accountService = accountService; this.accountService = accountService;
@ -30,14 +29,6 @@ public class AccountSubscribe extends AbstractBasketSubscribe<Message, Message>
this.answerSuccessfulPayment = answerSuccessfulPayment; this.answerSuccessfulPayment = answerSuccessfulPayment;
} }
public BoxAnswer getAnswerFailPayment() {
return answerFailPayment;
}
public void setAnswerFailPayment(BoxAnswer answerFailPayment) {
this.answerFailPayment = answerFailPayment;
}
@Override @Override
protected boolean check(Message userMessage) { protected boolean check(Message userMessage) {
return userMessage.getAttachments().size() > 0 && "Денежный перевод".equals(userMessage.getAttachments().get(0).getLink().getCaption()); return userMessage.getAttachments().size() > 0 && "Денежный перевод".equals(userMessage.getAttachments().get(0).getLink().getCaption());
@ -47,10 +38,12 @@ public class AccountSubscribe extends AbstractBasketSubscribe<Message, Message>
public void processing(Message message) { public void processing(Message message) {
if (message.getText() != null) { if (message.getText() != null) {
try { 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); sent.send(message.getPeerId(), answerSuccessfulPayment);
} }
} catch (PaymentException e) { } catch (PaymentException e) {
log.error(e.getMessage());
sent.send(message.getPeerId(), BoxAnswer.builder().message(e.getDescription()).build()); sent.send(message.getPeerId(), BoxAnswer.builder().message(e.getDescription()).build());
} }
} }

View File

@ -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<JsonObject, TopicComment> {
private final BoardCommentService boardCommentService;
private final Convert<TopicComment, BoardComment> topicConvert = new TopicCommentToBoardComment();
private Set<Integer> answerTopicsId;
private Set<Integer> 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<Integer> getAnswerTopicsId() {
return answerTopicsId;
}
public void setAnswerTopicsId(Set<Integer> answerTopicsId) {
this.answerTopicsId = answerTopicsId;
}
public Set<Integer> getNoAnswerPersonId() {
return noAnswerPersonId;
}
public void setNoAnswerPersonId(Set<Integer> 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));
}
}
}

View File

@ -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();
}
}
}

View File

@ -34,14 +34,14 @@ public class MailSenderVk implements Sent {
} }
@Override @Override
public void send(Integer idPerson, String message) { public void send(Integer personId, BoxAnswer boxAnswer) {
sendMessage(vkApiClient.messages().send(groupActor).peerId(idPerson).message(message).randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE))); MessagesSendQuery messagesSendQuery = createMessage(boxAnswer, personId);
sendMessage(messagesSendQuery);
} }
@Override @Override
public void send(Integer idPerson, BoxAnswer boxAnswer) { public void send(Integer integer, Integer integer1, BoxAnswer boxAnswer) {
MessagesSendQuery messagesSendQuery = createMessage(boxAnswer, idPerson);
sendMessage(messagesSendQuery);
} }
private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Integer peerId) { private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Integer peerId) {
@ -55,7 +55,7 @@ public class MailSenderVk implements Sent {
messages.keyboard(keyboard); messages.keyboard(keyboard);
} }
if (boxAnswer.getCoordinates() != null) { 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) { if (boxAnswer.getStickerId() != null) {
try { try {