Skip to main content

tg-notification-bot

CI PyPI

An async Python library for services, scripts and CI jobs that need to send text, photo and document notifications to one or more Telegram chats through a bot.

Install

pip install tg-notification-bot

Example

Create a bot with @BotFather, add it to the target chat, then set TG_BOT_TOKEN and TG_CHAT_ID (one chat ID or @channel_username) and run:

import asyncio
import os

from tg_notification_bot import TelegramNotificationBot


async def main() -> None:
    async with TelegramNotificationBot(os.environ["TG_BOT_TOKEN"], os.environ["TG_CHAT_ID"]) as bot:
        await bot.send_message("<b>Deploy finished</b>: api 2.3.1")


asyncio.run(main())

To send to several chats, use the bulk methods. They return a per-chat result instead of raising:

async def notify_all(token: str) -> None:
    async with TelegramNotificationBot(token, [-1001234567890, "@my_channel"]) as bot:
        result = await bot.send_message_bulk("Backup finished")
        print(f"{result.successful_chats}/{result.total_chats} delivered, failed: {result.failed_chat_ids}")

A longer script is in examples/multi_chat_example.py. Detailed behavior is described in docs/usage.md.

Features

  • send_message, send_photo and send_document for one chat. Telegram errors are raised as BotBlockedError, ChatNotFoundError, RateLimitError (with retry_after) or TelegramNotificationError.
  • send_message_bulk, send_photo_bulk and send_document_bulk for several chats, sent concurrently with a max_concurrent limit. Failures are collected in a BulkSendResult as the same exceptions, or with fail_silently=False the first one is raised and the remaining sends are cancelled.
  • Files as a local path, an http(s) URL or a file-like object.
  • Chat IDs as int, str or a list. A digit-only string is also tried with the - and -100 prefixes, so IDs copied from Telegram clients without the prefix work. The result is cached.
  • Default chat IDs from the TG_CHAT_ID or TG_CHAT_IDS environment variable (comma-separated) when only a token is given.
  • Validation of the token format, chat IDs and message length (1 to 4096 characters) with pydantic.
  • Async context manager that closes the HTTP session.
  • Type annotations shipped with the package (py.typed).

Limitations

  • Async only.
  • No retries and no rate limiting. max_concurrent limits parallel requests, not requests per second.
  • Messages are always sent with a parse mode (HTML by default), so <, > and & in plain text must be escaped.
  • Single-chat methods return None, not the sent message.
  • Telegram file_id strings are not accepted. A string that is not a URL is treated as a local path.
  • A digit-only string chat ID costs one to three getChat requests the first time it is used by a bot object. Integer IDs and @usernames are sent as is.

Compatibility

Dependency Supported Tested in CI
Python 3.10 to 3.14 3.10, 3.11, 3.12, 3.13, 3.14
aiogram >=3.15, <4 newest allowed release, and 3.15.0
pydantic >=2.4.1, <3 newest allowed by aiogram, and 2.4.1

The lowest versions (aiogram 3.15.0, pydantic 2.4.1) are tested on Python 3.10. Python 3.9 was supported up to 1.0.0.

Development

uv sync
uv run pytest
uv run ruff check .
uv run ruff format --check .
uv run mypy .

With pip 25.1 or newer, use pip install -e . --group dev and run the same commands without uv run. pre-commit install runs ruff and mypy on every commit.

Tests never call the Telegram API: aiogram.Bot is replaced with an autospec mock, and a fixture in tests/conftest.py fails any test that tries to send a real request. pytest fails below 100% branch coverage.

CI runs lint, type checks and tests on every push and pull request. To release, update __version__ in tg_notification_bot/__init__.py and CHANGELOG.md, then push a tag vX.Y.Z. The publish workflow checks that the tag matches the version, runs CI and uploads to PyPI.

License

MIT

Metadata

Release files for tg-notification-bot 1.1.0

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

Source distribution (sdist)

Source distribution for tg-notification-bot 1.1.0
File Size Uploaded Provenance
tg_notification_bot-1.1.0.tar.gz 17.4 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tg-notification-bot 1.1.0
File Interpreter ABI Platform
tg_notification_bot-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.0 kB

Release files / tg_notification_bot-1.1.0.tar.gz

Download URL tg_notification_bot-1.1.0.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
635a4e06e12c1e4c274673ca778a12f313853f478ccc4dc979327ada6e7e5568
BLAKE2b-256 checksum
How to use checksums
25d457f5294d6f93716b96d3588574cd08a8f7e90bd630e31c322a8a600d5900
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / tg_notification_bot-1.1.0-py3-none-any.whl

Download URL tg_notification_bot-1.1.0-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d9987c6337315cfa82e2d1d356e77c9b5b9614e452361c54c1e9f339796a0db
BLAKE2b-256 checksum
How to use checksums
d28443068a9039d4207bba9e8b105740513897911a416ce6a3e5cdfdd578b311
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Security for tg-notification-bot 1.1.0

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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tg_notification_bot-1.1.0.tar.gz

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 Oct 12, 2026.

Transparency log

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.

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