Python: Get a Telegram Notification When Training Finishes
push.tgModel training, a long scrape, a big data migration: you start it, then keep checking the terminal. Have Python message you instead. No packages are needed, only the standard library. The snippets below were tested with Python 3.
Step 1: get your endpoint (30 seconds)
Open @trafficRobot in Telegram and press Start. You get an address like k3x9q@push.tg. Your endpoint is the part before the @: https://push.tg/k3x9q. Every example below uses k3x9q, so swap in your own key.
There's no bot to create, no chat_id to look up and no token to keep safe. Add @trafficRobot to a group and the group gets its own address, so the whole team sees the alerts.
Step 2: a 6-line helper
import urllib.request
def tg(text, url="https://push.tg/k3x9q"):
try:
urllib.request.urlopen(url, data=text.encode(), timeout=10)
except Exception as e:
print("telegram notify failed:", e)The request body is the message. Notifications never crash your job; at worst a failure is printed.
Done, crashed, and progress
import socket, time, traceback
start = time.time()
try:
best = train() # your code
mins = (time.time() - start) / 60
tg(f"✅ training done on {socket.gethostname()} in {mins:.0f} min, best={best}")
except Exception:
tg("❌ training crashed:\n" + traceback.format_exc()[-1500:])
raiseProgress every N epochs:
for epoch in range(epochs):
loss = train_one_epoch()
if epoch % 10 == 0:
tg(f"epoch {epoch}/{epochs} loss={loss:.4f}")No code changes at all
Wrap the command in the shell:
python train.py; echo "train.py finished, exit code $?" | curl -s -m 10 --data-binary @- https://push.tg/k3x9q
In Jupyter, put tg("cell done") at the end of the long cell. On Colab or Kaggle this is handy because you can close the tab and still find out when it's finished.
Tips
- Keep the endpoint in an environment variable (
os.environ["PUSHTG_URL"]) instead of hard-coding it in notebooks you share. - Use a group address to tell the whole lab that the GPU box is free.
Check that the message really arrived. push.tg answers HTTP 200 even when the key is wrong. The body says {"error":"no such user"} instead of {"ok":...}. So a green "test sent" in your tool doesn't prove delivery. Look at Telegram.
More copy-paste guides
- Claude Code, Codex and Cursor: Get a Telegram Message When Your AI Agent Finishes
- Claude Code Telegram Notification Hook
- Uptime Kuma Telegram Alerts Without a Bot Token
- Healthchecks.io Alerts to Telegram
- Proxmox Notifications to Telegram with a Webhook Target
- Home Assistant Telegram Notification Without a Bot
- n8n Error Workflow: Telegram Alert When a Workflow Fails
- Grafana Alerts to Telegram with a Custom Webhook Payload
- Cron Job Failure Alert to Telegram (cron and systemd)
- GitHub Actions and GitLab CI Telegram Notification in One Line
- Synology NAS Email Alerts to Telegram
- Nagios Alerts to Telegram with curl
- How to Receive Email in Telegram Without Giving Out Your Real Address
- Send Server Alerts to Telegram with One curl Command
Get your endpoint: t.me/trafficRobot?start=tg-python. It takes 30 seconds, it has no ads, and it has been running since 2016.