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

bots: post on mastodon as well

This commit is contained in:
2024-10-05 14:42:44 +02:00
parent 729f1180b7
commit 5ba3dfc451
9 changed files with 163 additions and 58 deletions

View File

@@ -5,48 +5,89 @@
...
}:
with lib; let
cfg = config.niveum.telegramBots;
cfg = config.niveum.bots;
botService = name: bot:
nameValuePair "telegram-bot-${name}" {
nameValuePair "bot-${name}" {
enable = bot.enable;
startAt = bot.time;
serviceConfig = {
Type = "oneshot";
LoadCredential = "token:${bot.tokenFile}";
LoadCredential = lib.optionals (bot.telegram.enable) [
"telegram-token:${bot.telegram.tokenFile}"
] ++ lib.optionals (bot.mastodon.enable) [
"mastodon-token:${bot.mastodon.tokenFile}"
];
};
wants = ["network-online.target"];
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)}
${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")"
${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)}
''}
fi
'';
};
in {
options.niveum.telegramBots = mkOption {
options.niveum.bots = mkOption {
type = types.attrsOf (types.submodule {
options = {
enable = mkEnableOption "Telegram bot";
enable = mkEnableOption "Mastodon and Telegram bot";
time = mkOption {type = types.str;};
tokenFile = mkOption {type = types.path;};
chatIds = mkOption {
type = types.listOf (types.strMatching "-?[0-9]+|@[A-Za-z0-9]+");
};
command = mkOption {type = types.str;};
parseMode = mkOption {
type = types.nullOr (types.enum ["HTML" "Markdown"]);
default = null;
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 = "botsin.space";
};
};
};
};
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;
};
};
};
};
};
});