Skip to main content

tgcast

Lightweight Telegram Bot API broadcast engine for Python.

  • Self-contained — no external server, just pip install and go
  • Priority queues with weighted round-robin (5:3:1) and starvation prevention
  • Rate limiting — global + per-chat token buckets, auto-adapts to Telegram 429
  • Campaigns — broadcast to millions in batches, crash-safe with idempotent enqueue
  • SQLite — WAL mode, single-file, zero config
  • Templates — YAML with locale fallback, or plug in your own renderer
  • Media — send photos and documents (file_id, URL, or local path)

Intended use: transactional notifications and opt-in messaging. Please respect Telegram Bot API policies.

Install

pip install tgcast

For YAML templates:

pip install tgcast[templates]

Requires Python 3.11+.

Quickstart

import os
import signal
from tgcast import Telecast, Task

eng = Telecast(bot_token=os.environ["BOT_TOKEN"])
eng.start()

task_id = eng.enqueue(Task(
    chat_id=123456789,
    text="Hello from tgcast!",
))
print(f"Enqueued: {task_id}")

signal.pause()
eng.shutdown()

Or as a context manager:

with Telecast(bot_token="...") as eng:
    eng.enqueue(Task(chat_id=123, text="Hi!"))

See examples/ for more.

API

# Lifecycle
eng = Telecast(bot_token="...", store_dsn="tgcast.db")
eng.start()
eng.shutdown()

# Tasks
task_id = eng.enqueue(Task(...))
status = eng.task_status(task_id)

# Campaigns
cid = eng.create_campaign(CampaignConfig(name="...", template_key="..."))
eng.add_recipients(cid, [Recipient(chat_id=1), ...])
eng.start_campaign(cid)
eng.pause_campaign(cid)
stats = eng.campaign_stats(cid)

# Dead-letter queue
tasks, total = eng.dlq_list(limit=20, offset=0)
eng.dlq_requeue(task_id)

Templates

YAML with locale fallback (exact -> base -> en):

welcome:
  en: "Hello, {name}!"
  ru: "Привет, {name}!"
eng = Telecast(
    bot_token="...",
    templates_path="templates.yaml",
)
eng.enqueue(Task(
    chat_id=123,
    template_key="welcome",
    locale="ru",
    vars={"name": "Иван"},
))

Or a custom renderer:

class MyRenderer:
    def render(self, key: str, locale: str, vars: dict) -> str:
        return f"Hi {vars['name']}!"

eng = Telecast(bot_token="...", template_renderer=MyRenderer())

Media

from tgcast import Telecast, Task, TaskKind

eng.enqueue(Task(
    chat_id=123,
    kind=TaskKind.SEND_PHOTO,
    photo="https://cdn.example.com/banner.jpg",
    caption="Check this out!",
))

eng.enqueue(Task(
    chat_id=123,
    kind=TaskKind.SEND_DOCUMENT,
    document="/path/to/report.pdf",
    caption="Monthly report",
))

photo and document accept file_id, URL, or local file path.

Configuration

All parameters have sensible defaults. Only bot_token is required.

Parameter Default Description
bot_token — Telegram Bot API token
store_dsn tgcast.db SQLite database path
templates_path — YAML templates file
templates_data — YAML templates as bytes
template_renderer — Custom renderer (overrides YAML)
global_rps 25 Global messages/sec
per_chat_rps 1 Per-chat messages/sec
max_concurrency 8 Worker pool size
lease_ttl 30 Task lease duration (seconds)
max_retries 5 Max retries before DLQ
base_backoff 1 Initial retry delay (seconds)
max_backoff 300 Max retry delay (seconds)
logger logging.getLogger("tgcast") Custom logger

Idempotency

Set Task.idempotency_key to prevent duplicate sends:

eng.enqueue(Task(
    chat_id=123,
    text="Order confirmed",
    idempotency_key="order:456:confirmation",
))

Campaign tasks auto-generate keys: campaign:{id}:{chat_id}.

License

MIT

Metadata

Release files for tgcast 0.1.1

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

Source distribution (sdist)

Source distribution for tgcast 0.1.1
File Size Uploaded
tgcast-0.1.1.tar.gz 20.2 kB Details

Built distribution (wheel)

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

Total release size: 39.2 kB

Release files / tgcast-0.1.1.tar.gz

Download URL tgcast-0.1.1.tar.gz
Size 20.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5a01635d6255961b96eaec5e2cd8eac1c6844c38c739c9d9942ac5ae331b2b2b
BLAKE2b-256 checksum
How to use checksums
7df92077fbbc6b422d9f1ca76f54a2c1acd9f9e2de8d16a797b4211ce2df4701
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2026.

Transparency log

Release files / tgcast-0.1.1-py3-none-any.whl

Download URL tgcast-0.1.1-py3-none-any.whl
Size 19.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
455cf7443542baa333b8227986d9a3484e2c4ae57a9aed4ba9f888ad7ec601fa
BLAKE2b-256 checksum
How to use checksums
6db10ed060417cc5e366333755fe80f6163a832a95c289bc1ea2626248d8c1c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 30, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 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