pengrad / java-telegram-bot-api

Telegram Bot API for Java
https://core.telegram.org/bots
Apache License 2.0
1.81k stars 374 forks source link

Not receiving callbacks & trouble editing inline keyboard #192

Closed aekramer closed 4 years ago

aekramer commented 4 years ago

In the past I have used a similar library for making python tg bots with menus, but due to the nature of my program I wanted to switch over to java.

But unfortunately I am having some issues getting callback data back, as well as editing inline keyboard, I setup the general updates listener

bot.setUpdatesListener(new UpdatesListener() { @Override public int process(List<Update> updates) { for (Update u : updates) { System.out.println("Update for "+u.message().chat().username()); if (upd.callbackQuery() != null && upd.callbackQuery().data() != null) System.out.println("Callback check"); System.out.println("Data: "+upd.callbackQuery().data()); } } } });

And build the keyboard like this: private final InlineKeyboardMarkup mainMenu = new InlineKeyboardMarkup( new InlineKeyboardButton[] { new InlineKeyboardButton("Status").callbackData("cb_status"), }, new InlineKeyboardButton[] { new InlineKeyboardButton("First").callbackData("cb_first"), new InlineKeyboardButton("Last").callbackData("cb_last") }, new InlineKeyboardButton[] { new InlineKeyboardButton("Toggle alerts").switchInlineQuery("cb_alert") } );

And send it like this:

SendResponse response = bot.execute( new SendMessage(chatId, "My bot:") .replyToMessageId(msg.messageId()) .replyMarkup(mainMenu));

chat.msg = response.message();

if (!response.isOk()) getLogger().log(Level.WARNING, "[TG -> "+chatId+"] RESPONSE NOT OK: "+response.description());

I have been stuck on this for quite some time now, and hopefully someone can get me out of this pickle, thanks

pengrad commented 4 years ago

I think you agree that the code you have shared is unreadable. Please format it properly so at least I can understand what happens there

aekramer commented 4 years ago

I think you agree that the code you have shared is unreadable. Please format it properly so at least I can understand what happens there

Sorry, I do not understand why this is happening. When editing the message it appears alright, if its fine with you I will upload it to a pastebin instead? I don't remember having issues sharing code on github before, the code tags seem broken or something, because whitespace is just completely removed

EDIT: This is far too strange, the markup seems perfectly fine but when hitting update it ignores it completely. It even wont let me do a 3rd code tag (which is why I attempted a quote instead) I reuploaded the snippets to pastebin: https://pastebin.com/5xVZW0pB

pengrad commented 4 years ago

Your bot.setUpdatesListener can't even compile coz there is no return from process() method, but maybe your real code is different.

Other problem can be here System.out.println("Update for "+u.message().chat().username()), this can throw NullPointerException coz message can be null.

Sending inline seems fine, so no idea why it doesn't work.

Check logs to find what happens, there should be errors I guess Also build your bot with debug mode to have more verbose logs bot = new TelegramBot.Builder(token()).debug().build();

And take a look at my test code, it has coverage for callback data and inline modes https://github.com/pengrad/java-telegram-bot-api/blob/master/library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java