Simple, async-first Telegram alerts for Python
Project description
py-telegram-alert
Simple, async-first Telegram alerts for Python.
Installation
pip install py-telegram-alert
Setup
1. Create a Telegram Bot
- Open Telegram and message @BotFather
- Send
/newbotand follow the prompts - Copy the bot token you receive
2. Get Your Chat ID
- Message your new bot (send any message)
- Visit
https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates - Find
"chat":{"id":in the response - that's your chat ID
3. Configure Your Credentials
Rename .env.example to .env and fill in your values:
TELEGRAM_TOKEN=your-bot-token-here
TELEGRAM_CHAT_ID=your-chat-id-here
4. Send Messages
from telegram_alert import TelegramAlert
alert = TelegramAlert()
# Async
await alert.send("Hello World!")
# Sync (for simple scripts)
alert.send_sync("Hello World!")
That's it!
Examples
Basic Messages
from telegram_alert import TelegramAlert
alert = TelegramAlert()
await alert.send("Server started successfully")
Silent Messages
Send without notification sound:
await alert.send("Background task complete", silent=True)
Test Your Credentials
Verify your bot token works without sending a message:
alert = TelegramAlert()
if alert.test_sync():
print("Credentials are valid!")
else:
print("Check your .env file")
Multiple Chat IDs
Send to multiple chats at once:
# Option 1: Comma-separated in .env
# TELEGRAM_CHAT_ID=123456,789012,345678
# Option 2: Pass a list
alert = TelegramAlert(chat_id=["123456", "789012"])
await alert.send("Broadcast message") # Sends to all
# Option 3: Send to specific chat
await alert.send_to("999999", "Just for this chat")
Formatted Messages
# HTML formatting
await alert.send("<b>Bold</b> and <i>italic</i>", parse_mode="HTML")
# MarkdownV2 (auto-escaped)
await alert.send("*Bold* and _italic_", parse_mode="MarkdownV2")
Progress Bar
from telegram_alert import progress_bar
msg = f"Download: {progress_bar(75, 100)}"
await alert.send(msg)
# Output: Download: [███████████████░░░░░] 75/100
Context Manager
Reuse connections for better performance:
async with TelegramAlert() as alert:
await alert.send("Message 1")
await alert.send("Message 2") # Reuses connection
Error Handling
from telegram_alert import TelegramAlert, ConfigError, SendError, RateLimitError
try:
alert = TelegramAlert()
await alert.send("Test message")
except ConfigError as e:
print(f"Check your .env file: {e}")
except RateLimitError as e:
print(f"Too many messages, retry after {e.retry_after}s")
except SendError as e:
print(f"Failed to send: {e}")
Sync Usage
For simple scripts that don't use async:
from telegram_alert import TelegramAlert
alert = TelegramAlert()
alert.send_sync("Deployment complete!")
Note:
send_sync()won't work in Jupyter notebooks or async frameworks (FastAPI, etc.) since they already have an event loop running. In those environments, useawait alert.send()directly.
API Reference
TelegramAlert
alert = TelegramAlert(
token="...", # Optional, falls back to TELEGRAM_TOKEN
chat_id="...", # Optional, falls back to TELEGRAM_CHAT_ID (can be list)
rate_limit_delay=1.0, # Seconds between messages
)
# Async methods
await alert.send(message, parse_mode=None, silent=False, ...)
await alert.send_to(chat_id, message, ...)
await alert.test() # Verify credentials
await alert.close() # Close connections
# Sync wrappers
alert.send_sync(message, ...)
alert.send_to_sync(chat_id, message, ...)
alert.test_sync()
# Properties
alert.chat_id # Primary chat ID
alert.chat_ids # All configured chat IDs
Formatters
escape_markdown(text)- Escape MarkdownV2 special characters (preserves emojis)progress_bar(value, max_value, width=20)- Visual progress bartruncate(text, max_length=4096)- Truncate to Telegram's 4096 char limit
Exceptions
ConfigError- Missing or invalid.envconfigurationSendError- Message failed to sendRateLimitError- Too many messages (has.retry_afterseconds)
License
GPL-3.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_telegram_alert-0.1.0.tar.gz.
File metadata
- Download URL: py_telegram_alert-0.1.0.tar.gz
- Upload date:
- Size: 25.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f75bd807cbd75c3749a3a0eb4ed0f50b80fa0fa6e6d0f211105bb813fedbc13b
|
|
| MD5 |
343b87c55c28a75e74c5a85a72d34b31
|
|
| BLAKE2b-256 |
f0aa839c87ad1d4c43a8ebbef135b2d5e13e2af5c7ed78eb8af5523a635820fa
|
Provenance
The following attestation bundles were made for py_telegram_alert-0.1.0.tar.gz:
Publisher:
publish.yml on cainky/telegram_alert
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_telegram_alert-0.1.0.tar.gz -
Subject digest:
f75bd807cbd75c3749a3a0eb4ed0f50b80fa0fa6e6d0f211105bb813fedbc13b - Sigstore transparency entry: 782980978
- Sigstore integration time:
-
Permalink:
cainky/telegram_alert@31fe5303fa8820a3aa3509e7c7b2cf191ced4dd8 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/cainky
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@31fe5303fa8820a3aa3509e7c7b2cf191ced4dd8 -
Trigger Event:
push
-
Statement type:
File details
Details for the file py_telegram_alert-0.1.0-py3-none-any.whl.
File metadata
- Download URL: py_telegram_alert-0.1.0-py3-none-any.whl
- Upload date:
- Size: 21.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e70957d56622d6a59a1e2b300460dcd07f44abc2acb729aabeeba380fd4e23dd
|
|
| MD5 |
471cd821c153eb940fa47317283d88a6
|
|
| BLAKE2b-256 |
07463eb3f7f5515db89985d8549c791b86edf5931932eeb9bf597ba0d70fed23
|
Provenance
The following attestation bundles were made for py_telegram_alert-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on cainky/telegram_alert
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_telegram_alert-0.1.0-py3-none-any.whl -
Subject digest:
e70957d56622d6a59a1e2b300460dcd07f44abc2acb729aabeeba380fd4e23dd - Sigstore transparency entry: 782980991
- Sigstore integration time:
-
Permalink:
cainky/telegram_alert@31fe5303fa8820a3aa3509e7c7b2cf191ced4dd8 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/cainky
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@31fe5303fa8820a3aa3509e7c7b2cf191ced4dd8 -
Trigger Event:
push
-
Statement type: