Skip to main content

pyteledb

PyTeleDB

A Telegram-native embedded database for Telegram bots

PyPI version Python 3.10+ License: GPL v3


🧠 What is pyteledb?

pyteledb is a Telegram-native persistence abstraction that uses Telegram itself as the storage layer. No external databases required — just your bot and a Telegram channel or group.

Key Concepts

  • Telegram channel/group = your database
  • Pinned message = root index
  • Messages = records
  • Bot API only — no MTProto, no user accounts

✨ Features

  • 🔌 Zero external dependencies for storage — Telegram IS the database
  • 🚀 Async-first design with httpx
  • 💾 Optional local caching (memory or SQLite) for performance
  • 🔒 Soft locking for write coordination
  • 📦 Versioned records with optimistic concurrency
  • 🔧 Crash-resilient with repair tools
  • 📊 Built-in metrics and observability

📦 Installation

pip install pyteledb

# With SQLite cache support
pip install pyteledb[sqlite]

🚀 Quick Start

import asyncio
from pyteledb import TelegramDatabase, DatabaseConfig

async def main():
    config = DatabaseConfig(
        bot_token="YOUR_BOT_TOKEN",
        chat_id=-100123456789,  # Your channel/group ID
        db_name="my_database",
    )

    async with TelegramDatabase(config) as db:
        # Initialize (creates root index if not exists)
        await db.initialize()

        # Insert a record
        record = await db.insert({
            "user_id": 12345,
            "name": "Alice",
            "score": 100,
        })
        print(f"Created record: {record.id}")

        # Update
        await db.update(record.id, {
            "user_id": 12345,
            "name": "Alice",
            "score": 150,  # New high score!
        })

        # Get database info
        info = await db.info()
        print(f"Total records: {info['record_count']}")

asyncio.run(main())

📁 Project Structure

pyteledb/
├── core/       # Database logic (TelegramDatabase, Record, RootIndex)
├── telegram/   # Bot API client (messages, files, pins)
├── storage/    # Serialization, checksums, versioning
├── cache/      # Memory and SQLite caching
├── ops/        # Rate limiting, write queue, metrics
└── utils/      # Logging, time, ID generation

⚡ Performance Model

pyteledb is designed for small to medium Telegram bots with predictable workloads:

  • Never scans full chat history — fetches only required message IDs
  • Uses local cache for frequently accessed records
  • Rate-limit aware with automatic throttling
  • Prefers editMessage over sendMessage for updates

🛡️ Consistency & Safety

Telegram provides no transactions, so pyteledb implements:

  • Record versioning for optimistic concurrency
  • Soft locks for write coordination
  • Idempotent, retry-safe writes
  • Repair tools for crash recovery

🎯 Target Use Cases

  • ✅ Small to medium Telegram bots
  • ✅ Developer tooling
  • ✅ Predictable workloads
  • ✅ Human-inspectable data

🚫 Not Designed For

  • ❌ High-throughput applications
  • ❌ Complex queries (SQL/NoSQL parity)
  • ❌ Multi-service orchestration

📚 Documentation

📄 License

This project is licensed under the GPL-3.0 License — see the LICENSE file for details.

🤝 Contributing

Contributions are welcome! Please read our Contributing Guide first.


Made with ❤️ for the Telegram bot community

Metadata

Release files for pyteledb 0.1.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 pyteledb 0.1.0
File Size Uploaded
pyteledb-0.1.0.tar.gz 59.3 kB Details

Built distribution (wheel)

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

Total release size: 121.9 kB

Release files / pyteledb-0.1.0.tar.gz

Download URL pyteledb-0.1.0.tar.gz
Size 59.3 kB
Tags Source
SHA-256 checksum
How to use checksums
3c09052c320d66740958842aeb4dd9366a9501592d4922fe19428498fe0bc900
BLAKE2b-256 checksum
How to use checksums
fc65747750c92eaa59f2233a8d45a4212ba62944f3dee6a460cb78f4d0d7f7af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pyteledb-0.1.0-py3-none-any.whl

Download URL pyteledb-0.1.0-py3-none-any.whl
Size 62.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee87ef43ed17b3edbf7d9598bac9b955f3cb8837a5f6c990a58cdacaa7f87861
BLAKE2b-256 checksum
How to use checksums
c867253fb447a00bb6fb05ecd8a7f1fc1e8fdd74a88e6ab8b129ccd6d9037a49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.0 This release

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