1

I try to create the telegram bot in Java, and I need to add inline buttons in there. I looked at the official documentation official documentation and everything I made - the button, but they don't work. I also check all GitHub Java examples, but it didn't help. I don't understand how I can obtain the answer of pressing to this button and do something with. For example, If the user presses this button - send him something. Here is my code:

public void sending(Long chatID, File photo) {
    InlineKeyboardMarkup keyboardMarkup = new InlineKeyboardMarkup();
    InlineKeyboardButton button = new InlineKeyboardButton();

    List<List<InlineKeyboardButton>> buttons = new ArrayList<>();
    List<InlineKeyboardButton> buttonList = new ArrayList<>();
    button.setText("Just button");
    button.setCallbackData("callBack");
    buttonList.add(button);
    buttons.add(buttonList);
    keyboardMarkup.setKeyboard(buttons);

    SendPhoto sending = new SendPhoto();
    sending.setChatId(chatID.toString());
    sending.setNewPhoto(photo);
    sending.setReplyMarkup(keyboardMarkup);
    try {
        sendPhoto(sending);
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }
}

Now I have only this error PM org.telegram.telegrambots.logging.BotLogger severe SEVERE: BOTSESSION java.lang.NullPointerException at mybot.SimpleBot.onUpdateReceived(SimpleBot.java:62) at org.telegram.telegrambots.updatesreceivers.BotSession$HandlerThread.run(BotSession.java:197)

I would be really appreciated if somebody gives me some tips or simple java code with using callback buttons.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • @kayaman How is this question a duplicate of http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ? – Charles Okwuagwu Nov 21 '16 at 22:12
  • @CharlesOkwuagwu He edited out the "Now I have only this error ``" part. – Kayaman Nov 22 '16 at 06:45
  • Rolled this back to the original version because otherwise it doesn't make sense why it was closed as a duplicate. Also, the original version has the most information about what was going wrong. – Kayaman Nov 22 '16 at 06:56
  • I was also surprised how that post is similar. Thanks for rolled back! – Artem Kamko Nov 22 '16 at 13:41
  • By the way, I received the callback data, it needs to do in another override method - `public void onUpdateReceived(Update update)`. Just check the press off button `callbackQuery = update.getCallbackQuery(); if (callbackQuery.getData().equals("callBack")){//do thomething}`. Now I have another question - how I can use the EditMessageText - if the button were pressed it would disappear? Any suggestion? – Artem Kamko Nov 22 '16 at 13:45
  • If it would be helpful for somebody this is the answer to my last question: `editMessageReplyMarkup.setChatId(callbackQuery.getMessage().getChatId().toString()) .setMessageId(callbackQuery.getMessage().getMessageId()).setReplyMarkup(new InlineKeyboardMarkup()); try { editMessageReplyMarkup(editMessageReplyMarkup); } catch (TelegramApiException e) { e.printStackTrace(); }` – Artem Kamko Nov 22 '16 at 17:09
  • @HerbertVincent You can edit your question instead of trying to put the code in the comments. As you see it isn't very readable. – Kayaman Nov 23 '16 at 14:43
  • Sorry for this, I'm new at StackOverflow. – Artem Kamko Nov 24 '16 at 08:51

0 Answers0