Skip to main content

Simple Telegram notification library

Project description

tiny-tg

PyPI version Python License: MIT

A simple, lightweight Python library for sending Telegram notifications. Perfect for automation scripts, server monitoring, and quick alerts.

Features

  • 🚀 Simple - One command to send messages
  • 📦 Tiny - Minimal dependencies (just httpx and python-dotenv)
  • 🔧 Flexible - Use as CLI tool or Python library
  • Fast - Direct API calls, no bloat
  • 🐍 Modern - Python 3.11+ with type hints

Installation

As a Library (for Python projects)

# Using uv (recommended)
uv add tiny-tg

As a CLI Tool (system-wide)

# Using uv (recommended - isolated environment)
uv tool install tiny-tg

After uv tool install, the tg command is available globally without activating a virtual environment.

Setup

  1. Create a Telegram Bot:

    • Message @BotFather on Telegram
    • Send /newbot and follow the prompts
    • Copy your API token
  2. Get Your Chat ID:

    • Message @userinfobot to get your chat ID
    • Or message your bot and visit: https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates
  3. Configure Environment:

Create a .env file in your project root:

TELEGRAM_API_KEY=your_bot_token_here

Usage

Command Line

# Send a message
tg CHAT_ID "Hello from tiny-tg!"

# With custom timeout
tg CHAT_ID "Server is down!" --timeout 30

Python API

from tiny_tg import send_message

# Send a notification
send_message(
    chat_id=123456789,
    text="Deployment complete! ✅"
)

# With custom timeout
send_message(
    chat_id=123456789,
    text="Critical alert!",
    timeout=30
)

Examples

Cron Job Notifications

# Daily reminder at 09:00
0 9 * * * /path/to/venv/bin/tg 123456789 "Daily backup complete"

Script Integration

from tiny_tg import send_message

def backup_database():
    try:
        # ... backup logic ...
        send_message(123456789, "✅ Backup successful")
    except Exception as e:
        send_message(123456789, f"❌ Backup failed: {e}")

Server Monitoring

import psutil
from tiny_tg import send_message

CHAT_ID = 123456789

# Check disk space
disk = psutil.disk_usage('/')
if disk.percent > 90:
    send_message(CHAT_ID, f"⚠️ Disk usage: {disk.percent}%")

Raspberry Pi Alerts

from tiny_tg import send_message
import subprocess

def check_temperature():
    temp = subprocess.check_output(['vcgencmd', 'measure_temp'])
    temp_c = float(temp.decode().split('=')[1].split("'")[0])
    
    if temp_c > 70:
        send_message(123456789, f"🌡️ High temp: {temp_c}°C")

check_temperature()

Project Structure

tiny-tg/
├── tiny_tg/
│   ├── __init__.py      # Package exports
│   ├── telegram.py      # Core messaging logic
│   ├── utils.py         # Config utilities
│   └── cli.py           # Command-line interface
├── pyproject.toml       # Project configuration
├── .env                 # API credentials (gitignored)
└── README.md

Configuration

Environment Variables

Variable Required Description
TELEGRAM_API_KEY Yes Your Telegram bot token from @BotFather

Function Parameters

send_message(chat_id, text, timeout=10)

Parameter Type Default Description
chat_id int | str - Telegram chat ID or username
text str - Message text to send
timeout int 10 Request timeout in seconds

Returns: bool - True if successful, False otherwise

Raises: httpx.RequestError - If the API request fails

Requirements

  • Python 3.11+
  • httpx - Modern HTTP client
  • python-dotenv - Environment variable management

License

MIT License - see LICENSE file for details.

Contributing

Contributions welcome! Please feel free to submit a Pull Request.

Links


Keep it simple.
Make it happen.

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

tiny_tg-0.1.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

tiny_tg-0.1.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file tiny_tg-0.1.1.tar.gz.

File metadata

  • Download URL: tiny_tg-0.1.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tiny_tg-0.1.1.tar.gz
Algorithm Hash digest
SHA256 879fc93949ffc100bb70f072c61addebfc01f20e3ee39194dfc7ffb8e1120a36
MD5 9251d3e9b15046ef55ce48d68806b0b5
BLAKE2b-256 88f08293d9e46b6a5deeb62ff63da054345f5944d032c4d618d0d55e8928be36

See more details on using hashes here.

File details

Details for the file tiny_tg-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tiny_tg-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tiny_tg-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dbcd090a625e4da6f6f503899bf61b9624817b9ca321c24310605d048996c42f
MD5 c56f9e80dc6dfeb9ecec48df6c9c7767
BLAKE2b-256 659b4b5789f7ac5bdd043ea3bc396a8670e3f0f4349572a78dc969c7df929949

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