Skip to main content

Small package for working with config files

Project description

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

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

telegram_handler-1.4.5.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

telegram_handler-1.4.5-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file telegram_handler-1.4.5.tar.gz.

File metadata

  • Download URL: telegram_handler-1.4.5.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for telegram_handler-1.4.5.tar.gz
Algorithm Hash digest
SHA256 70b8ebe28968e32f69a51ea1af00e8ebb8614c5966317ca27a59f67e56f83456
MD5 0c3db731f84c2b0d24640a62fb92c527
BLAKE2b-256 d4a02848493dd71bb7c6983dd4a88c5f9c5866ca1168d59a5bdb0fb592d1a97f

See more details on using hashes here.

File details

Details for the file telegram_handler-1.4.5-py3-none-any.whl.

File metadata

File hashes

Hashes for telegram_handler-1.4.5-py3-none-any.whl
Algorithm Hash digest
SHA256 73c8a3baf9a7d857aa35a51bed96c4d6b2445e436c0f46265d1724019c24a341
MD5 32e0f0b39a16ba8aa3ea1157128fc8b0
BLAKE2b-256 238a2fdf000383becb4dcd39de746c043a900ddc49e561fe89ff3d3a9b317637

See more details on using hashes here.

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