nixos/modules/telegram-notifications/default.nix

31 lines
1018 B
Nix
Raw Normal View History

2022-11-04 19:35:57 +01:00
{ custom }: { config, pkgs, ... }:
2022-08-27 16:30:54 +02:00
let
send-to-telegram = pkgs.writeShellScript "send-to-telegram" ''
2022-11-04 13:06:15 +01:00
export $(${pkgs.gnugrep}/bin/grep -v '^#' ${config.age.secrets.telegramNotifyEnv.path} | ${pkgs.findutils}/bin/xargs)
2022-08-27 16:30:54 +02:00
URL="https://api.telegram.org/bot$TELEGRAM_KEY/sendMessage"
${pkgs.curl}/bin/curl -s -d "chat_id=$CHAT_ID&disable_web_page_preview=1&text=$1" $URL > /dev/null'';
unit-status-telegram = pkgs.writeShellScript "unit-status-telegram" ''
2022-08-27 17:16:23 +02:00
UNIT="$1"
UNITSTATUS="$(systemctl status $UNIT)"
ALERT="$(echo -e "\u26A0")"
${send-to-telegram} "$ALERT Unit failed $UNIT $ALERT
2022-08-27 17:16:23 +02:00
Status:
$UNITSTATUS"'';
2022-08-27 16:30:54 +02:00
in
{
2022-11-04 19:35:57 +01:00
age.secrets.telegramNotifyEnv.file = "${custom.inputs.self}/scrts/telegram_notify_env.age";
2022-08-27 17:16:23 +02:00
systemd.services."unit-status-telegram@" = {
description = "Unit Status Telegram Service";
unitConfig = {
After = "network-online.target";
2022-08-27 17:16:23 +02:00
};
serviceConfig = {
Type = "simple";
ExecStart = "${unit-status-telegram} %I";
2022-08-27 17:16:23 +02:00
};
};
2022-08-27 16:30:54 +02:00
}