Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram Bot, inline button

How to define that inline button is pressed and how to get callbackdata using pengrad/telegram-bot-api library? I have this code to send message with inline keyboard button

private void approveAdmin(User u){
    User admin = userService.findByUserRole("ROLE_ADMIN");
    SendMessage sm = new SendMessage(admin.getChatId(),
            "Do you approve user: "+u.getfName()+" "+u.getlName()+" as admin?");
    sm.replyMarkup(new InlineKeyboardMarkup(new InlineKeyboardButton[]
            {new InlineKeyboardButton("Approve user.").callbackData(u.getIdUser().toString())}));
    BOT.execute(sm);
}

but how to handle update from inline button?

like image 206
Dmitry Avatar asked Sep 04 '26 08:09

Dmitry


1 Answers

below snippet may helps you:

GetUpdatesResponse updatesResponse = bot.execute(new GetUpdates());
List<Update> updates = updatesResponse.updates();
for (Update update : updates) {
    CallbackQuery callbackQuery = update.callbackQuery();
    if (callbackQuery != null)  {
        //use the callbackQuery object peroperties to provide the appropriate response
    }
    //to make the update handler fully functional, make sure to check other types of messages
}
like image 126
tashakori Avatar answered Sep 05 '26 21:09

tashakori



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!