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.0.tar.gz (7.4 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.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tiny_tg-0.1.0.tar.gz
  • Upload date:
  • Size: 7.4 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.0.tar.gz
Algorithm Hash digest
SHA256 efe62a23a823a90e0286a21dbeba5df327e5fd04de164ddb060fcf58b6a17fc0
MD5 35c5a95392cc1b715761497f974b42b4
BLAKE2b-256 f58aaff6f4ed20812a80874f45c51ac6448040a909d07648db5899a2824d5a10

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tiny_tg-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28cd7b23160942b5e0d6a6ff6097b39b257244c636df9d59c74da50ab70fba6d
MD5 d386b9412f4dc32836cabb9846e17a4a
BLAKE2b-256 d739cc4bdd21e172e1cd38068fd4b80949209138d33799b6f7b72ba01814e6ed

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