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

42 lines
1.3 KiB
Nix
Raw Normal View History

2019-04-20 09:08:39 +02:00
{ lib, config, pkgs, ... }:
with lib;
let
cfg = config.niveum.telegramBots;
botService = name: bot: nameValuePair "telegram-bot-${name}" {
enable = bot.enable;
startAt = bot.time;
serviceConfig.Type = "oneshot";
wants = [ "network-online.target" ];
2019-05-01 15:44:04 +02:00
script = strings.concatStringsSep "\n" (["QUOTE=$(${bot.command})"] ++ map (chatId: ''
2019-04-20 09:08:39 +02:00
${pkgs.curl}/bin/curl -s -X POST "https://api.telegram.org/bot${bot.token}/sendMessage" \
2019-04-21 15:22:40 +02:00
-d chat_id="${chatId}" \
2019-05-01 15:44:04 +02:00
-d text="$QUOTE" ${
2019-04-20 09:08:39 +02:00
if bot.parseMode == null then ""
else "-d parse_mode=${bot.parseMode}"
}
2019-04-21 15:22:40 +02:00
'') 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:-]+"; };
2019-04-21 15:22:40 +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 {
type = types.nullOr (types.enum ["HTML" "Markdown"]);
default = null;
};
};
});
default = {};
};
config = {
systemd.services = attrsets.mapAttrs' botService cfg;
};
}