Lesson 5 of 9 · 22m
Inline Mode: Results, Feedback, and Latency Discipline
Design inline query handlers that return useful results fast, handle chosen results correctly, and avoid leaking private data into shared chats.
Inline mode is the moment your bot stops being a chat participant and becomes a universal search box inside Telegram. A user types @YourBot deploy in any chat, your service returns results, and the selected result is posted as a message. This is a powerful distribution mechanism — and a fast way to leak internal information if you forget that the invoking chat may include people the user should not share with.
Enable and understand the flow
Inline mode is enabled through @BotFather. Telegram then sends inline_query updates when users invoke the bot inline:
{
"inline_query": {
"id": "913412341234",
"from": {"id": 42},
"query": "incident 183",
"offset": ""
}
}Your handler calls answerInlineQuery with result objects. The user selects one; Telegram sends it into the current chat. Depending on configuration, you may receive a chosen_inline_result update telling you which result was selected.
Latency is product quality
Inline queries are interactive keystrokes. If your handler searches a slow primary database, users perceive the bot as broken. Use indexes, cached projections, small limits, and strict timeouts. Returning nothing fast is often better than returning the perfect answer after the UI has moved on.
results = await search_incidents(query.text, limit=10, timeout_ms=500)
await bot.answer_inline_query(
inline_query_id=query.id,
results=results,
cache_time=5,
is_personal=True,
)cache_time is a hint to Telegram clients. It is not a freshness guarantee or a security control. Use short cache times for operational data and think carefully before caching user-specific answers.
Result content is shared content
An inline result lands in whatever chat the user invoked. If it contains private hostnames, customer names, financial data, or internal incident details, you have created a one-tap exfiltration mechanism. Mark results as personal where appropriate, but understand that is_personal affects caching behavior, not the fact that the chosen message is shared.
A safer pattern is to return a teaser and require the user to open an authenticated flow:
Incident INC-183: database latency elevated
Open the incident console to view details.If your organization accepts richer sharing, classify result templates by data sensitivity and intentionally choose what fields can appear.
Structure results for clients
Inline result types include articles, photos, videos, documents, and more. Article results are the dependable starting point because they are just structured text plus optional keyboard:
InlineQueryResultArticle(
id="incident:183",
title="INC-183 — database latency elevated",
description="Opened 14 minutes ago; owner: platform",
input_message_content=InputTextMessageContent(
"INC-183 database latency elevated\nhttps://ops.sudosecurity.org/incidents/183"
),
)Use stable result IDs. They help with feedback and debugging. Do not put large serialized objects in them.
Chosen result feedback is observability, not delivery proof
chosen_inline_result tells you that a result was chosen, but it does not replace application state or analytics design. Treat it as a signal for measuring usage and improving search ranking. If selection must start a workflow, use explicit buttons or commands in the posted message rather than inferring intent from selection alone.
Field notes
- Test inline mode in a group with another account. DM-only testing misses the sharing boundary.
- Pagination uses
offset; implement it even if initial traffic is small. - Empty queries are common. Decide whether to show trending, recent, or no results.
- Do not expose internal search ranking metadata or debug IDs in user-visible text.
- Inline mode can generate significant request volume because users type incrementally; budget for it.
Was this lesson helpful?