{"slug": "how-i-protected-my-telegram-bot-s-ai-quota-from-spam-users", "title": "How I Protected My Telegram Bot's AI Quota From Spam Users", "summary": "A developer running a Telegram bot on Google's Gemini free tier described building a three-layer quota protection system after a single user sent 1,400 messages in three hours and exhausted the API's 1,500 daily requests, leaving 999+ users with \"quota exceeded\" errors. The fix combines a per-user rate limit of 20 messages per hour, a global daily cap of 1,400 requests, and an in-memory response cache that the developer says saves 50-70% of quota with no database or extra cost.", "body_md": "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.\n\nLast week, one user sent 1,400 messages in 3 hours.\n\nThe result? 999+ users got \"quota exceeded\" errors. My entire AI feature was dead for the day.\n\nHere's how I fixed it.\n\nFree AI APIs have hard daily limits. One aggressive user can consume the entire quota. There's no native protection unless you build it.\n\nI needed:\n\ndef check_rate_limit(user_id):\n\n    now = time.time()\n\n    hour_ago = now - 3600\n\n    uid = str(user_id)\n\n    user_requests.setdefault(uid, [])\n\n    user_requests[uid] = [t for t in user_requests[uid] if t > hour_ago]\n\n    if len(user_requests[uid]) >= 20:\n\n        return False\n\n    user_requests[uid].append(now)\n\n    return True\n\n20 messages per hour per user. 99% users never hit this.\n\nLayer 2: Global Daily Cap\n\nglobal_counter = {\"date\": \"...\", \"count\": 0}\n\ndef check_global_limit():\n\n    today = datetime.utcnow().strftime(\"%Y-%m-%d\")\n\n    if global_counter[\"date\"] != today:\n\n        global_counter[\"date\"] = today\n\n        global_counter[\"count\"] = 0\n\n    if global_counter[\"count\"] >= 1400:\n\n        return False\n\n    global_counter[\"count\"] += 1\n\n    return True\n\nCap at 1,400 — 100 buffer below the 1,500 limit.\n\nLayer 3: Response Cache\n\nanswer_cache = {}\n\ncached = answer_cache.get(question.lower().strip())\n\nif cached:\n\n    return cached\n\nanswer_cache[question.lower().strip()] = answer\n\nCommon questions like \"What is Bitcoin?\" get cached. Repeat users hit cache, not API.\n\nThe Results\n\nSpam blocked: 20 msg/hour limit\n\n99% users unaffected\n\nQuota saved: 50-70% via caching\n\nZero extra cost: All in-memory, no database\n\nWhat I Learned\n\nFree tiers are fragile. Build protection before you scale. Rate limiting + caching is the cheapest fix — no infrastructure needed.\n\nWant the full script?\n\nI packaged this as a production-tested template. Includes:\n\nComplete quota_protection.py\n\nBot integration example\n\nSetup guide\n\nMulti-provider fallback ready\n\nI'm building an automated Telegram empire in public. Follow along for real code, real numbers, no hype.", "url": "https://wpnews.pro/news/how-i-protected-my-telegram-bot-s-ai-quota-from-spam-users", "canonical_source": "https://dev.to/rooterbhai/how-i-protected-my-telegram-bots-ai-quota-from-spam-users-5ca5", "published_at": "2026-09-20 16:52:48+00:00", "updated_at": "2026-09-20 17:24:35.398886+00:00", "lang": "en", "topics": ["ai-tools", "ai-products", "developer-tools"], "entities": ["Telegram", "Google", "Gemini"], "alternates": {"html": "https://wpnews.pro/news/how-i-protected-my-telegram-bot-s-ai-quota-from-spam-users", "markdown": "https://wpnews.pro/news/how-i-protected-my-telegram-bot-s-ai-quota-from-spam-users.md", "text": "https://wpnews.pro/news/how-i-protected-my-telegram-bot-s-ai-quota-from-spam-users.txt", "jsonld": "https://wpnews.pro/news/how-i-protected-my-telegram-bot-s-ai-quota-from-spam-users.jsonld"}}