Skip to content
TGWAY
Channels & Bots STEP 72 / 99

Build a Telegram Inline Bot People Can Call From Any Chat

Turn on inline mode in BotFather, answer inline_query updates with answerInlineQuery, use cache_time, is_personal and next_offset, and keep answers fast.

  • Applies toDevelopers
  • LevelL4
  • Reading time4 min
  • Verified

The short answer

  1. 1@yourbot plus a query shows your results in any chat, without adding the bot.
  2. 2Turn it on with /setinline in @BotFather and set a placeholder.
  3. 3Answer each inline_query quickly with answerInlineQuery, up to 50 results.
In this article
  1. What inline mode does
  2. Turn it on in @BotFather
  3. Receive a query and answer it
  4. Answer inline queries fast
  5. Other things worth knowing
  6. FAQ
  7. At a glance

What inline mode does

In any chat, a user types @yourbot and a search word in the message field. Your bot shows a list of results above the field, and the one they pick is sent straight into that chat. Your bot does not have to be a member of the chat. Inline mode suits "find it and paste it" jobs such as GIF search, translation or sharing links. For how this looks to users, see using inline bots without adding them.

Turn it on in @BotFather

  1. Choose your bot and turn on inline mode with /setinline.
  2. Set the placeholder, the grey hint shown in the message field, for example "Search GIFs…".
  3. Optionally turn on /setinlinegeo to receive the user's location with each query, and /setinlinefeedback to learn which result they picked.

Receive a query and answer it

As the user types, your bot receives inline_query updates (see webhooks vs long polling). Each one has the search text in query and an id. Reply with answerInlineQuery using that ID:

curl https://api.telegram.org/bot123456:ABC-EXAMPLE/answerInlineQuery \
  -H "Content-Type: application/json" \
  -d '{"inline_query_id":"QUERY_ID",
       "results":[{"type":"article","id":"1","title":"Say hello",
         "input_message_content":{"message_text":"Hello there"}}],
       "cache_time":60}'
FieldMeaning
resultsUp to 50 per answer; there are formats for articles, photos, GIFs, videos, stickers and more
cache_timeHow many seconds Telegram's servers keep the results for the same query; the default is 300
is_personalCache separately for each user, when results differ from person to person
next_offsetMarks that more results exist; when the user scrolls to the end you are asked again with this value

Answer inline queries fast

Users expect results while they are still typing. If your answer is slow, nothing appears. Prepare heavy searches in advance, set a sensible cache_time for common queries, and have default results ready for when the query is empty, that is, when the user has typed only @yourbot.

Other things worth knowing

  • Which result was chosen: with feedback on, the picked result arrives as chosen_inline_result, useful for statistics.
  • Starting from a button: an inline button with switch_inline_query lets the user pick a chat and opens inline mode there; see bot buttons.
  • Groups can block it: a group's permissions can stop members from using inline bots, and then your results cannot be sent there.

FAQ

How do I enable inline mode for my Telegram bot?

Send /setinline to @BotFather, choose your bot and set the placeholder text.

How many results can an inline bot return?

Up to 50 per answer. Use next_offset to load more when the user scrolls.

Why are my inline results not showing?

Usually the answer is too slow, or answerInlineQuery failed. Answer fast and keep default results for an empty query.

Sources

Summary

At a glance

Build a Telegram Inline Bot People Can Call From Any Chat — at a glance illustration
  1. 1Enable

    /setinline, plus optional geo and feedback.

  2. 2Answer

    answerInlineQuery with the query id; cache_time defaults to 300 s.

  3. 3Privacy

    Use is_personal when results depend on the user.

Share this guide

Send it to a friend who has the same question.

Telegram WhatsApp
More

Open on your phonePoint your phone camera here to open this guide.

Updated

Related articles

View all ›