1
0
mirror of https://github.com/kmein/niveum synced 2026-03-16 18:21:07 +01:00
Files
niveum/modules/telegram-bot.nix

44 lines
1.4 KiB
Nix
Raw Permalink Normal View History

2019-04-20 09:08:39 +02:00
{ lib, config, pkgs, ... }:
with lib;
let
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;
serviceConfig.Type = "oneshot";
wants = [ "network-online.target" ];
script = strings.concatStringsSep "\n" ([ "QUOTE=$(${bot.command})" ]
++ map (chatId: ''
${pkgs.curl}/bin/curl -s -X POST "https://api.telegram.org/bot${bot.token}/sendMessage" \
-d chat_id="${chatId}" \
-d text="$QUOTE" ${
lib.strings.optionalString (bot.parseMode != null)
"-d parse_mode=${bot.parseMode}"
}
'') bot.chatIds);
};
2019-04-20 09:08:39 +02:00
in {
options.niveum.telegramBots = mkOption {
type = types.attrsOf (types.submodule {
options = {
enable = mkEnableOption "Telegram bot";
time = mkOption { type = types.str; };
token = mkOption { type = types.strMatching "[0-9A-Za-z:-]+"; };
2020-06-10 17:37:25 +02:00
chatIds = mkOption {
type = types.listOf (types.strMatching "[0-9]+|@[A-Za-z0-9]+");
};
2019-04-20 09:08:39 +02:00
command = mkOption { type = types.str; };
parseMode = mkOption {
2020-06-10 17:37:25 +02:00
type = types.nullOr (types.enum [ "HTML" "Markdown" ]);
2019-04-20 09:08:39 +02:00
default = null;
};
};
});
2020-06-10 17:37:25 +02:00
default = { };
2019-04-20 09:08:39 +02:00
};
2020-06-10 17:37:25 +02:00
config = { systemd.services = attrsets.mapAttrs' botService cfg; };
2019-04-20 09:08:39 +02:00
}