Telegram Bot Utilities – examples: Inline Query

Telegram Bot Utilities – examples: Inline Query

In this article you can see an example of inline query that returns two images when you send a query equal to “photo”.

public static void main(String[] args) {
    TelegramBot b = new TelegramBot("<your-token>");
    Update u, ar[];
    while (true) {
        try {
            int updateId = b.getCurrentUpdateId();
            if (updateId < 0) {
                ar = b.getUpdates(updateId, 0, 0);
                u = ar[ar.length - 1];
                if (u.getQuery() != null) {
                    System.out.println(u.getQuery().getQuery());
                    if (u.getQuery().getQuery().compareTo("photo") == 0) {
                        InlineQueryResultPhoto[] p = new InlineQueryResultPhoto[2];
                        p[0] = new InlineQueryResultPhoto("0", "urlToYourImage", "urlToYourThumbnail");
                        p[1] = new InlineQueryResultPhoto("1", "urlToYourImage", "urlToYourThumbnail");
                        b.answerInlineQuery(u.getQuery().getId(), p);
                    }
                }
            }
        } catch (EmptyUpdatesException e) {
            e.printStackTrace();
        } catch (GettingUpdatesException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here you can see the result

Telegram Bot utilities: inline query example
Inline query example
Previous Entries Telegram Bot Utilities - examples: Parsing text Next Entries Telegram Bot Utilities - examples: InlineKeyboardMarkup

Leave a Reply

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