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

Help, error when sending message #549

Closed guccilenses closed 5 years ago

guccilenses commented 5 years ago

java.lang.NullPointerException at Bot.sendMsg(Bot.java:50) at Bot.onUpdateReceived(Bot.java:41) at java.util.ArrayList.forEach(Unknown Source) at org.telegram.telegrambots.meta.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27) at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:305)

My Code: `
@Override public void onUpdateReceived(Update update) { Message message = update.getMessage(); if(message != null && message.hasText()) { if(message.getText().equals("/help")) { sendMsg(message, "Список доступных команд:\n /help - список команд\n /info - информация о боте. \n "); } if(message.getText().equals("/random")) { sendMsg(null, "я думаю это " + message.getFrom().toString()); }

    }
    }

private void sendMsg(Message message, String s) {
    SendMessage sendMessage = new SendMessage();
    sendMessage.enableMarkdown(true);
    sendMessage.setChatId(message.getChatId().toString());
    sendMessage.setReplyToMessageId(message.getMessageId());
    sendMessage.setText(s);
    try {
        execute(sendMessage);
    } catch (TelegramApiException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

` When i'm send /help all works, when /random nothing works.

aNNiMON commented 5 years ago

@guccilenses here's your problem: sendMsg(null, ...) just replace null with message

guccilenses commented 5 years ago

message

org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException: Error sending message: [400] Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 114 at org.telegram.telegrambots.meta.api.methods.send.SendMessage.deserializeResponse(SendMessage.java:170) at org.telegram.telegrambots.meta.api.methods.send.SendMessage.deserializeResponse(SendMessage.java:24) at org.telegram.telegrambots.bots.DefaultAbsSender.sendApiMethod(DefaultAbsSender.java:717) at org.telegram.telegrambots.meta.bots.AbsSender.execute(AbsSender.java:47) at Bot.sendMsg(Bot.java:51) at Bot.onUpdateReceived(Bot.java:38) at java.util.ArrayList.forEach(Unknown Source) at org.telegram.telegrambots.meta.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27) at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:305)

doesn't fix my problem

aNNiMON commented 5 years ago

This is another error. [400] Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 114 It says that telegram server can't parse your html or markdown message, for example, when you forgot to close the tag. Try to temporary disable markdown or just escape _, * and ` symbols:

sendMessage.setText(s.replaceAll("([_*`])", "\\\\$1"));
Clevero commented 5 years ago

Was your problem solved @guccilenses ? If yes, please close the issue. Thanks!

rubenlagus commented 5 years ago

Closing due to inactivity