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

146 lines
4.7 KiB
Nix
Raw Normal View History

2022-03-10 21:52:12 +01:00
{
lib,
config,
pkgs,
...
}:
2025-12-27 22:22:54 +01:00
with lib;
let
2024-10-05 14:42:44 +02:00
cfg = config.niveum.bots;
2019-04-20 09:08:39 +02:00
2025-12-27 22:22:54 +01:00
botService =
name: bot:
2024-10-05 14:42:44 +02:00
nameValuePair "bot-${name}" {
2020-06-10 17:37:25 +02:00
enable = bot.enable;
startAt = bot.time;
serviceConfig = {
Type = "oneshot";
2025-12-27 22:22:54 +01:00
LoadCredential =
lib.optionals (bot.telegram.enable) [
"telegram-token:${bot.telegram.tokenFile}"
]
++ lib.optionals (bot.mastodon.enable) [
"mastodon-token:${bot.mastodon.tokenFile}"
]
++ lib.optionals (bot.matrix.enable) [
"matrix-token:${bot.matrix.tokenFile}"
];
};
2025-12-27 22:22:54 +01:00
wants = [ "network-online.target" ];
script = ''
QUOTE=$(${bot.command})
if [ -n "$QUOTE" ]; then
echo $QUOTE >&2
2024-10-05 14:42:44 +02:00
2024-10-08 22:35:41 +02:00
${lib.optionalString (bot.matrix.enable) ''
export MATRIX_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/matrix-token")"
export JSON_PAYLOAD=$(${pkgs.jq}/bin/jq -n --arg msgtype "m.text" --arg body "$QUOTE" '{msgtype: $msgtype, body: $body}')
2025-12-27 22:22:54 +01:00
${strings.concatStringsSep "\n" (
map (chatId: ''
${pkgs.curl}/bin/curl -X POST "https://${bot.matrix.homeserver}/_matrix/client/r0/rooms/${chatId}/send/m.room.message" \
-d "$JSON_PAYLOAD" \
-H "Authorization: Bearer $MATRIX_TOKEN" \
-H "Content-Type: application/json"
'') bot.matrix.chatIds
)}
2024-10-08 22:35:41 +02:00
''}
2024-10-05 14:42:44 +02:00
${lib.optionalString (bot.mastodon.enable) ''
export MASTODON_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/mastodon-token")"
${pkgs.curl}/bin/curl -X POST "https://${bot.mastodon.homeserver}/api/v1/statuses" \
-H "Authorization: Bearer $MASTODON_TOKEN" \
-d status="$QUOTE" \
-d "language=${bot.mastodon.language}" \
-d "visibility=public"
''}
${lib.optionalString (bot.telegram.enable) ''
export TELEGRAM_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/telegram-token")"
2025-12-27 22:22:54 +01:00
${strings.concatStringsSep "\n" (
map (chatId: ''
${pkgs.curl}/bin/curl -X POST "https://api.telegram.org/bot''${TELEGRAM_TOKEN}/sendMessage" \
-d chat_id="${chatId}" \
-d text="$QUOTE" ${
lib.strings.optionalString (
bot.telegram.parseMode != null
) "-d parse_mode=${bot.telegram.parseMode}"
} | ${pkgs.jq}/bin/jq -e .ok
'') bot.telegram.chatIds
)}
2024-10-05 14:42:44 +02:00
''}
fi
'';
2020-06-10 17:37:25 +02:00
};
2025-12-27 22:22:54 +01:00
in
{
2024-10-05 14:42:44 +02:00
options.niveum.bots = mkOption {
2025-12-27 22:22:54 +01:00
type = types.attrsOf (
types.submodule {
options = {
enable = mkEnableOption "Mastodon and Telegram bot";
time = mkOption { type = types.str; };
command = mkOption { type = types.str; };
matrix = mkOption {
default = { };
type = types.submodule {
options = {
enable = mkEnableOption "Posting to Matrix";
tokenFile = mkOption { type = types.path; };
homeserver = mkOption {
type = types.str;
};
chatIds = mkOption {
type = types.listOf types.str;
};
2024-10-08 22:35:41 +02:00
};
};
};
2025-12-27 22:22:54 +01:00
mastodon = mkOption {
default = { };
type = types.submodule {
options = {
enable = mkEnableOption "Posting to Mastodon";
language = mkOption {
type = types.str;
default = "en";
};
tokenFile = mkOption { type = types.path; };
homeserver = mkOption {
type = types.str;
default = "social.krebsco.de";
};
2024-10-05 14:42:44 +02:00
};
};
};
2025-12-27 22:22:54 +01:00
telegram = mkOption {
default = { };
type = types.submodule {
options = {
enable = mkEnableOption "Posting to Telegram";
tokenFile = mkOption { type = types.path; };
chatIds = mkOption {
type = types.listOf (types.strMatching "-?[0-9]+|@[A-Za-z0-9]+");
};
parseMode = mkOption {
type = types.nullOr (
types.enum [
"HTML"
"Markdown"
]
);
default = null;
};
2024-10-05 14:42:44 +02:00
};
};
};
2019-04-20 09:08:39 +02:00
};
2025-12-27 22:22:54 +01:00
}
);
default = { };
2019-04-20 09:08:39 +02:00
};
2025-12-27 22:22:54 +01:00
config = {
systemd.services = attrsets.mapAttrs' botService cfg;
};
2019-04-20 09:08:39 +02:00
}