<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: twofolp</title>
    <description>The latest articles on DEV Community by twofolp (@dahmadiev57).</description>
    <link>https://dev.to/dahmadiev57</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4191901%2Fdf0887d5-53c9-476e-8820-443d9685bf1c.png</url>
      <title>DEV Community: twofolp</title>
      <link>https://dev.to/dahmadiev57</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dahmadiev57"/>
    <language>en</language>
    <item>
      <title>Building a fast Telegram inline bot: what actually matters</title>
      <dc:creator>twofolp</dc:creator>
      <pubDate>Wed, 18 Nov 2026 20:57:08 +0000</pubDate>
      <link>https://dev.to/dahmadiev57/building-a-fast-telegram-inline-bot-what-actually-matters-37bk</link>
      <guid>https://dev.to/dahmadiev57/building-a-fast-telegram-inline-bot-what-actually-matters-37bk</guid>
      <description>&lt;p&gt;Every inline Telegram bot lives or dies by two things: relevance and latency. There is no onboarding, no menu, no conversation — a user types @yourbot query in any chat, a panel appears above the keyboard, and they tap a result.&lt;/p&gt;

&lt;p&gt;I built an image search bot (@repinlybot) and this post is about the Telegram side of it: how inline mode works, which parameters actually change behaviour, and &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F89giqpaqt18yrxxsrp09.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F89giqpaqt18yrxxsrp09.png" alt="Six actions to send one reference image, reduced to one query" width="800" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Enabling inline mode
&lt;/h2&gt;

&lt;p&gt;In BotFather:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/setinline
Choose a bot: @yourbot
Prompt: Search images…
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The prompt is the placeholder users see. Keep it short and make it obvious what they can type.&lt;/p&gt;

&lt;h2&gt;
  
  
  The update flow
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User types "@yourbot cats"
        |
        v
Telegram --inline_query--&amp;gt; your backend
        &amp;lt;--answerInlineQuery-- your backend
        |
        v
Panel with results -&amp;gt; tap -&amp;gt; content sent to the chat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frvgnsqwm1h1q5soplnqa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frvgnsqwm1h1q5soplnqa.png" alt="Inline query flow: the user types, Telegram calls the bot, results appear above the keyboard" width="800" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Two update types matter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;inline_query — the user typed or changed the query.&lt;/li&gt;
&lt;li&gt;chosen_inline_result — the user picked a result. This is your conversion event.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;chosen_inline_result is only delivered if you enable it in BotFather (/setinlinefeedback). It is off by default, and without it you have no idea whether anyone actually uses your results.&lt;/p&gt;

&lt;h2&gt;
  
  
  Answering a query
&lt;/h2&gt;

