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

[Bug] MessageEvent doesn`t support plain string in payload. (Probable fix provided) #160

Closed
bivashy opened this issue Apr 5, 2023 · 0 comments · Fixed by #161
Closed
Assignees
Labels
good first issue Good for newcomers
Milestone

Comments

@bivashy
Copy link
Owner

bivashy commented Apr 5, 2023

I'm executing this request (replace with URI from your logs): https://lp.vk.com/wh*******

with parameters (replace with params from your logs): wait=25&act=a_check&key=***&ts=19695

and I'm receiving (replace with response from your logs):

{"ts":"19696","updates":[{"group_id":203083412,"type":"message_event","event_id":"ae8e0ea6cca21fb263f871ca5aded4c59d91dc51","v":"5.131","object":{"user_id":205639000,"peer_id":205639000,"event_id":"15a1124fb169","payload":"\/принять Username","conversation_message_id":5187}}]}

Here is the code example (replace with your code example):

public class VkBotTest extends LongPollBot {
    private static final String GROUP_TOKEN = "TOKEN_HERE";

    public static void main(String[] args) throws VkApiException {
        new VkBotTest().startPolling();
    }

    @Override
    public void onMessageEvent(MessageEvent messageEvent) {
        System.out.println(messageEvent.getEventId());
        System.out.println(messageEvent.getConversationMessageId());
        System.out.println(messageEvent.getPayload());
    }

    @Override
    public String getAccessToken() {
        return GROUP_TOKEN;
    }
}
}

Stacktrace:

Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $
	at com.google.gson.Gson.fromJson(Gson.java:902)
	at com.google.gson.Gson.fromJson(Gson.java:852)
	at com.google.gson.Gson.fromJson(Gson.java:801)
	at com.google.gson.Gson.fromJson(Gson.java:773)
	at api.longpoll.bots.adapters.deserializers.PayloadDeserializer.deserialize(PayloadDeserializer.java:23)
	at api.longpoll.bots.adapters.deserializers.PayloadDeserializer.deserialize(PayloadDeserializer.java:14)
	at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:69)
	at com.google.gson.TypeAdapter$1.read(TypeAdapter.java:199)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
	at com.google.gson.Gson.fromJson(Gson.java:887)
	at com.google.gson.Gson.fromJson(Gson.java:952)
	at com.google.gson.internal.bind.TreeTypeAdapter$GsonContextImpl.deserialize(TreeTypeAdapter.java:162)
	at api.longpoll.bots.adapters.deserializers.UpdateDeserializer.deserialize(UpdateDeserializer.java:66)
	at api.longpoll.bots.adapters.deserializers.UpdateDeserializer.deserialize(UpdateDeserializer.java:47)
	at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:69)
	at com.google.gson.TypeAdapter$1.read(TypeAdapter.java:199)
	at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
	at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
	at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
	at com.google.gson.Gson.fromJson(Gson.java:887)
	at com.google.gson.Gson.fromJson(Gson.java:852)
	at com.google.gson.Gson.fromJson(Gson.java:801)
	at com.google.gson.Gson.fromJson(Gson.java:773)
	at api.longpoll.bots.methods.impl.VkMethod.execute(VkMethod.java:130)
	at api.longpoll.bots.LongPollBot.startPolling(LongPollBot.java:73)
	at com.ubivashka.java.test.VkBotTest.main(VkBotTest.java:14)
Caused by: com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $
	at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1559)
	at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:590)
	at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
	at com.google.gson.Gson.fromJson(Gson.java:883)
	... 28 more

I'm using java-vk-bots-longpoll-api version (replace with version you are using): 4.1.0

Could you please help me to understand what is wrong here?

My thoughts: This is issue with payload. I am sending its as an string. But api.longpoll.bots.adapters.deserializers.PayloadDeserializer tries to parse it as json. Yes, i can fix this issue with just changing payload. But i hope you`ll fix that

I think fixed version would be:

public class PayloadDeserializer implements JsonDeserializer<JsonElement> {
    /**
     * {@link Gson} object.
     */
    private static final Gson GSON = new Gson();

    @Override
    public JsonElement deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
        return jsonElement;
    }
}

Because GSON.fromJson(jsonElement.getAsString(), JsonElement.class) cannot process simple strings.

Good luck! And have a good day!

@yvasyliev yvasyliev self-assigned this Apr 5, 2023
@yvasyliev yvasyliev added the good first issue Good for newcomers label Apr 5, 2023
yvasyliev added a commit that referenced this issue Apr 5, 2023
@yvasyliev yvasyliev linked a pull request Apr 5, 2023 that will close this issue
yvasyliev added a commit that referenced this issue Apr 5, 2023
…pport-plain-string-in-payload-probable-fix-provided

Fixed #160
@yvasyliev yvasyliev added this to the 4.1.1 milestone Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants