Anti-abuse security middleware for Telegram bots
Project description
🛡️ Shieldgram
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 — PyPI package, Plugin system
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
shieldgram-0.1.0.tar.gz
(15.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shieldgram-0.1.0.tar.gz.
File metadata
- Download URL: shieldgram-0.1.0.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed195e895437baa24b0698c63a378d81f555d11a3a0b91985ef230057f381f72
|
|
| MD5 |
7d27fe30630e3b9e349685930c8cb374
|
|
| BLAKE2b-256 |
6ed36c9a01f5594f0726c640bbe33994fea1eae6ba8fe1b8a351d9b17efdeffb
|
File details
Details for the file shieldgram-0.1.0-py3-none-any.whl.
File metadata
- Download URL: shieldgram-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a42ed2b80595f5db433da5347efb1451fa2fc3d93b7ff2546fc7464c8129e5dd
|
|
| MD5 |
c3a7658b533109d5c198245e5b6ce423
|
|
| BLAKE2b-256 |
b087167db9469dc567a9541298680efbfee7b0330cf93888512c8813d7f81cf6
|