Skip to content

Commit

Permalink
Merge pull request #131 from yvasyliev/dev
Browse files Browse the repository at this point in the history
Fixed #130
  • Loading branch information
yvasyliev committed Sep 19, 2022
2 parents 60d6669 + 2888802 commit 8bb70c7
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This library uses the next third-party dependencies:
<dependency>
<groupId>com.github.yvasyliev</groupId>
<artifactId>java-vk-bots-longpoll-api</artifactId>
<version>3.4.6</version>
<version>3.4.7</version>
</dependency>
```
4. Extend `LongPollBot` class and override necessary methods:
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.github.yvasyliev</groupId>
<artifactId>java-vk-bots-longpoll-api</artifactId>
<packaging>jar</packaging>
<version>3.4.6</version>
<version>3.4.7</version>
<name>Java VK Bots Long Poll API</name>
<description>A Java library to create VK bots using Bots Long Poll API</description>
<url>https://github.com/yvasyliev/java-vk-bots-long-poll-api</url>
Expand Down Expand Up @@ -39,7 +39,7 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.18.0</version>
<version>2.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/api/longpoll/bots/VkBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import api.longpoll.bots.model.events.other.VkpayTransaction;
import api.longpoll.bots.model.events.photos.PhotoComment;
import api.longpoll.bots.model.events.photos.PhotoCommentDelete;
import api.longpoll.bots.model.events.poll.PollVoteNew;
import api.longpoll.bots.model.events.users.GroupJoin;
import api.longpoll.bots.model.events.users.GroupLeave;
import api.longpoll.bots.model.events.users.UserBlock;
Expand Down Expand Up @@ -239,6 +240,10 @@ public void handle(List<Update> updates) {
onMessageDeny((MessageDeny) update.getObject());
break;

case POLL_VOTE_NEW:
onPollVoteNew((PollVoteNew) update.getObject());
break;

default:
LOGGER.warn("No update handler found update updateType: {}", updateType);
break;
Expand Down Expand Up @@ -597,4 +602,12 @@ public void onMessageAllow(MessageAllow messageAllow) {
*/
public void onMessageDeny(MessageDeny messageDeny) {
}

/**
* Handles <b>poll_vote_new</b> events.
*
* @param pollVoteNew event object.
*/
public void onPollVoteNew(PollVoteNew pollVoteNew) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import api.longpoll.bots.model.events.other.VkpayTransaction;
import api.longpoll.bots.model.events.photos.PhotoComment;
import api.longpoll.bots.model.events.photos.PhotoCommentDelete;
import api.longpoll.bots.model.events.poll.PollVoteNew;
import api.longpoll.bots.model.events.users.GroupJoin;
import api.longpoll.bots.model.events.users.GroupLeave;
import api.longpoll.bots.model.events.users.UserBlock;
Expand All @@ -31,6 +32,7 @@
import api.longpoll.bots.model.objects.media.Audio;
import api.longpoll.bots.model.objects.media.Photo;
import api.longpoll.bots.model.objects.media.Video;
import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
Expand All @@ -43,12 +45,19 @@
* Deserializes JSON objects to {@link Update}.
*/
public class UpdateDeserializer implements JsonDeserializer<Update> {
private final Gson gson = new Gson();

@Override
public final Update deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonUpdate = jsonElement.getAsJsonObject();

Update update = new Update();
update.setType(context.deserialize(jsonUpdate.get("type"), Update.Type.class));

if (update.getType() == null) {
throw new IllegalArgumentException("Cannot deserialize '" + jsonUpdate.get("type") + "'. JSON: " + gson.toJson(jsonElement));
}

update.setGroupId(jsonUpdate.get("group_id").getAsInt());
update.setEventId(jsonUpdate.get("event_id").getAsString());
update.setObject(context.deserialize(jsonUpdate.get("object"), getObjectClass(update.getType())));
Expand Down Expand Up @@ -131,6 +140,9 @@ private Class<? extends Update.Object> getObjectClass(Update.Type type) {
case PHOTO_NEW:
return Photo.class;

case POLL_VOTE_NEW:
return PollVoteNew.class;

case USER_BLOCK:
return UserBlock.class;

Expand Down Expand Up @@ -166,7 +178,7 @@ private Class<? extends Update.Object> getObjectClass(Update.Type type) {
return VkpayTransaction.class;

default:
throw new RuntimeException("Update type '" + type + "' is not handled.");
throw new IllegalArgumentException("Update type '" + type + "' is not handled.");
}
}
}
1 change: 1 addition & 0 deletions src/main/java/api/longpoll/bots/model/events/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public enum Type {
@SerializedName("photo_comment_new") PHOTO_COMMENT_NEW,
@SerializedName("photo_comment_restore") PHOTO_COMMENT_RESTORE,
@SerializedName("photo_new") PHOTO_NEW,
@SerializedName("poll_vote_new") POLL_VOTE_NEW,
@SerializedName("user_block") USER_BLOCK,
@SerializedName("user_unblock") USER_UNBLOCK,
@SerializedName("video_comment_delete") VIDEO_COMMENT_DELETE,
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/api/longpoll/bots/model/events/poll/PollVoteNew.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package api.longpoll.bots.model.events.poll;

import api.longpoll.bots.model.events.Update;
import com.google.gson.annotations.SerializedName;

/**
* A new vote in a public poll.
*/
public class PollVoteNew implements Update.Object {
/**
* Poll owner ID.
*/
@SerializedName("owner_id")
private Integer ownerId;

/**
* Poll ID.
*/
@SerializedName("poll_id")
private Integer pollId;

/**
* Poll option ID.
*/
@SerializedName("option_id")
private Integer optionId;

/**
* User ID.
*/
@SerializedName("user_id")
private Integer userId;

public Integer getOwnerId() {
return ownerId;
}

public void setOwnerId(Integer ownerId) {
this.ownerId = ownerId;
}

public Integer getPollId() {
return pollId;
}

public void setPollId(Integer pollId) {
this.pollId = pollId;
}

public Integer getOptionId() {
return optionId;
}

public void setOptionId(Integer optionId) {
this.optionId = optionId;
}

public Integer getUserId() {
return userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

@Override
public String toString() {
return "PollVoteNew{" +
"ownerId=" + ownerId +
", pollId=" + pollId +
", optionId=" + optionId +
", userId=" + userId +
'}';
}
}

0 comments on commit 8bb70c7

Please sign in to comment.