rubenlagus / TelegramBots

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

Handle getCallbackQuery on TelegramBot #1077

Closed samreachyan closed 2 years ago

samreachyan commented 2 years ago

Currently I faced issued on handling callBackQuery on TelegramBot which I cannot catch event and reply the message as I want. You can comment my codes and help share me document solution resources.

public static void main(String[] args){
        try {
            TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
            telegramBotsApi.registerBot(new Bot());

        } catch (TelegramApiException e) {
            throw new RuntimeException(e);
        }
    }

and Bot.java

public class Bot extends TelegramLongPollingBot {
    @Override
    public void onUpdateReceived(Update update) {
        System.out.println(update.getMessage().getText());

        if(update.hasMessage()){
            if (update.getMessage().hasText()) {
                if (update.getMessage().getText().equals("Hello")){
                    try {
                        execute(sendInlineKeyBoardMessage(update.getMessage().getChatId()));
                    } catch (TelegramApiException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else if (update.hasCallbackQuery()) {
            try {
                SendMessage sendMessage = new SendMessage();
                sendMessage.setText("You click and return : " + update.getCallbackQuery().getData());
                sendMessage.setChatId(String.valueOf(update.getCallbackQuery().getMessage().getChatId()));
                execute(sendMessage);
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String getBotUsername() {
        return "bot";
    }

    @Override
    public String getBotToken() {
        return "1234568...";
    }

    public static SendMessage sendInlineKeyBoardMessage(long chatId){
        InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
        InlineKeyboardButton inlineKeyboardButton1 = new InlineKeyboardButton();
        inlineKeyboardButton1.setText("Report Sale");
        inlineKeyboardButton1.setCallbackData("sale");
        List<InlineKeyboardButton> keyboardButtonsRow1 = new ArrayList<>();
        keyboardButtonsRow1.add(inlineKeyboardButton1);
        List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
        rowList.add(keyboardButtonsRow1);
        inlineKeyboardMarkup.setKeyboard(rowList);
        SendMessage sendMessage = new SendMessage();
        sendMessage.setChatId(String.valueOf(chatId));
        sendMessage.setText("Show button sale: ");
        sendMessage.setReplyMarkup(inlineKeyboardMarkup);
        return sendMessage;
    }
}

I want to welcome by Message and Show option inlineKeyboardButton. Then user click on the Button, we will handle clicked button and reply with a message or a photo.

I already read this. https://github.com/rubenlagus/TelegramBots/wiki/FAQ#how_to_use_custom_keyboards

But I cannot handle when they click on that's button by Getting getCallbackQuery.data() to compare as command to send new message or send a photo.

I hope someone can guide me to solve that issue. Thanks you.

samreachyan commented 2 years ago

I know why it's error exception. and if (update.hasCallbackquery cannot handle because, I have println which call method System.out.println(update.getMessage().getText()); is null out of IF Condition. So it cannot return value to display. So it's error