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

@@ -41,18 +41,24 @@
' '
''; '';
in { in {
niveum.telegramBots.transits = { niveum.bots.transits = {
enable = true; enable = true;
time = "*:0/1"; time = "*:0/1";
tokenFile = config.age.secrets.telegram-token-kmein.path; telegram = {
chatIds = ["-1001796440545"]; enable = true;
tokenFile = config.age.secrets.telegram-token-kmein.path;
chatIds = ["-1001796440545"];
};
command = toString (pkgs.writers.writeDash "common-transits" '' command = toString (pkgs.writers.writeDash "common-transits" ''
set -efu
now=$(${pkgs.coreutils}/bin/date +%_H:%M | ${pkgs.gnused}/bin/sed 's/^\s*//') now=$(${pkgs.coreutils}/bin/date +%_H:%M | ${pkgs.gnused}/bin/sed 's/^\s*//')
date=$(${pkgs.coreutils}/bin/date +'%m %d %Y') 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 cd ${pkgs.astrolog}/bin
${pkgs.astrolog}/bin/astrolog -Yt -Yd -q 10 22 1999 6:32 -zN Kassel -td $date -R Uranus Neptune Pluto "North Node" ./astrolog -qd $date -zN Berlin -Yt -Yd -d -R Uranus Neptune Pluto "North Node" -A 2
} | ${toSymbols} | ${pkgs.coreutils}/bin/sort -n | ${pkgs.gnugrep}/bin/grep "^$now" || : ./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" || :
''); '');
}; };
} }

View File

