bivashy / java-vk-bots-long-poll-api

A Java library to create VK bots using Bots Long Poll API
MIT License
4 stars 1 forks source link

Some problems with encoding. #98

Closed l4npro closed 2 years ago

l4npro commented 2 years ago

Got an issue with encoding after updating from 2.1.7 to 3.2.9.

This message is "меню", but the result is (first line):

image

yvasyliev commented 2 years ago

Hi @l4npro,

I need some extra info from you to solve the issue:

  1. Could you please share a code example that you are running?
  2. What encoding is set in your console?
l4npro commented 2 years ago
  1. I hope this method is suitable. image There weren't any problems with encoding before updating, so I think that's not my code's fault.

  2. UTF-8. Btw i tried to switch through some encodings, it worked only for russian words directly in code, not for text from VK messages.

yvasyliev commented 2 years ago

Try to set UTF-8 encoding right before running your app:

chcp 65001
java -jar your-jar-file.jar

Is result the same?

l4npro commented 2 years ago

image

Ok, It worked in console. But there is other problem: bot still don't understand this commands, cos it's getting wrong encoded message. How can I fix it?

l4npro commented 2 years ago

For example: I have a text button named "Подтвердить" image

So, when I'm pressing this button, I send message: image

Bot getting it: image

And returns this: image

yvasyliev commented 2 years ago

Try to set UTF-8 encoding in your pom.xml and rebuild your app:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

Is result the same?

l4npro commented 2 years ago

Yep, the same.

yvasyliev commented 2 years ago

Please try this workaround:

byte[] encoded = message.getText().getBytes(StandardCharsets.UTF_8);
String decoded = new String(encoded, StandardCharsets.UTF_8);

Is it working?

l4npro commented 2 years ago

image

It's a waste of time. Maybe I should try to downgrade through versions of your API to find where this problem occurs?

yvasyliev commented 2 years ago

Please try this command:

chcp 65001
java -Dfile.encoding=UTF-8 -jar your-jar-file.jar

I will be grateful if you let me know the result. :worried:

l4npro commented 2 years ago

Thanks, it worked!

Hm, so, is this is nessesary option from now on, am I right?

yvasyliev commented 2 years ago

I see that HTTP response is read without explicit encoding specified. So the actual encoding depends on what Charset.defaultCharset() retuns in runtime. Most probably I should specify UTF-8 explicitily to InputStreamReader.

Will investigate and update further in next 1-2 days.

So please use -Dfile.encoding=UTF-8 option as workaround for now.

l4npro commented 2 years ago

Okay, thanks a lot! I'll check for your updates!

yvasyliev commented 2 years ago

UTF-8 encoding was explicitily set to InputStreamReader while reading HTTP response.

Fixed in version 3.2.10.