rubenlagus / TelegramBots

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

sendMessage error #151

Closed alevdyk closed 7 years ago

alevdyk commented 7 years ago

Hi. I have an error in this piece of code in case when type.equals('s') in other situations everything works fine. the problem is the error itself say nothing about what is wrong, I've spent like two hours already and nothing changes. In case type=t it sends the message, in case type=s - no.

String text = "You user-type has been modified.";       
if (type.equals('s')) {
                text = text + "Use /set GROUP_ID to set your group id. \n📚For example: /set CE-I1NP";
                sendTextOnly(message,text);
            } 
if (type.equals('t')) {
                text = text + "Use /set NAME SURNAME to set your personal data. \n📚For example /set Walesa Lech";
                sendTextOnly(message,text);
            }

here is the sendTextOnly method

private void sendTextOnly(Message message, String text) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.enableMarkdown(true);
        sendMessage.setChatId(message.getChatId().toString());
        sendMessage.setText(text);
        sendMessage.setReplayMarkup(new ReplyKeyboardHide());
        try {
            sendMessage(sendMessage);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }

and the stack trace

org.telegram.telegrambots.TelegramApiException: Error at sendmessage
    at org.telegram.telegrambots.bots.AbsSender.sendApiMethod(AbsSender.java:435)
    at org.telegram.telegrambots.bots.AbsSender.sendMessage(AbsSender.java:51)
    at test.VistulaBot.sendTextOnly(VistulaBot.java:274)
    at test.VistulaBot.setType(VistulaBot.java:353)
    at test.VistulaBot.handleMesssage(VistulaBot.java:95)
    at test.VistulaBot.onUpdateReceived(VistulaBot.java:61)
    at org.telegram.telegrambots.updatesreceivers.UpdatesThread$HandlerThread.run(UpdatesThread.java:124)

!!!!!!!!!SOLVED!!!!!!!!!!!! finally after 2 hours of painful runs and debugging I find out that _ (underscore) symbol in message text was a problem. kind of stupid limitation, but it works.

rubenlagus commented 7 years ago

Not stupid limitation at all, if you enable MarkDown, you need to escape any _ that should not start a new entity (and you could have use getApiResponse method to get the explanation why it failed.

Closing as it seems to be fixed.