@@ -13,12 +13,19 @@
autorenkalender = autorenkalender =
pkgs.python3Packages.callPackage autorenkalender-package {}; pkgs.python3Packages.callPackage autorenkalender-package {};
in { in {
niveum.telegramBots.autorenkalender = { niveum.bots.autorenkalender = {
enable = true; enable = true;
time = "07:00"; time = "07:00";
tokenFile = config.age.secrets.telegram-token-kmein.path; mastodon = {
chatIds = ["@autorenkalender"]; enable = false;
parseMode = "Markdown"; language = "de";
};
telegram = {
enable = true;
tokenFile = config.age.secrets.telegram-token-kmein.path;
chatIds = ["@autorenkalender"];
parseMode = "Markdown";
};
command = "${autorenkalender}/bin/autorenkalender"; command = "${autorenkalender}/bin/autorenkalender";
}; };

View File

@@ -9,22 +9,49 @@
sha256 = "sha256-nA+EwAH2vkeolsy9AoPLEMt1uGKDZe/aPrS95CZvuus="; sha256 = "sha256-nA+EwAH2vkeolsy9AoPLEMt1uGKDZe/aPrS95CZvuus=";
}; };
in { in {
niveum.telegramBots.celan = { niveum.bots.celan = {
enable = true; enable = true;
time = "08:00"; time = "08:00";
tokenFile = config.age.secrets.telegram-token-kmein.path; telegram = {
chatIds = ["@PaulCelan"]; 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" '' command = toString (pkgs.writers.writeDash "random-celan" ''
cd ${celan} cd ${celan}
poem="$(${pkgs.findutils}/bin/find . -type f | ${pkgs.coreutils}/bin/shuf -n1)" 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" cat "$poem"
echo 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 = [ niveum.passport.services = [
{ {

View File

@@ -7,15 +7,22 @@
}: let }: let
hesychius = inputs.scripts.outPath + "/hesychius/hesychius.txt"; hesychius = inputs.scripts.outPath + "/hesychius/hesychius.txt";
in { in {
niveum.telegramBots.hesychius = { niveum.bots.hesychius = {
enable = true; enable = true;
time = "08:00"; time = "08:00";
tokenFile = config.age.secrets.telegram-token-kmein.path; mastodon = {
chatIds = ["@HesychiosAlexandreus"]; enable = false;
language = "el";
};
telegram = {
enable = true;
tokenFile = config.age.secrets.telegram-token-kmein.path;
chatIds = ["@HesychiosAlexandreus"];
};
command = "${pkgs.coreutils}/bin/shuf -n1 ${hesychius}"; 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 = [ niveum.passport.services = [
{ {

View File

@@ -5,13 +5,16 @@
niveumPackages, niveumPackages,
... ...
}: { }: {
niveum.telegramBots.quotebot = { niveum.bots.quotebot = {
enable = true; enable = true;
time = "08/6:00"; time = "08/6:00";
tokenFile = config.age.secrets.telegram-token-kmein.path; telegram = {
chatIds = ["-1001760262519"]; enable = true;
tokenFile = config.age.secrets.telegram-token-kmein.path;
chatIds = ["-1001760262519"];
parseMode = "Markdown";
};
command = "${niveumPackages.literature-quote}/bin/literature-quote"; command = "${niveumPackages.literature-quote}/bin/literature-quote";
parseMode = "Markdown";
}; };
niveum.passport.services = [ niveum.passport.services = [

View File

@@ -4,11 +4,18 @@
lib, lib,
... ...
}: { }: {
niveum.telegramBots.smyth = { niveum.bots.smyth = {
enable = true; enable = true;
time = "08:00"; time = "08:00";
tokenFile = config.age.secrets.telegram-token-kmein.path; mastodon = {
chatIds = ["@HerbertWeirSmyth"]; enable = false;
language = "en";
};
telegram = {
enable = true;
tokenFile = config.age.secrets.telegram-token-kmein.path;
chatIds = ["@HerbertWeirSmyth"];
};
command = toString (pkgs.writers.writeDash "random-smyth" '' command = toString (pkgs.writers.writeDash "random-smyth" ''
set -efu set -efu
@@ -26,7 +33,7 @@
''); '');
}; };
systemd.timers.telegram-bot-smyth.timerConfig.RandomizedDelaySec = "10h"; systemd.timers.bot-smyth.timerConfig.RandomizedDelaySec = "10h";
niveum.passport.services = [ niveum.passport.services = [
{ {

View File

@@ -5,7 +5,7 @@
niveumPackages, niveumPackages,
... ...
}: { }: {
systemd.services.telegram-bot-tlg-wotd = { systemd.services.bot-tlg-wotd = {
enable = true; enable = true;
wants = ["network-online.target"]; wants = ["network-online.target"];
startAt = "9:30"; startAt = "9:30";
@@ -29,10 +29,16 @@
definition=$(echo "$json_data" | jq -r '.definition | sub("<.*>"; "") | rtrimstr(" ")') definition=$(echo "$json_data" | jq -r '.definition | sub("<.*>"; "") | rtrimstr(" ")')
first_occurrence=$(echo "$json_data" | jq -r '.firstOccurrence') first_occurrence=$(echo "$json_data" | jq -r '.firstOccurrence')
total_occurrences=$(echo "$json_data" | jq -r '.totalOccurrences') total_occurrences=$(echo "$json_data" | jq -r '.totalOccurrences')
caption="*$word* $definition telegram_caption="*$word* $definition
First occurrence (century): $first_occurrence First occurrence (century): $first_occurrence
Number of occurrences (in all Ancient Greek texts): $total_occurrences" 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" { transliteration=$(${pkgs.writers.writePython3 "translit.py" {
libraries = [ niveumPackages.cltk ]; libraries = [ niveumPackages.cltk ];
@@ -105,16 +111,16 @@
-annotate +100+120 "$definition" \ -annotate +100+120 "$definition" \
-fill "$color2" \ -fill "$color2" \
-pointsize 40 -gravity southwest \ -pointsize 40 -gravity southwest \
-annotate +100+60 "t.me/TLGWotD" \ -annotate +100+60 "attested $total_occurrences times" \
-pointsize 40 -gravity southeast \ -pointsize 40 -gravity southeast \
-annotate +100+60 "$(date -I)" \ -annotate +100+60 "$(date -I)" \
"$photo_path" "$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 "chat_id=\"$chat_id\"" \
-F "photo=@$photo_path" \ -F "photo=@$photo_path" \
-F parse_mode=Markdown \ -F parse_mode=Markdown \
-F caption="$caption" -F caption="$telegram_caption"
mastodon_upload_response=$(curl -X POST "https://botsin.space/api/v2/media" \ mastodon_upload_response=$(curl -X POST "https://botsin.space/api/v2/media" \
-H "Authorization: Bearer $MASTODON_TOKEN" \ -H "Authorization: Bearer $MASTODON_TOKEN" \
@@ -123,7 +129,8 @@
mastodon_image_id=$(echo $mastodon_upload_response | jq -r .id) mastodon_image_id=$(echo $mastodon_upload_response | jq -r .id)
curl -X POST "https://botsin.space/api/v1/statuses" \ curl -X POST "https://botsin.space/api/v1/statuses" \
-H "Authorization: Bearer $MASTODON_TOKEN" \ -H "Authorization: Bearer $MASTODON_TOKEN" \
-d "status=$caption" \ -d "status=$mastodon_caption" \
-d "visibility=public" \
-d "media_ids[]=$mastodon_image_id" -d "media_ids[]=$mastodon_image_id"
''; '';
serviceConfig = { serviceConfig = {

View File

@@ -5,48 +5,89 @@
... ...
}: }:
with lib; let with lib; let
cfg = config.niveum.telegramBots; cfg = config.niveum.bots;
botService = name: bot: botService = name: bot:
nameValuePair "telegram-bot-${name}" { nameValuePair "bot-${name}" {
enable = bot.enable; enable = bot.enable;
startAt = bot.time; startAt = bot.time;
serviceConfig = { serviceConfig = {
Type = "oneshot"; 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"]; wants = ["network-online.target"];
script = '' script = ''
export TOKEN="$(cat "$CREDENTIALS_DIRECTORY/token")"
QUOTE=$(${bot.command}) QUOTE=$(${bot.command})
if [ -n "$QUOTE" ]; then if [ -n "$QUOTE" ]; then
echo $QUOTE >&2 echo $QUOTE >&2
${strings.concatStringsSep "\n" (map (chatId: ''
${pkgs.curl}/bin/curl -X POST "https://api.telegram.org/bot''${TOKEN}/sendMessage" \ ${lib.optionalString (bot.mastodon.enable) ''
-d chat_id="${chatId}" \ export MASTODON_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/mastodon-token")"
-d text="$QUOTE" ${ ${pkgs.curl}/bin/curl -X POST "https://${bot.mastodon.homeserver}/api/v1/statuses" \
lib.strings.optionalString (bot.parseMode != null) -H "Authorization: Bearer $MASTODON_TOKEN" \
"-d parse_mode=${bot.parseMode}" -d status="$QUOTE" \
} | ${pkgs.jq}/bin/jq -e .ok -d "language=${bot.mastodon.language}" \
'') -d "visibility=public"
bot.chatIds)} ''}
${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 fi
''; '';
}; };
in { in {
options.niveum.telegramBots = mkOption { options.niveum.bots = mkOption {
type = types.attrsOf (types.submodule { type = types.attrsOf (types.submodule {
options = { options = {
enable = mkEnableOption "Telegram bot"; enable = mkEnableOption "Mastodon and Telegram bot";
time = mkOption {type = types.str;}; 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;}; command = mkOption {type = types.str;};
parseMode = mkOption { mastodon = mkOption {
type = types.nullOr (types.enum ["HTML" "Markdown"]); default = {};
default = null; 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;
};
};
};
}; };
}; };
}); });

Submodule secrets updated: c1618c86c3...c819d0180b