Skip to main content

Lightweight Telegram Bot API broadcast engine for Python

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tgcast-0.1.0.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tgcast-0.1.0-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file tgcast-0.1.0.tar.gz.

File metadata

  • Download URL: tgcast-0.1.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tgcast-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ff979f72d8ff20b46e8fac7e5e21770ede5894486f4d2392ef04d4495060c0e5
MD5 6316abb95661457787bba693fdbb2ef6
BLAKE2b-256 5d82c441024199fad60ebb7541e44904eff69bc997022360f969d2febfea8499

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcast-0.1.0.tar.gz:

Publisher: publish.yml on Seinarukiro2/telecast-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcast-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tgcast-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tgcast-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d42410d5c09dbbb1bee6b899f1c57d5a16a896dd05457a14a3248085ab4eccdc
MD5 857c174ca123d1805da9ae1ba13e75f4
BLAKE2b-256 2f5b040669b665e7d38cda4953f54a050ec3b6b8198d689b9c2b19638de658fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcast-0.1.0-py3-none-any.whl:

Publisher: publish.yml on Seinarukiro2/telecast-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page