Telegram Bot Utilities – examples: InlineKeyboardMarkup

Telegram Bot Utilities – examples: InlineKeyboardMarkup

Here you can see an example of InlineKeyboardMarkup that send a callback query to your bot.
An InlineKeyboardButton can also aim to a link, or it can switch to an inline query.

Here is the code:

public static void main(String[] args) throws GettingUpdatesException, IOException {
    String yourToken = "<your-token>";
    String chat_id = "<your-chat-id>";
    TelegramBot b = new TelegramBot(yourToken);    
    InlineKeyboardButton[][] buttons = new InlineKeyboardButton[2][2];    
    buttons[0][0] = new InlineKeyboardButton("Top-Left", null, "top-left", null);
    buttons[0][1] = new InlineKeyboardButton("Top-Right", null, "top-right", null);
    buttons[1][0] = new InlineKeyboardButton("Bottom-Left", null, "bottom-left", null);
    buttons[1][1] = new InlineKeyboardButton("Bottom-Right", null, "bottom-right", null);
    InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup(buttons);
    b.sendMessage(chat_id, "<b>InlineKeyboardMarkup test</b>", "html", false, false, 0, inlineKeyboardMarkup);
    /* Here you can handle the answer, in this case I gave 8 seconds to answer
     * But it can be done also in a loop.</ul>
     */
    try {
 	Thread.sleep(8000);
        Update[] u = b.getUpdates(0, 0, 0);
        Update up = u[u.length - 1];
        if (up.getCallbackQuery() != null) {
            b.sendMessage(chat_id, up.getCallbackQuery().getData());
        }
    } catch (EmptyUpdatesException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}


Here you can see the result:

InlineKeyboardMarkup example: callback query
Inline Keyboard Markup example: callback query

Like I already said into the code, the answer can be handled in different ways, the one I used into this example is not the best, it was only a fast solution to show you a simple example of callback query.
For example it could be part of a loop, that checks if the callback query has a different date from the last query received, and then it sends its answer, or handles the query received.

Previous Entries Telegram Bot Utilities - examples: Inline Query Next Entries Motion detection bot that sends images via Telegram in only 45 lines of code in java! - Part 1

8 thoughts on “Telegram Bot Utilities – examples: InlineKeyboardMarkup

  1. Hello. Good example, I try to add inline mode to my bot, but I don’t have this method like in your example.
    Can you tell me what library do you use for your bot. Here is my code:
    public void sending(Long chatID, File photo) {
    InlineKeyboardMarkup keyboardMarkup = new InlineKeyboardMarkup();
    InlineKeyboardButton button = new InlineKeyboardButton();

    List<List> buttons = new ArrayList();
    List 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();
    }
    }
    I would be really appreciated for any answer:)

  2. Abilfazl on said:

    Hi
    For example I have 2 buttons “yes” & “no”
    When user touch yes I want to send other inline keyboard
    Do u know how to do that ?

  3. Hi,
    For some reason I can’t send a query with an un-equal number if rows and coloumns.
    In addition I wasn’t able to send a long links, for instance:

    “https://telegram.me/share/url?url=%D7%94%D7%99%D7%99%F0%9F%98%8E%0A%0A%D7%A8%D7%A6%D7%99%D7%AA%D7%99%20%D7%9C%D7%A9%D7%AA%D7%A3%20%D7%90%D7%95%D7%AA%D7%9A%20%D7%91%D7%A2%D7%A8%D7%95%D7%A5%20%D7%9E%D7%92%D7%A0%D7%99%D7%91%20%D7%A9%D7%9E%D7%A6%D7%90%D7%AA%D7%99%20%D7%91%D7%98%D7%9C%D7%92%D7%A8%D7%9D.%F0%9F%98%98%0A%D7%99%D7%A9%20%D7%91%D7%95%20%D7%9E%D7%9C%D7%90%20%D7%A7%D7%99%D7%A9%D7%95%D7%A8%D7%99%D7%9D%20%D7%9C%D7%9E%D7%95%D7%A6%D7%A8%D7%99%D7%9D%20%D7%90%D7%99%D7%9B%D7%95%D7%AA%D7%99%D7%99%D7%9D%20%D7%9E%D7%97%D7%95%22%D7%9C%21%F0%9F%91%87%0A%0Ahttps%3A%2F%2Ft.me%2FIsrael”
    Is there any possible solution?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.