diff --git a/pom.xml b/pom.xml index 8e694c4e..b96e6875 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.yvasyliev java-vk-bots-longpoll-api jar - 4.1.4 + 4.1.5 Java VK Bots Long Poll API A Java library to create VK bots using Bots Long Poll API https://github.com/yvasyliev/java-vk-bots-long-poll-api diff --git a/src/main/java/api/longpoll/bots/VkBot.java b/src/main/java/api/longpoll/bots/VkBot.java index 1319df23..08671e68 100644 --- a/src/main/java/api/longpoll/bots/VkBot.java +++ b/src/main/java/api/longpoll/bots/VkBot.java @@ -244,6 +244,10 @@ public void handle(List 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; @@ -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) { + } } diff --git a/src/main/java/api/longpoll/bots/adapters/deserializers/UpdateDeserializer.java b/src/main/java/api/longpoll/bots/adapters/deserializers/UpdateDeserializer.java index 90323d76..2a36c499 100644 --- a/src/main/java/api/longpoll/bots/adapters/deserializers/UpdateDeserializer.java +++ b/src/main/java/api/longpoll/bots/adapters/deserializers/UpdateDeserializer.java @@ -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; @@ -45,11 +44,6 @@ * Deserializes JSON objects to {@link Update}. */ public class UpdateDeserializer implements JsonDeserializer { - /** - * {@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(); @@ -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; } diff --git a/src/main/java/api/longpoll/bots/model/events/Update.java b/src/main/java/api/longpoll/bots/model/events/Update.java index aa58f9ee..982b48cf 100644 --- a/src/main/java/api/longpoll/bots/model/events/Update.java +++ b/src/main/java/api/longpoll/bots/model/events/Update.java @@ -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; @@ -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, @@ -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 + + '}'; + } + } }