2022-03-10 21:52:12 +01:00
|
|
|
{
|
|
|
|
|
lib,
|
|
|
|
|
config,
|
|
|
|
|
pkgs,
|
|
|
|
|
...
|
|
|
|
|
}:
|
|
|
|
|
with lib; let
|
2019-04-20 09:08:39 +02:00
|
|
|
cfg = config.niveum.telegramBots;
|
|
|
|
|
|
2020-06-10 17:37:25 +02:00
|
|
|
botService = name: bot:
|
|
|
|
|
nameValuePair "telegram-bot-${name}" {
|
|
|
|
|
enable = bot.enable;
|
|
|
|
|
startAt = bot.time;
|
2023-02-22 10:02:55 +01:00
|
|
|
serviceConfig = {
|
|
|
|
|
Type = "oneshot";
|
|
|
|
|
LoadCredential = "token:${bot.tokenFile}";
|
|
|
|
|
};
|
2022-03-10 21:52:12 +01:00
|
|
|
wants = ["network-online.target"];
|
2023-02-22 10:02:55 +01:00
|
|
|
script = ''
|
|
|
|
|
export TOKEN="$(cat "$CREDENTIALS_DIRECTORY/token")"
|
|
|
|
|
QUOTE=$(${bot.command})
|
|
|
|
|
if [ -n "$QUOTE" ]; then
|
|
|
|
|
echo $QUOTE >&2
|
|
|
|
|
${strings.concatStringsSep "\n" (map (chatId: ''
|
|
|
|
|
${pkgs.curl}/bin/curl -X POST "https://api.telegram.org/bot''${TOKEN}/sendMessage" \
|
|
|
|
|
-d chat_id="${chatId}" \
|
|
|
|
|
-d text="$QUOTE" ${
|
|
|
|
|
lib.strings.optionalString (bot.parseMode != null)
|
|
|
|
|
"-d parse_mode=${bot.parseMode}"
|
|
|
|
|
} | ${pkgs.jq}/bin/jq -e .ok
|
|
|
|
|
'')
|
|
|
|
|
bot.chatIds)}
|
|
|
|
|
fi
|
|
|
|
|
'';
|
2020-06-10 17:37:25 +02:00
|
|
|
};
|
2019-04-20 09:08:39 +02:00
|
|
|
in {
|
|
|
|
|
options.niveum.telegramBots = mkOption {
|
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
|
options = {
|
|
|
|
|
enable = mkEnableOption "Telegram bot";
|
2022-03-10 21:52:12 +01:00
|
|
|
time = mkOption {type = types.str;};
|
2023-02-22 10:02:55 +01:00
|
|
|
tokenFile = mkOption {type = types.path;};
|
2020-06-10 17:37:25 +02:00
|
|
|
chatIds = mkOption {
|
2022-01-05 16:40:34 +01:00
|
|
|
type = types.listOf (types.strMatching "-?[0-9]+|@[A-Za-z0-9]+");
|
2020-06-10 17:37:25 +02:00
|
|
|
};
|
2022-03-10 21:52:12 +01:00
|
|
|
command = mkOption {type = types.str;};
|
2019-04-20 09:08:39 +02:00
|
|
|
parseMode = mkOption {
|
2022-03-10 21:52:12 +01:00
|
|
|
type = types.nullOr (types.enum ["HTML" "Markdown"]);
|
2019-04-20 09:08:39 +02:00
|
|
|
default = null;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
});
|
2022-03-10 21:52:12 +01:00
|
|
|
default = {};
|
2019-04-20 09:08:39 +02:00
|
|
|
};
|
|
|
|
|
|
2022-03-10 21:52:12 +01:00
|
|
|
config = {systemd.services = attrsets.mapAttrs' botService cfg;};
|
2019-04-20 09:08:39 +02:00
|
|
|
}
|