Skip to main content

Telegram Handler

Drop-in Python logging.Handler that forwards log records to a Telegram chat or channel with buffering, retries, and a background worker so your app stays responsive.

Highlights

  • Works with channel usernames or chat IDs; just pass your bot token and destination.
  • Buffered writer batches messages and trims overlong payloads to avoid Telegram limits.
  • Automatic retries with backoff when Telegram throttles or transient network errors occur.
  • Thread-safe background thread keeps log emission non-blocking for the main application.
  • Simple to slot into existing logging setups alongside stream/file handlers.

Install

pip install telegram-handler

Quickstart

import logging

from telegram_handler import TelegramLoggingHandler

BOT_TOKEN = "123456:ABCDEF_example_bot_token"
CHANNEL = "example_channel_logger"  # or an integer chat ID

handler = TelegramLoggingHandler(BOT_TOKEN, CHANNEL)
logging.basicConfig(
    handlers=[handler, logging.StreamHandler()],
    level=logging.INFO,
    format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)

logger = logging.getLogger("demo")
for i in range(5):
    logger.error("iterating %s..", i)

What you need

  • Create a bot with BotFather to obtain BOT_TOKEN.
  • Create a channel or use a chat; add your bot as an admin so it can post messages.
  • Find the chat ID if you prefer using IDs instead of a public channel name (guide here).

Usage notes

  • bot_token: token returned by BotFather (keep it secret; do not commit it).
  • channel: public channel username (without the leading @) or an integer chat ID.
  • Messages are buffered and flushed every few seconds in a daemon thread to keep logging non-blocking.
  • Payloads are trimmed to stay under Telegram's message size limits; long bursts are split across flushes.
  • The handler automatically retries with exponential backoff on Telegram's 429 Too Many Requests responses.
  • Call handler.close() during shutdown if you need to stop the background thread cleanly.

Screenshot

screenshot

Development

  • Run tests: tox
  • Lint with ruff: ruff check

Release files for telegram-handler 1.4.5

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

Source distribution (sdist)

Source distribution for telegram-handler 1.4.5
File Size Uploaded
telegram_handler-1.4.5.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for telegram-handler 1.4.5
File Interpreter ABI Platform
telegram_handler-1.4.5-py3-none-any.whl Python 3 none any Details

Total release size: 11.0 kB

Release files / telegram_handler-1.4.5.tar.gz

Download URL telegram_handler-1.4.5.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
70b8ebe28968e32f69a51ea1af00e8ebb8614c5966317ca27a59f67e56f83456
BLAKE2b-256 checksum
How to use checksums
d4a02848493dd71bb7c6983dd4a88c5f9c5866ca1168d59a5bdb0fb592d1a97f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / telegram_handler-1.4.5-py3-none-any.whl

Download URL telegram_handler-1.4.5-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73c8a3baf9a7d857aa35a51bed96c4d6b2445e436c0f46265d1724019c24a341
BLAKE2b-256 checksum
How to use checksums
238a2fdf000383becb4dcd39de746c043a900ddc49e561fe89ff3d3a9b317637
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

1.4.5 This release

2 release files

1.4.4

2 release files

1.4.3

2 release files

1.4.1

2 release files

1.4

2 release files

1.3.1

1 release file

1.3

2 release files

1.2

2 release files

1.1

2 release files

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