&lt;p&gt;A minimal handler:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bot.on('inline_query', async (ctx) =&amp;gt; {
  const query = ctx.inlineQuery.query.trim();
  const offset = Number(ctx.inlineQuery.offset || 0);

  if (query.length &amp;lt; 2) {
    return ctx.answerInlineQuery([], {
      switch_pm_text: 'Type a search query',
      switch_pm_parameter: 'empty',
      cache_time: 0,
    });
  }

  const page = await search(query, offset, 24);

  await ctx.answerInlineQuery(
    page.items.map(toResult),
    {
      cache_time: 300,
      is_personal: false,
      next_offset: page.hasMore ? String(offset + 24) : '',
    }
  );
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Four parameters do most of the work.&lt;/p&gt;

&lt;h3&gt;
  
  
  cache_time
&lt;/h3&gt;

&lt;p&gt;How long Telegram caches the answer for the same query. For image search, 300 seconds is reasonable. But for short or empty queries use 0 — otherwise a user who fixes a typo still sees the cached "nothing found" result.&lt;/p&gt;

&lt;h3&gt;
  
  
  is_personal
&lt;/h3&gt;

&lt;p&gt;Set true if results depend on the user (history, saved items, language). Otherwise Telegram may serve one user's cached results to another. For a public image search, false is correct.&lt;/p&gt;

&lt;h3&gt;
  
  
  next_offset
&lt;/h3&gt;

&lt;p&gt;Pagination. Telegram calls you again with this offset when the user scrolls. Return an empty string when there is nothing more. If you always return empty, users see one page and think that is all.&lt;/p&gt;

&lt;h3&gt;
  
  
  switch_pm_text / switch_pm_parameter
&lt;/h3&gt;

&lt;p&gt;Renders a button that opens a private chat with the bot. Use it for empty states instead of leaving a blank panel.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fiw7wj1t8v1vg2rzjz4fk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fiw7wj1t8v1vg2rzjz4fk.png" alt="The inline result panel inside a chat: a grid of pins above the keyboard" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Result types: URL vs cached file
&lt;/h2&gt;

&lt;p&gt;For photos you have two options:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 1. Let Telegram fetch and cache the file
{
  type: 'photo',
  id: String(item.id),
  photo_url: item.fullUrl,
  thumbnail_url: item.thumbUrl,
  photo_width: item.width,
  photo_height: item.height,
  title: item.title,
  description: item.author,
}

// 2. Send a file Telegram already has
{
  type: 'photo',
  id: String(item.id),
  photo_file_id: item.fileId,
  title: item.title,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The second one is effectively instant, but file_id is only valid for your bot. The pattern that works well:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Return photo_url the first time.&lt;/li&gt;
&lt;li&gt;On chosen_inline_result, look up the item by result_id.&lt;/li&gt;
&lt;li&gt;Store the file_id that Telegram assigned to the file.&lt;/li&gt;
&lt;li&gt;On subsequent queries, return photo_file_id.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This avoids re-downloading the same image on every search.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 500 ms budget
&lt;/h2&gt;

&lt;p&gt;If the panel takes a second to appear, users describe the bot as slow or broken. Everything else is secondary.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff5470pb02uplr42ys4h4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff5470pb02uplr42ys4h4.png" alt="Perceived quality by response time: 300 ms feels instant, 2500 ms feels broken" width="800" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What helped:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Small thumbnails.&lt;/strong&gt; The panel is a grid. Serving 1000 px images there is the fastest way to make it feel sluggish. 200–300 px on the long edge is enough.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Query normalization and cache.&lt;/strong&gt; "  Scandinavian  Bedroom " and "scandinavian bedroom" should hit the same cache entry.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Skip the origin for short queries.&lt;/strong&gt; You cannot stop Telegram from sending an inline_query on every keystroke, but you can avoid hitting the backend: check the cache first, and skip search under 2 characters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Paginate, do not dump.&lt;/strong&gt; 20–30 results per page. More results means a bigger response and a slower panel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Images that Telegram cannot fetch
&lt;/h2&gt;

&lt;p&gt;A direct image URL is not always fetchable. Some CDNs require a Referer, block unknown user agents, or return HTML instead of an image. Telegram then silently drops the thumbnail.&lt;/p&gt;

&lt;p&gt;The fix is an image proxy or CDN you control:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Telegram -&amp;gt; your proxy -&amp;gt; origin
              |
              +- cache: 300 px thumb + original on demand
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This also gives you resizing, format normalization, and the ability to swap the origin later without touching the bot. Budget for it: it is the least glamorous part and the one that decides whether half your results render at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Metrics that matter
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Queries&lt;/strong&gt; — how many times the bot is invoked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Answers&lt;/strong&gt; — how many inline_query calls got a non-empty result.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choices&lt;/strong&gt; — how many chosen_inline_result events.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choice rate&lt;/strong&gt; = choices / answers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the choice rate is low, the problem is relevance. If the answer rate is low, the problem is coverage or normalization. Neither is fixed by adding features.&lt;/p&gt;

&lt;p&gt;Also log queries with zero results. It is the cheapest product research you will ever do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lessons
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Latency is the feature.&lt;/strong&gt; A relevant result that arrives in 2 seconds loses to a good result in 300 ms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handle the empty state.&lt;/strong&gt; A blank panel reads as "the bot is dead".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache the file, not just the search.&lt;/strong&gt; file_id reuse is a free speedup.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Treat mobile networks as the default.&lt;/strong&gt; Thumbnails, not originals.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enable chosen_inline_result early.&lt;/strong&gt; Otherwise you are flying blind.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;@repinlybot — inline image search for Telegram chats.&lt;/p&gt;

&lt;p&gt;If you are building an inline bot, the two areas worth over-investing in are thumbnails and the query cache. Everything else is comparatively straightforward.&lt;/p&gt;

</description>
      <category>telegram</category>
      <category>bots</category>
      <category>api</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
