From 5ba3dfc45177c4be84a37214601eff82060e3bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kier=C3=A1n=20Meinhardt?= Date: Sat, 5 Oct 2024 14:42:44 +0200 Subject: [PATCH] bots: post on mastodon as well --- configs/telegram-bots/astrology.nix | 20 +++-- configs/telegram-bots/autorenkalender.nix | 15 +++- configs/telegram-bots/celan.nix | 39 ++++++++-- configs/telegram-bots/hesychius.nix | 15 +++- configs/telegram-bots/literature-quote.nix | 11 ++- configs/telegram-bots/smyth.nix | 15 +++- configs/telegram-bots/tlg-wotd.nix | 19 +++-- modules/telegram-bot.nix | 85 ++++++++++++++++------ secrets | 2 +- 9 files changed, 163 insertions(+), 58 deletions(-) diff --git a/configs/telegram-bots/astrology.nix b/configs/telegram-bots/astrology.nix index fe096ce..6a7eb19 100644 --- a/configs/telegram-bots/astrology.nix +++ b/configs/telegram-bots/astrology.nix @@ -41,18 +41,24 @@ ' ''; in { - niveum.telegramBots.transits = { + niveum.bots.transits = { enable = true; time = "*:0/1"; - tokenFile = config.age.secrets.telegram-token-kmein.path; - chatIds = ["-1001796440545"]; + telegram = { + enable = true; + tokenFile = config.age.secrets.telegram-token-kmein.path; + chatIds = ["-1001796440545"]; + }; command = toString (pkgs.writers.writeDash "common-transits" '' + set -efu + now=$(${pkgs.coreutils}/bin/date +%_H:%M | ${pkgs.gnused}/bin/sed 's/^\s*//') date=$(${pkgs.coreutils}/bin/date +'%m %d %Y') - { - ${pkgs.astrolog}/bin/astrolog -qd $date -zN Berlin -Yt -Yd -d -R Uranus Neptune Pluto "North Node" -A 2 - ${pkgs.astrolog}/bin/astrolog -Yt -Yd -q 10 22 1999 6:32 -zN Kassel -td $date -R Uranus Neptune Pluto "North Node" - } | ${toSymbols} | ${pkgs.coreutils}/bin/sort -n | ${pkgs.gnugrep}/bin/grep "^$now" || : + ( + cd ${pkgs.astrolog}/bin + ./astrolog -qd $date -zN Berlin -Yt -Yd -d -R Uranus Neptune Pluto "North Node" -A 2 + ./astrolog -Yt -Yd -q 10 22 1999 6:32 -zN Kassel -td $date -R Uranus Neptune Pluto "North Node" + ) | ${toSymbols} | ${pkgs.coreutils}/bin/sort -n | ${pkgs.gnugrep}/bin/grep "^$now" || : ''); }; } diff --git a/configs/telegram-bots/autorenkalender.nix b/configs/telegram-bots/autorenkalender.nix index 9505dc0..d6e8322 100644 --- a/configs/telegram-bots/autorenkalender.nix +++ b/configs/telegram-bots/autorenkalender.nix @@ -13,12 +13,19 @@ autorenkalender = pkgs.python3Packages.callPackage autorenkalender-package {}; in { - niveum.telegramBots.autorenkalender = { + niveum.bots.autorenkalender = { enable = true; time = "07:00"; - tokenFile = config.age.secrets.telegram-token-kmein.path; - chatIds = ["@autorenkalender"]; - parseMode = "Markdown"; + mastodon = { + enable = false; + language = "de"; + }; + telegram = { + enable = true; + tokenFile = config.age.secrets.telegram-token-kmein.path; + chatIds = ["@autorenkalender"]; + parseMode = "Markdown"; + }; command = "${autorenkalender}/bin/autorenkalender"; }; diff --git a/configs/telegram-bots/celan.nix b/configs/telegram-bots/celan.nix index 15fd8e6..54596b8 100644 --- a/configs/telegram-bots/celan.nix +++ b/configs/telegram-bots/celan.nix @@ -9,22 +9,49 @@ sha256 = "sha256-nA+EwAH2vkeolsy9AoPLEMt1uGKDZe/aPrS95CZvuus="; }; in { - niveum.telegramBots.celan = { + niveum.bots.celan = { enable = true; time = "08:00"; - tokenFile = config.age.secrets.telegram-token-kmein.path; - chatIds = ["@PaulCelan"]; + telegram = { + enable = true; + tokenFile = config.age.secrets.telegram-token-kmein.path; + chatIds = ["@PaulCelan"]; + }; + mastodon = { + enable = true; + tokenFile = config.age.secrets.mastodon-token-celan.path; + language = "de"; + }; command = toString (pkgs.writers.writeDash "random-celan" '' cd ${celan} poem="$(${pkgs.findutils}/bin/find . -type f | ${pkgs.coreutils}/bin/shuf -n1)" - source="$(${pkgs.coreutils}/bin/dirname "$poem" | ${pkgs.gnused}/bin/sed 's#^\./##;s/[-_]/ /g;s!/! › !g;s/0\([0-9]\+\)/\1/g')" + source="$(${pkgs.coreutils}/bin/dirname "$poem" \ + | ${pkgs.gnused}/bin/sed 's#^\./##;s/[-_]/ /g;s!/! › !g;s/0\([0-9]\+\)/\1/g' \ + | ${pkgs.gnused}/bin/sed 's/Der Sand aus den Urnen/#Der_Sand_aus_den_Urnen (1948)/g' \ + | ${pkgs.gnused}/bin/sed 's/Mohn und Gedächtnis/#Mohn_und_Gedächtnis (1952)/g' \ + | ${pkgs.gnused}/bin/sed 's/Von Schwelle zu Schwelle/#Von_Schwelle_zu_Schwelle (1955)/g' \ + | ${pkgs.gnused}/bin/sed 's/Sprachgitter/#& (1959)/g' \ + | ${pkgs.gnused}/bin/sed 's/Niemandsrose/#& (1963)/g' \ + | ${pkgs.gnused}/bin/sed 's/Atemwende/#& (1967)/g' \ + | ${pkgs.gnused}/bin/sed 's/Fadensonnen/#& (1968)/g' \ + | ${pkgs.gnused}/bin/sed 's/Lichtzwang/#& (1970)/g' \ + | ${pkgs.gnused}/bin/sed 's/Schneepart/#& (1971)/g' \ + | ${pkgs.gnused}/bin/sed 's/Zeitgehöft/#& (1976)/g' \ + | ${pkgs.gnused}/bin/sed 's/Frühwerk/#& (1989)/g' \ + | ${pkgs.gnused}/bin/sed 's/Eingedunkelt/#& (1991)/g' \ + | ${pkgs.gnused}/bin/sed 's/Nachlaß/#& (1997)/g' + )" cat "$poem" echo - printf "Aus: %s\n" "$source" + printf "Aus: %s\n\n#PaulCelan #Celan #Lyrik #poetry" "$source" ''); }; - systemd.timers.telegram-bot-celan.timerConfig.RandomizedDelaySec = "10h"; + age.secrets = { + mastodon-token-celan.file = ../../secrets/mastodon-token-celan.age; + }; + + systemd.timers.bot-celan.timerConfig.RandomizedDelaySec = "10h"; niveum.passport.services = [ { diff --git a/configs/telegram-bots/hesychius.nix b/configs/telegram-bots/hesychius.nix index 53e55ed..feea093 100644 --- a/configs/telegram-bots/hesychius.nix +++ b/configs/telegram-bots/hesychius.nix @@ -7,15 +7,22 @@ }: let hesychius = inputs.scripts.outPath + "/hesychius/hesychius.txt"; in { - niveum.telegramBots.hesychius = { + niveum.bots.hesychius = { enable = true; time = "08:00"; - tokenFile = config.age.secrets.telegram-token-kmein.path; - chatIds = ["@HesychiosAlexandreus"]; + mastodon = { + enable = false; + language = "el"; + }; + telegram = { + enable = true; + tokenFile = config.age.secrets.telegram-token-kmein.path; + chatIds = ["@HesychiosAlexandreus"]; + }; command = "${pkgs.coreutils}/bin/shuf -n1 ${hesychius}"; }; - systemd.timers.telegram-bot-hesychius.timerConfig.RandomizedDelaySec = "10h"; + systemd.timers.bot-hesychius.timerConfig.RandomizedDelaySec = "10h"; niveum.passport.services = [ { diff --git a/configs/telegram-bots/literature-quote.nix b/configs/telegram-bots/literature-quote.nix index a251234..edebdde 100644 --- a/configs/telegram-bots/literature-quote.nix +++ b/configs/telegram-bots/literature-quote.nix @@ -5,13 +5,16 @@ niveumPackages, ... }: { - niveum.telegramBots.quotebot = { + niveum.bots.quotebot = { enable = true; time = "08/6:00"; - tokenFile = config.age.secrets.telegram-token-kmein.path; - chatIds = ["-1001760262519"]; + telegram = { + enable = true; + tokenFile = config.age.secrets.telegram-token-kmein.path; + chatIds = ["-1001760262519"]; + parseMode = "Markdown"; + }; command = "${niveumPackages.literature-quote}/bin/literature-quote"; - parseMode = "Markdown"; }; niveum.passport.services = [ diff --git a/configs/telegram-bots/smyth.nix b/configs/telegram-bots/smyth.nix index 5f1d52d..a7324c2 100644 --- a/configs/telegram-bots/smyth.nix +++ b/configs/telegram-bots/smyth.nix @@ -4,11 +4,18 @@ lib, ... }: { - niveum.telegramBots.smyth = { + niveum.bots.smyth = { enable = true; time = "08:00"; - tokenFile = config.age.secrets.telegram-token-kmein.path; - chatIds = ["@HerbertWeirSmyth"]; + mastodon = { + enable = false; + language = "en"; + }; + telegram = { + enable = true; + tokenFile = config.age.secrets.telegram-token-kmein.path; + chatIds = ["@HerbertWeirSmyth"]; + }; command = toString (pkgs.writers.writeDash "random-smyth" '' set -efu @@ -26,7 +33,7 @@ ''); }; - systemd.timers.telegram-bot-smyth.timerConfig.RandomizedDelaySec = "10h"; + systemd.timers.bot-smyth.timerConfig.RandomizedDelaySec = "10h"; niveum.passport.services = [ { diff --git a/configs/telegram-bots/tlg-wotd.nix b/configs/telegram-bots/tlg-wotd.nix index 237e509..900c9b0 100644 --- a/configs/telegram-bots/tlg-wotd.nix +++ b/configs/telegram-bots/tlg-wotd.nix @@ -5,7 +5,7 @@ niveumPackages, ... }: { - systemd.services.telegram-bot-tlg-wotd = { + systemd.services.bot-tlg-wotd = { enable = true; wants = ["network-online.target"]; startAt = "9:30"; @@ -29,10 +29,16 @@ definition=$(echo "$json_data" | jq -r '.definition | sub("<.*>"; "") | rtrimstr(" ")') first_occurrence=$(echo "$json_data" | jq -r '.firstOccurrence') total_occurrences=$(echo "$json_data" | jq -r '.totalOccurrences') - caption="*$word* ‘$definition’ + telegram_caption="*$word* ‘$definition’ First occurrence (century): $first_occurrence Number of occurrences (in all Ancient Greek texts): $total_occurrences" + mastodon_caption="$word ‘$definition’ + + First occurrence (century): $first_occurrence + Number of occurrences (in all Ancient Greek texts): $total_occurrences" + + #ancientgreek #classics #wotd #wordoftheday transliteration=$(${pkgs.writers.writePython3 "translit.py" { libraries = [ niveumPackages.cltk ]; @@ -105,16 +111,16 @@ -annotate +100+120 "‘$definition’" \ -fill "$color2" \ -pointsize 40 -gravity southwest \ - -annotate +100+60 "t.me/TLGWotD" \ + -annotate +100+60 "attested $total_occurrences times" \ -pointsize 40 -gravity southeast \ -annotate +100+60 "$(date -I)" \ "$photo_path" - curl -X POST "https://api.telegram.org/bot$TOKEN/sendPhoto" \ + curl -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendPhoto" \ -F "chat_id=\"$chat_id\"" \ -F "photo=@$photo_path" \ -F parse_mode=Markdown \ - -F caption="$caption" + -F caption="$telegram_caption" mastodon_upload_response=$(curl -X POST "https://botsin.space/api/v2/media" \ -H "Authorization: Bearer $MASTODON_TOKEN" \ @@ -123,7 +129,8 @@ mastodon_image_id=$(echo $mastodon_upload_response | jq -r .id) curl -X POST "https://botsin.space/api/v1/statuses" \ -H "Authorization: Bearer $MASTODON_TOKEN" \ - -d "status=$caption" \ + -d "status=$mastodon_caption" \ + -d "visibility=public" \ -d "media_ids[]=$mastodon_image_id" ''; serviceConfig = { diff --git a/modules/telegram-bot.nix b/modules/telegram-bot.nix index 9e233b2..a024cbb 100644 --- a/modules/telegram-bot.nix +++ b/modules/telegram-bot.nix @@ -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; + }; + }; + }; }; }; }); diff --git a/secrets b/secrets index c1618c8..c819d01 160000 --- a/secrets +++ b/secrets @@ -1 +1 @@ -Subproject commit c1618c86c3283a15585eb3757a89075a8d4f9a39 +Subproject commit c819d0180bac5a0499f09a4745213f958d3bafb3