rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.78k stars 1.22k forks source link

Exception on sending message with underline character #121

Closed azimot closed 8 years ago

azimot commented 8 years ago

i catch exception when i want to sending underline '_' to bot

org.telegram.telegrambots.TelegramApiException: Error at sendmessage: 
Bad Request: Can't parse message text: Can't find end of the entity starting at byte offset 264

i find that happen when i had underline character in my message text it's seems underline is a special character in bot API like as '\' in java String that must be use '\' for that how can i to resolve ?

rubenlagus commented 8 years ago

Same as in java, what about escaping it too with '\'? Another option is that you don't use Markdown parse mode.

azimot commented 8 years ago

with the '\' not worked but after i used to set SendMessage.enableHtml(true) worked !

rubenlagus commented 8 years ago

I'm using '\' to escape character in the messages and works correctly. Maybe you are escaping too much or something. enabling html markdown just change the type of markdown.

azimot commented 8 years ago

yes it's worked ! i check in code and see i had a mistake by myself in end of SendMessage operation

Clevero commented 8 years ago

What was exactly your error?

Currently, my problem is that I want to list all my users. With their First, Last and username. It seems that one user has special characters in this name that is interpreted as a markdown thing.

Since I get the same error message, I wonder If I do the same error as you did.

azimot commented 8 years ago

hmm in username phrase ! i dont think about it yet !! but i did catch that error when i want to send message to users because some message must be have some special characters and other dont need.

so in your case you could to create a Pattern Object and check username phrase with Regex if it's have markdown characters then enable markdown and if have html characters enable html i think in this way you could to resolve your problem

hems commented 8 years ago

For me scaping _ with \ isn't working!

KilluaFein commented 7 years ago

I also have problem like @hems, can't escape _ with .

hems commented 7 years ago

@KilluaFein if i'm not mistaken i had to scape with \\ instead! like \\_

Fotomultman commented 7 years ago

Yes, the problem is not solved yet

Clevero commented 7 years ago

@Fotomultman do you escaped properly?

Fotomultman commented 7 years ago

@Clevero I tried but no result :( I'm wondering, could you tell me please how to do it right?

Clevero commented 7 years ago

//should fail
String text = "My amazing text _ is really awesome";
sendMessage.setText(text);

//escape the character
String text = "My amazing text \\_ is really awesome";
sendMessage.setText(text);

Some explanations in a Java tutorial at Oracle

Fotomultman commented 7 years ago

@Clevero As you said before

//escape the character
String text = "My amazing text \\_ is really awesome";
sendMessage.setText(text);

works fine, but when I try to execute

sndMessage(message,message.getSticker().getFileId().replaceAll("_","\\_")) Ther is the same error... :( It it suitable to use replaceAll("_","\\_") construction in this way?

Sorry if it is a silly question :)

P.S. BTW

System.out.println( message.getSticker().getFileId().replaceAll("_","\\_"));

shows result likeCAADAgADNgADGXMNCX5t7t_uKaEZAg without \ before _

But Oracle manual says it would work ...

Clevero commented 7 years ago

Have tested similar code to yours, cannot reproduce your issue:

String text = "My amazing text is _ awesome";

sndMessage(message,text.replaceAll("_","\\_"));

Whereas sndMessage() is a method that creates a SendMessage object and fire its up with execute()

Fotomultman commented 7 years ago

@Clevero

BTW

String mysticker;
String mysticker_txt;
mysticker_txt = message.getSticker().getFileId().replaceAll("_","\\\\_");
sndMessage(message,mysticker_txt);

It works with "\\\\_"mask... dont know why