<?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: Rooter</title>
    <description>The latest articles on DEV Community by Rooter (@rooterbhai).</description>
    <link>https://dev.to/rooterbhai</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%2F4133689%2F8efb2b0c-5b42-4546-97d8-db7ca5fa3724.png</url>
      <title>DEV Community: Rooter</title>
      <link>https://dev.to/rooterbhai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rooterbhai"/>
    <language>en</language>
    <item>
      <title>How I Protected My Telegram Bot's AI Quota From Spam Users</title>
      <dc:creator>Rooter</dc:creator>
      <pubDate>Sun, 20 Sep 2026 16:52:48 +0000</pubDate>
      <link>https://dev.to/rooterbhai/how-i-protected-my-telegram-bots-ai-quota-from-spam-users-5ca5</link>
      <guid>https://dev.to/rooterbhai/how-i-protected-my-telegram-bots-ai-quota-from-spam-users-5ca5</guid>
      <description>&lt;p&gt;I run a Telegram bot that uses Google's Gemini API for free-tier AI features. The free tier gives 1,500 requests per day.&lt;/p&gt;

&lt;p&gt;Last week, one user sent 1,400 messages in 3 hours.&lt;/p&gt;

&lt;p&gt;The result? 999+ users got "quota exceeded" errors. My entire AI feature was dead for the day.&lt;/p&gt;

&lt;p&gt;Here's how I fixed it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Free AI APIs have hard daily limits. One aggressive user can consume the entire quota. There's no native protection unless you build it.&lt;/p&gt;

&lt;p&gt;I needed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Per-user rate limiting&lt;/li&gt;
&lt;li&gt;Global daily cap&lt;/li&gt;
&lt;li&gt;Response caching for common questions&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Fix — 3 Layers
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Layer 1: Per-User Rate Limit
&lt;/h3&gt;

&lt;p&gt;def check_rate_limit(user_id):&lt;br&gt;
    now = time.time()&lt;br&gt;
    hour_ago = now - 3600&lt;br&gt;
    uid = str(user_id)&lt;br&gt;
    user_requests.setdefault(uid, [])&lt;br&gt;
    user_requests[uid] = [t for t in user_requests[uid] if t &amp;gt; hour_ago]&lt;br&gt;
    if len(user_requests[uid]) &amp;gt;= 20:&lt;br&gt;
        return False&lt;br&gt;
    user_requests[uid].append(now)&lt;br&gt;
    return True&lt;br&gt;
20 messages per hour per user. 99% users never hit this.&lt;/p&gt;

&lt;p&gt;Layer 2: Global Daily Cap&lt;/p&gt;

&lt;p&gt;global_counter = {"date": "...", "count": 0}&lt;/p&gt;

&lt;p&gt;def check_global_limit():&lt;br&gt;
    today = datetime.utcnow().strftime("%Y-%m-%d")&lt;br&gt;
    if global_counter["date"] != today:&lt;br&gt;
        global_counter["date"] = today&lt;br&gt;
        global_counter["count"] = 0&lt;br&gt;
    if global_counter["count"] &amp;gt;= 1400:&lt;br&gt;
        return False&lt;br&gt;
    global_counter["count"] += 1&lt;br&gt;
    return True&lt;br&gt;
Cap at 1,400 — 100 buffer below the 1,500 limit.&lt;/p&gt;

&lt;p&gt;Layer 3: Response Cache&lt;/p&gt;

&lt;p&gt;answer_cache = {}&lt;/p&gt;

&lt;p&gt;cached = answer_cache.get(question.lower().strip())&lt;br&gt;
if cached:&lt;br&gt;
    return cached&lt;/p&gt;

&lt;p&gt;answer_cache[question.lower().strip()] = answer&lt;br&gt;
Common questions like "What is Bitcoin?" get cached. Repeat users hit cache, not API.&lt;/p&gt;

&lt;p&gt;The Results&lt;br&gt;
Spam blocked: 20 msg/hour limit&lt;/p&gt;

&lt;p&gt;99% users unaffected&lt;/p&gt;

&lt;p&gt;Quota saved: 50-70% via caching&lt;/p&gt;

&lt;p&gt;Zero extra cost: All in-memory, no database&lt;/p&gt;

&lt;p&gt;What I Learned&lt;br&gt;
Free tiers are fragile. Build protection before you scale. Rate limiting + caching is the cheapest fix — no infrastructure needed.&lt;/p&gt;

&lt;p&gt;Want the full script?&lt;br&gt;
I packaged this as a production-tested template. Includes:&lt;/p&gt;

&lt;p&gt;Complete quota_protection.py&lt;/p&gt;

&lt;p&gt;Bot integration example&lt;/p&gt;

&lt;p&gt;Setup guide&lt;/p&gt;

&lt;p&gt;Multi-provider fallback ready&lt;/p&gt;

&lt;p&gt;I'm building an automated Telegram empire in public. Follow along for real code, real numbers, no hype.&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%2Fn77a2v3qc3j0jgmhcouj.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%2Fn77a2v3qc3j0jgmhcouj.png" alt=" " width="287" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>telegram</category>
      <category>automation</category>
      <category>sideprojects</category>
    </item>
  </channel>
</rss>
