- Python 99.4%
- Jinja 0.6%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| templates | ||
| tgscheduler | ||
| .gitignore | ||
| config.example.yaml | ||
| pyproject.toml | ||
| README.md | ||
| README.ru.md | ||
| uv.lock | ||
tgscheduler
A terminal-based scheduler for sending messages to Telegram channels, groups, and forum topics via MTProto. Features a TUI dashboard, Jinja2 templates, cron scheduling, and a persistent background daemon.
Features
- TUI interface built with Textual — manage jobs without leaving the terminal
- Jinja2 templates — parameterized messages with date formatting and conditional sections
- Cron scheduling with biweekly support (extended 6-field syntax)
- Forum topics (Telegram supergroup threads) supported
- Background daemon — persists after closing the TUI, reload on
SIGHUP - Send history — every sent message is logged to SQLite with full text
- Job dashboard — press Enter on any job to preview the rendered message and next run time
- Global install via
uv tool install
Requirements
- Python 3.11+
- uv (recommended) or pip
- A Telegram account with API credentials from my.telegram.org
- A Telethon
StringSessionfor your account
Installation
# Clone the repository
git clone https://github.com/your-username/tgscheduler.git
cd tgscheduler
# Install globally (recommended)
uv tool install .
# Or install into a local venv
uv sync
Setup
1. Get Telegram API credentials
Go to my.telegram.org, log in, and create an app. Copy api_id and api_hash.
2. Generate a session string
Run the following once to authenticate and save a session:
from telethon.sync import TelegramClient
from telethon.sessions import StringSession
api_id = 12345678
api_hash = "your_api_hash"
with TelegramClient(StringSession(), api_id, api_hash) as client:
print(client.session.save())
Save the printed string to a file (e.g. keys/mtproto.session).
3. Configure
cp config.example.yaml config.yaml
# Edit config.yaml with your api_id, api_hash, and session_path
The app data directory defaults to ~/.tgscheduler/. You can override it:
export TGSCHEDULER_HOME=/path/to/your/dir
# or pass it per-command:
tgscheduler -C /path/to/your/dir tui
First run automatically creates the directory structure:
~/.tgscheduler/
├── config.yaml ← fill in your credentials
├── keys/
│ └── mtproto.session
├── templates/ ← put your .j2 templates here
├── scheduler.db ← auto-created
└── daemon.log ← auto-created
Usage
tgscheduler tui # open TUI
tgscheduler daemon # start background daemon (foreground, use TUI's S key for background)
tgscheduler list-chats # list chats to find target IDs
tgscheduler send <id> # send a job immediately
TUI keybindings
| Key | Action |
|---|---|
Enter |
Open job dashboard (preview + next run) |
n |
New job |
e |
Edit selected job |
d |
Delete selected job |
r |
Send selected job right now |
s |
Start / stop daemon |
h |
Send history |
l |
Daemon log |
Ctrl+R |
Refresh |
q |
Quit |
Templates
Templates live in the templates/ directory and use Jinja2 syntax.
Available variables:
{{ now }}— currentdatetimeobject{{ now | strftime('%d.%m.%Y') }}— formatted date- Any custom variables you define in the job's JSON vars field
Example — templates/announcement.j2:
📢 Meeting on {{ now | strftime('%d.%m.%Y') }}
{% if location %}📍 {{ location }}{% endif %}
Everyone is welcome!
Job vars (JSON):
{"location": "Main Hall, 2nd floor"}
Cron expressions
Standard 5-field cron is supported, plus an optional 6th field for ISO week number (useful for biweekly schedules):
MIN HOUR DAY MONTH DOW [WEEK]
0 10 * * 1 2/2
│ │ MON └── every even week (biweekly)
└─────┴── 10:00 AM
| Expression | Meaning |
|---|---|
0 9 * * 1-5 |
9:00 AM on weekdays |
0 10 * * 0 |
10:00 AM every Sunday |
0 10 * * 1 2/2 |
10:00 AM every other Monday (even ISO weeks) |
0 10 * * 1 1/2 |
10:00 AM every other Monday (odd ISO weeks) |
Development
git clone https://github.com/your-username/tgscheduler.git
cd tgscheduler
uv sync
# Run from source
uv run tgscheduler tui
# After code changes, reinstall the global tool
uv tool install . --reinstall
License
MIT