NUT UPS Power Alerts to Telegram Without a Bot Token

NUT UPS Power Alerts to Telegram Without a Bot Token

push.tg

Network UPS Tools (NUT) knows the moment your UPS switches to battery, runs low or comes back on mains power. upsmon can run a program for each of those events, so a two-line script with curl gets you a Telegram message every time the power blinks. No BotFather, no bot token, no chat ID.

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 everyone on call sees the messages.

Step 2: the notify script

Save this as /usr/local/bin/pushtg-nut and make it executable (sudo chmod +x /usr/local/bin/pushtg-nut):

#!/bin/sh
curl -s -o /dev/null -m 10 --data-urlencode "data=$(hostname): $NOTIFYTYPE - $1" https://push.tg/k3x9q

upsmon calls the script with the ready-made message (like UPS myups@localhost on battery) as its last argument, $1, and sets NOTIFYTYPE (ONBATT, ONLINE, LOWBATT and so on) and UPSNAME in the environment. --data-urlencode "data=..." sends the text in the form field push.tg reads, safely encoded.

Step 3: wire it into upsmon.conf

In upsmon.conf (usually /etc/nut/upsmon.conf), set the command and add EXEC to the events you care about:

NOTIFYCMD /usr/local/bin/pushtg-nut

NOTIFYFLAG ONBATT   SYSLOG+WALL+EXEC
NOTIFYFLAG ONLINE   SYSLOG+WALL+EXEC
NOTIFYFLAG LOWBATT  SYSLOG+WALL+EXEC
NOTIFYFLAG FSD      SYSLOG+WALL+EXEC
NOTIFYFLAG COMMBAD  SYSLOG+WALL+EXEC
NOTIFYFLAG COMMOK   SYSLOG+WALL+EXEC
NOTIFYFLAG REPLBATT SYSLOG+WALL+EXEC
NOTIFYFLAG NOCOMM   SYSLOG+WALL+EXEC

upsmon only runs NOTIFYCMD for events that have EXEC in their NOTIFYFLAG. SYSLOG+WALL keeps the usual log line and terminal broadcast.

Want your own wording? NOTIFYMSG overrides the stock text, and %s becomes the UPS name:

NOTIFYMSG ONBATT "Power cut: %s is on battery"

Then reload upsmon:

sudo upsmon -c reload

Step 4: test it

Run the script by hand the way upsmon would, as the user upsmon runs as (often nut):

sudo -u nut env NOTIFYTYPE=ONBATT UPSNAME=myups /usr/local/bin/pushtg-nut 'UPS myups on battery (test)'

For a real end-to-end test, pull the UPS plug from the wall for a few seconds: you should get ONBATT, then ONLINE. If nothing arrives, check the network with:

curl -d "test from $(hostname)" https://push.tg/k3x9q

Good to know

upsmon starts the script in the background, so a slow network never holds up a shutdown. It also means several messages can go out at once when a lot happens together, such as ONBATT, LOWBATT and FSD during a long outage.

Tip: point this at a group. Each Telegram chat or group gets its own push.tg address, so everyone who looks after the rack sees the power warnings.

Check that the message really arrived. push.tg answers HTTP 200 even when the key is wrong; the body then says {"error":"no such user"}. Send one test by hand and look at Telegram before you trust it:

Official references: upsmon.conf(5) (NOTIFYCMD, NOTIFYMSG, NOTIFYFLAG) and upsmon(8) (NOTIFYCMD environment, -c reload).

Open @trafficRobot · push.tg

More copy-paste guides

Report Page