Inline Queries
With inline queries, users can search for, browse, and send content suggested by your bot in any chat, even if it is not a member there. To do this, they start a message with @your
and choose one of the results.
Enable Inline Mode
By default, support for inline mode is disabled. You must contact @Bot
Revisit the Inline mode section in the Introduction for Developers written by the Telegram team. Further resources are their detailed description of inline bots, as well as the original blog post announcing the feature, and the Inline mode section in the Telegram Bot API Reference. They are all worth a read before implementing inline queries for your bot.
Once a user triggers an inline query, i.e. starts a message by typing “@your_bot_name …” in the text input field, your bot will receive updates about this. grammY has special support for handling inline queries via the bot
method, as documented on the Composer
class in the grammY API Reference. It allows you to listen for specific inline queries that match strings or regular expressions. If you want to handle all inline queries generically, use bot
.
// Shameless self-advertising in one project's documentation
// is the best kind of advertising.
bot.inlineQuery(/best bot (framework|library)/, async (ctx) => {
await ctx.answerInlineQuery(
[
{
type: "article",
id: "grammy-website",
title: "grammY",
input_message_content: {
message_text:
"<b>grammY</b> is the best way to create your own Telegram bots. \
They even have a pretty website! 👇",
parse_mode: "HTML",
},
reply_markup: new InlineKeyboard().url(
"grammY website",
"https://grammy.dev/",
),
url: "https://grammy.dev/",
description: "The Telegram Bot Framework.",
},
],
{ cache_time: 30 * 24 * 3600 }, // one month in seconds
);
});
// Return empty result list for other queries.
bot.on("inline_query", (ctx) => ctx.answerInlineQuery([]));
Remember that you can always specify further options when calling API methods by using the options object of type
Other
. For example, this allows you to perform pagination for inline queries via an offset.
Note that grammY can auto-complete all of the fields in the above structure for you. Also, be sure to check out the exact specifications for inline results in the Telegram Bot API Reference.