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 Requestsresponses. - Call
handler.close()during shutdown if you need to stop the background thread cleanly.
Screenshot
Development
- Run tests:
tox - Lint with ruff:
ruff check
Metadata
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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| telegram_handler-1.4.5.tar.gz | 5.5 kB | Without provenance | Details for telegram_handler-1.4.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| telegram_handler-1.4.5-py3-none-any.whl | Python 3 | none | any | Details for telegram_handler-1.4.5-py3-none-any.whl |
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
|
Security for telegram-handler 1.4.5
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.