Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling unknown events #175

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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>4.1.4</version>
<version>4.1.5</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
12 changes: 12 additions & 0 deletions src/main/java/api/longpoll/bots/VkBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ public void handle(List<Update> updates) {
onPollVoteNew((PollVoteNew) update.getObject());
break;

case UNKNOWN:
onUnknownObject((Update.UnknownObject) update.getObject());
break;

default:
LOGGER.warn("No update handler found update updateType: {}", updateType);
break;
Expand Down Expand Up @@ -610,4 +614,12 @@ public void onMessageDeny(MessageDeny messageDeny) {
*/
public void onPollVoteNew(PollVoteNew pollVoteNew) {
}

/**
* Handles all unknown events.
*
* @param unknownObject unknown event.
*/
public void onUnknownObject(Update.UnknownObject unknownObject) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
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 @@ -45,11 +44,6 @@
* Deserializes JSON objects to {@link Update}.
*/
public class UpdateDeserializer implements JsonDeserializer<Update> {
/**
* {@link Gson} object.
*/
private final Gson gson = new Gson();

@Override
public final Update deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonUpdate = jsonElement.getAsJsonObject();
Expand All @@ -58,12 +52,16 @@ public final Update deserialize(JsonElement jsonElement, Type type, JsonDeserial
update.setType(context.deserialize(jsonUpdate.get("type"), Update.Type.class));

if (update.getType() == null) {
throw new IllegalArgumentException("There is no mapping for event '" + jsonUpdate.get("type") + "'. JSON: " + gson.toJson(jsonElement));
update.setType(Update.Type.UNKNOWN);
Update.UnknownObject unknownObject = new Update.UnknownObject();
unknownObject.setData(jsonUpdate.get("object"));
update.setObject(unknownObject);
} else {
update.setObject(context.deserialize(jsonUpdate.get("object"), getObjectClass(update.getType())));
}

update.setGroupId(jsonUpdate.get("group_id").getAsInt());
update.setEventId(jsonUpdate.get("event_id").getAsString());
update.setObject(context.deserialize(jsonUpdate.get("object"), getObjectClass(update.getType())));
return update;
}

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/api/longpoll/bots/model/events/Update.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api.longpoll.bots.model.events;

import api.longpoll.bots.adapters.deserializers.UpdateDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

Expand Down Expand Up @@ -112,6 +113,7 @@ public enum Type {
@SerializedName("photo_comment_restore") PHOTO_COMMENT_RESTORE,
@SerializedName("photo_new") PHOTO_NEW,
@SerializedName("poll_vote_new") POLL_VOTE_NEW,
UNKNOWN,
@SerializedName("user_block") USER_BLOCK,
@SerializedName("user_unblock") USER_UNBLOCK,
@SerializedName("video_comment_delete") VIDEO_COMMENT_DELETE,
Expand All @@ -133,4 +135,29 @@ public enum Type {
*/
public interface Object {
}

/**
* Default object for all unknown events.
*/
public static class UnknownObject implements Object {
/**
* Object data.
*/
private JsonElement data;

public JsonElement getData() {
return data;
}

public void setData(JsonElement data) {
this.data = data;
}

@Override
public String toString() {
return "UnknownObject{" +
"data=" + data +
'}';
}
}
}
Loading