Skip to main content

🛡️ Shieldgram

PyPI version Python Tests License

Anti-abuse security middleware for Telegram bots.

Protect your bot from:

  • ✅ Flood attacks
  • ✅ Spam
  • ✅ Command abuse
  • ✅ Automated users
pip install shieldgram

Quick Start

from aiogram import Bot, Dispatcher, Router
from aiogram.filters import Command
from aiogram.types import Message
from shieldgram import Shield

router = Router()
shield = Shield(redis="redis://localhost:6379/0")

@router.message(Command("start"))
async def cmd_start(message: Message) -> None:
    await message.answer("Hello!")

async def main():
    bot = Bot(token="YOUR_BOT_TOKEN")
    dp = Dispatcher()
    dp.message.middleware(shield)
    dp.include_router(router)
    await dp.start_polling(bot)

Threat Score Engine

Shieldgram assigns a threat score to every user action:

Signal Weight
Flood (burst) +0.40
Spam (repeat) +0.30
Too many links +0.20
Suspicious behavior +0.10
Score Verdict
0.0–0.4 ALLOW
0.4–0.7 WARN
0.7–1.0 BLOCK

Architecture

Telegram Update → Shield Middleware → Detection Engine
                                          │
                              ┌───────────┼───────────┐
                         RateLimiter  FloodDetector  ...
                              │           │
                              └───────────┘
                                    │
                            Threat Score Engine
                                    │
                            Decision Engine
                               │        │
                           Redis    PostgreSQL

Configuration

shield = Shield(
    redis="redis://localhost:6379/0",
    rate_limiter={
        "sliding_window_seconds": 60,
        "max_requests_per_window": 30,
        "token_bucket_rate": 5,
        "token_bucket_burst": 10,
    },
    flood_detector={
        "burst_window_seconds": 5,
        "burst_threshold": 10,
        "repeat_window_seconds": 30,
        "repeat_threshold": 5,
    },
    block_threshold=0.7,
    warn_threshold=0.4,
    ignore_users=[123456789],
)

Run the demo

docker compose up -d

Or locally:

pip install shieldgram
export BOT_TOKEN="your_token"
python examples/protected_bot.py

Then attack it:

python examples/attack_simulator.py

Roadmap

  • v0.1 — Middleware, Rate Limiter, Flood Detector, Threat Scoring
  • v0.2 — Spam detection, Reputation system, PostgreSQL logs
  • v0.3 — Dashboard, API, Docker deployment
  • v1.0 — Plugin system, production hardening

License

MIT — see LICENSE

Metadata

Release files for shieldgram 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shieldgram 0.2.0
File Size Uploaded
shieldgram-0.2.0.tar.gz 20.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for shieldgram 0.2.0
File Interpreter ABI Platform
shieldgram-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 43.8 kB

Release files / shieldgram-0.2.0.tar.gz

Download URL shieldgram-0.2.0.tar.gz
Size 20.9 kB
Tags Source
SHA-256 checksum
How to use checksums
21415e2247db90240cf2c4eef765f6183a29bc552b9389d67ec058f29bfb229b
BLAKE2b-256 checksum
How to use checksums
96a14b36e30ce5835a80237b31f58f347d007dffd55d0df2607920d3afe2061f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.0

Release files / shieldgram-0.2.0-py3-none-any.whl

Download URL shieldgram-0.2.0-py3-none-any.whl
Size 23.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38f13592c02ab844f480c2ef905fc84123137971f55375f9952c9964cb7da824
BLAKE2b-256 checksum
How to use checksums
5458bf4600591f8c85e975330493f20eb1ed5ac0cc74ec13150f12e1a507e383
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page