Python: Get a Telegram Notification When Training Finishes

Python: Get a Telegram Notification When Training Finishes

push.tg

Model 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:])
    raise

Progress 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

Get your endpoint: t.me/trafficRobot?start=tg-python. It takes 30 seconds, it has no ads, and it has been running since 2016.

Report Page