maxwell/custom/packages/maxwell-notify.nix
2022-10-21 17:31:18 +02:00

36 lines
739 B
Nix

{ lib
, writers
, curl
, jq
, homeserver
, roomId
, authToken
}:
writers.writeDashBin "notify" ''
export PATH="$PATH:${lib.makeBinPath [ curl jq ]}"
if test $(id -u) != 0; then
echo 'you must be root to send a notice'
exit 1
fi
token=$(cat ${authToken})
url="${homeserver}/rooms/${roomId}/send/m.room.message?access_token=$token"
if test $# -eq 1; then
# send first arg as text
msg=$(printf "%s" "$1" | jq -Rsc '{ "msgtype": "m.text", "body": . }')
else
# send stdin formatted as code
msg=$(jq -Rsc '{
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"body": "",
"formatted_body": ("<pre><code>" + . + "</code></pre>")
}')
fi
curl -s "$url" -d "$msg"
''