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

rename bot structure

This commit is contained in:
2024-10-08 18:51:43 +02:00
parent e26b8408f6
commit a28fca11a7
10 changed files with 4 additions and 6 deletions

View File

@@ -0,0 +1,44 @@
{
pkgs,
lib,
config,
...
}: let
autorenkalender-package = pkgs.fetchFromGitHub {
owner = "kmein";
repo = "autorenkalender";
rev = "cf49a7b057301332d980eb47042a626add93db66";
sha256 = "1pa7sjg33vdnjianrqldv445jdzzv3mn231ljk1j58hs0cd505gs";
};
autorenkalender =
pkgs.python3Packages.callPackage autorenkalender-package {};
in {
niveum.bots.autorenkalender = {
enable = true;
time = "07:00";
mastodon = {
enable = false;
language = "de";
tokenFile = config.age.secrets.mastodon-token-autorenkalender.path;
};
telegram = {
enable = true;
tokenFile = config.age.secrets.telegram-token-kmein.path;
chatIds = ["@autorenkalender"];
parseMode = "Markdown";
};
command = "${autorenkalender}/bin/autorenkalender";
};
age.secrets = {
mastodon-token-autorenkalender.file = ../../secrets/mastodon-token-autorenkalender.age;
};
niveum.passport.services = [
{
title = "Autorenkalender";
description = "sends <a href=\"https://www.projekt-gutenberg.org/\">Projekt Gutenberg</a>'s anniversary information to Telegram.";
link = "https://t.me/Autorenkalender";
}
];
}

63
configs/bots/celan.nix Normal file
View File

@@ -0,0 +1,63 @@
{
pkgs,
lib,
config,
...
}: let
celan = pkgs.fetchzip {
url = "http://c.krebsco.de/celan.tar.gz";
sha256 = "sha256-nA+EwAH2vkeolsy9AoPLEMt1uGKDZe/aPrS95CZvuus=";
};
in {
niveum.bots.celan = {
enable = true;
time = "08:00";
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' \
| ${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\n#PaulCelan #Celan #Lyrik #poetry" "$source"
'');
};
age.secrets = {
mastodon-token-celan.file = ../../secrets/mastodon-token-celan.age;
};
systemd.timers.bot-celan.timerConfig.RandomizedDelaySec = "10h";
niveum.passport.services = [
{
title = "Paul Celan Bot";
description = "sends a random poem by Paul Celan to Telegram.";
link = "https://t.me/PaulCelan";
}
];
}

103
configs/bots/default.nix Normal file
View File

@@ -0,0 +1,103 @@
{
config,
pkgs,
lib,
inputs,
...
}: let
telebots = inputs.telebots.defaultPackage.x86_64-linux;
reverseDirectory = "/run/telegram-reverse";
proverbDirectory = "/run/telegram-proverb";
inherit (import ../../lib) tmpfilesConfig;
in {
imports = [
./logotheca.nix
./transits.nix
./autorenkalender.nix
./hesychius.nix
./smyth.nix
./nachtischsatan.nix
./tlg-wotd.nix
./celan.nix
];
systemd.tmpfiles.rules = map (path:
tmpfilesConfig {
type = "d";
mode = "0750";
age = "1h";
inherit path;
}) [reverseDirectory proverbDirectory];
niveum.passport.services = [
{
title = "Rückwarts-Bot";
link = "https://t.me/RueckwaertsBot";
description = "reverses things on Telegram.";
}
{
title = "BetaCode-Bot";
link = "https://t.me/BetaCodeBot";
description = "converts <a href=\"https://en.wikipedia.org/wiki/Beta_Code\">beta code</a> to polytonic Greek on Telegram.";
}
{
title = "Sprichwortgenerator-Bot";
link = "https://t.me/SprichwortGeneratorBot";
description = "generates useless German proverbs with optional stock photo background on Telegram.";
}
];
age.secrets = {
telegram-token-reverse.file = ../../secrets/telegram-token-reverse.age;
telegram-token-betacode.file = ../../secrets/telegram-token-betacode.age;
telegram-token-proverb.file = ../../secrets/telegram-token-proverb.age;
telegram-token-streaming-link.file = ../../secrets/telegram-token-streaming-link.age;
};
systemd.services.telegram-reverse = {
wantedBy = ["multi-user.target"];
description = "Telegram reverse bot";
path = [pkgs.ffmpeg];
enable = true;
script = ''
TELEGRAM_BOT_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/token")" ${telebots}/bin/telegram-reverse
'';
serviceConfig.Restart = "always";
serviceConfig.WorkingDirectory = reverseDirectory;
serviceConfig.LoadCredential = "token:${config.age.secrets.telegram-token-reverse.path}";
};
systemd.services.telegram-streaming-link = {
wantedBy = ["multi-user.target"];
description = "Telegram bot converting YouTube Music <-> Spotify";
enable = true;
script = ''
TELEGRAM_BOT_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/token")" ${telebots}/bin/telegram-streaming-link
'';
serviceConfig.Restart = "always";
serviceConfig.LoadCredential = "token:${config.age.secrets.telegram-token-streaming-link.path}";
};
systemd.services.telegram-betacode = {
wantedBy = ["multi-user.target"];
description = "Telegram beta code bot";
enable = true;
script = ''
TELEGRAM_BOT_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/token")" ${telebots}/bin/telegram-betacode
'';
serviceConfig.Restart = "always";
serviceConfig.LoadCredential = "token:${config.age.secrets.telegram-token-betacode.path}";
};
systemd.services.telegram-proverb = {
wantedBy = ["multi-user.target"];
description = "Telegram proverb bot";
enable = true;
script = ''
TELEGRAM_BOT_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/token")" ${telebots}/bin/telegram-proverb
'';
serviceConfig.Restart = "always";
serviceConfig.WorkingDirectory = proverbDirectory;
serviceConfig.LoadCredential = "token:${config.age.secrets.telegram-token-proverb.path}";
};
}

View File

@@ -0,0 +1,39 @@
{
pkgs,
config,
inputs,
lib,
...
}: let
hesychius = inputs.scripts.outPath + "/hesychius/hesychius.txt";
in {
niveum.bots.hesychius = {
enable = true;
time = "08:00";
mastodon = {
enable = true;
language = "el";
tokenFile = config.age.secrets.mastodon-token-hesychius.path;
};
telegram = {
enable = true;
tokenFile = config.age.secrets.telegram-token-kmein.path;
chatIds = ["@HesychiosAlexandreus"];
};
command = "${pkgs.coreutils}/bin/shuf -n1 ${hesychius}";
};
systemd.timers.bot-hesychius.timerConfig.RandomizedDelaySec = "10h";
age.secrets = {
mastodon-token-hesychius.file = ../../secrets/mastodon-token-hesychius.age;
};
niveum.passport.services = [
{
title = "Hesychius of Alexandria Bot";
description = "sends a random word from Hesychius of Alexandria's lexicon to Telegram.";
link = "https://t.me/HesychiosAlexandreus";
}
];
}

View File

@@ -0,0 +1,36 @@
{
pkgs,
config,
lib,
niveumPackages,
...
}: {
niveum.bots.logotheca = {
enable = true;
time = "08/6:00";
telegram = {
enable = true;
tokenFile = config.age.secrets.telegram-token-kmein.path;
chatIds = ["-1001760262519"];
parseMode = "Markdown";
};
mastodon = {
enable = true;
homeserver = "c.im";
tokenFile = config.age.secrets.mastodon-token-logotheca.path;
language = "de";
};
command = "${niveumPackages.literature-quote}/bin/literature-quote";
};
age.secrets = {
mastodon-token-logotheca.file = ../../secrets/mastodon-token-logotheca.age;
};
niveum.passport.services = [
{
title = "Literature quote bot";
description = "sends me and my friends three <a href=\"https://logotheca.xn--kiern-0qa.de/\">logotheca</a> quotes a day.";
}
];
}

View File

@@ -0,0 +1,48 @@
{
pkgs,
config,
lib,
...
}: let
nachtischsatan-bot = {tokenFile}:
pkgs.writers.writePython3 "nachtischsatan-bot" {
libraries = [pkgs.python3Packages.python-telegram-bot];
} ''
from telegram.ext import Application, ContextTypes, MessageHandler, filters
from telegram import Update
import random
import time
async def flubber(update: Update, context: ContextTypes.DEFAULT_TYPE):
time.sleep(random.randrange(4000) / 1000)
await update.message.reply_text("*flubberflubber*")
with open('${tokenFile}', 'r') as tokenFile:
token = tokenFile.read().strip()
application = Application.builder().token(token).build()
application.add_handler(MessageHandler(filters.ALL, flubber))
application.run_polling()
'';
in {
systemd.services.telegram-nachtischsatan = {
wantedBy = ["multi-user.target"];
description = "*flubberflubber*";
enable = true;
script = toString (nachtischsatan-bot {
tokenFile = config.age.secrets.telegram-token-nachtischsatan.path;
});
serviceConfig.Restart = "always";
};
age.secrets.telegram-token-nachtischsatan.file = ../../secrets/telegram-token-nachtischsatan.age;
niveum.passport.services = [
{
title = "Nachtischsatan-Bot";
link = "https://t.me/NachtischsatanBot";
description = "*flubberflubber*";
}
];
}

53
configs/bots/smyth.nix Normal file
View File

@@ -0,0 +1,53 @@
{
config,
pkgs,
lib,
...
}: {
niveum.bots.smyth = {
enable = true;
time = "08:00";
mastodon = {
enable = true;
tokenFile = config.age.secrets.mastodon-token-smyth.path;
language = "en";
};
telegram = {
enable = true;
tokenFile = config.age.secrets.telegram-token-kmein.path;
chatIds = ["@HerbertWeirSmyth"];
};
command = toString (pkgs.writers.writeDash "random-smyth" ''
set -efu
RANDOM_SECTION=$(
${pkgs.curl}/bin/curl -sSL http://www.perseus.tufts.edu/hopper/xmltoc?doc=Perseus%3Atext%3A1999.04.0007%3Asmythp%3D1 \
| ${pkgs.gnugrep}/bin/grep -o 'ref="[^"]*"' \
| ${pkgs.coreutils}/bin/shuf -n1 \
| ${pkgs.gnused}/bin/sed 's/^ref="//;s/"$//'
)
url="http://www.perseus.tufts.edu/hopper/text?doc=$RANDOM_SECTION"
${pkgs.curl}/bin/curl -sSL "$url"\
| ${pkgs.htmlq}/bin/htmlq '#text_main' \
| ${pkgs.gnused}/bin/sed 's/<\/\?hr>//g' \
| ${pkgs.pandoc}/bin/pandoc -f html -t plain --wrap=none
printf '\n%s\n\n#AncientGreek' "$url"
'');
};
systemd.timers.bot-smyth.timerConfig.RandomizedDelaySec = "10h";
age.secrets = {
mastodon-token-smyth.file = ../../secrets/mastodon-token-smyth.age;
};
niveum.passport.services = [
{
title = "Herbert Weir Smyth Bot";
description = "sends a random section from Smyth's Ancient Greek grammar to Telegram.";
link = "https://t.me/HerbertWeirSmyth";
}
];
}

160
configs/bots/tlg-wotd.nix Normal file
View File

@@ -0,0 +1,160 @@
{
pkgs,
lib,
config,
niveumPackages,
...
}: {
systemd.services.bot-tlg-wotd = {
enable = true;
wants = ["network-online.target"];
startAt = "9:30";
path = [ pkgs.jq pkgs.curl pkgs.recode pkgs.deno pkgs.imagemagick pkgs.gawk pkgs.gnugrep pkgs.coreutils ];
environment = {
NPM_CONFIG_CACHE = "/tmp";
CLTK_DATA = "/tmp";
};
script = ''
set -efux
chat_id=@tlgwotd
export TELEGRAM_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/telegram-token")"
export MASTODON_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/mastodon-token")"
json_data=$(curl -sSL http://stephanus.tlg.uci.edu/Iris/Wotd | recode html..utf8)
word=$(echo "$json_data" | jq -r '.word')
compact_word=$(echo "$word" | sed 's/,.*$//')
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')
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 ];
} ''
import sys
from cltk.phonology.grc.transcription import Transcriber
probert = Transcriber("Attic", "Probert")
text = " ".join(sys.argv[1:])
ipa = probert.transcribe(text)
print(ipa)
''} "$compact_word")
photo_path=/tmp/output.png
hex_to_rgb() {
hex="$1"
r=$(printf "%d" "0x$(echo "$hex" | cut -c2-3)")
g=$(printf "%d" "0x$(echo "$hex" | cut -c4-5)")
b=$(printf "%d" "0x$(echo "$hex" | cut -c6-7)")
echo "$r $g $b"
}
calculate_luminance() {
r="$1"
g="$2"
b="$3"
r_l=$(echo "$r" | awk '{print ($1 / 255 <= 0.03928) ? $1 / 255 / 12.92 : (($1 / 255 + 0.055) / 1.055)^2.4}')
g_l=$(echo "$g" | awk '{print ($1 / 255 <= 0.03928) ? $1 / 255 / 12.92 : (($1 / 255 + 0.055) / 1.055)^2.4}')
b_l=$(echo "$b" | awk '{print ($1 / 255 <= 0.03928) ? $1 / 255 / 12.92 : (($1 / 255 + 0.055) / 1.055)^2.4}')
echo "$r_l $g_l $b_l" | awk '{print 0.2126*$1 + 0.7152*$2 + 0.0722*$3}'
}
hex_color="#$(echo "$compact_word" | md5sum | cut -c 1-6)"
if echo "$hex_color" | grep -qE '^#[0-9A-Fa-f]{6}$'; then
set -- $(hex_to_rgb "$hex_color")
r="$1"
g="$2"
b="$3"
fi
luminance=$(calculate_luminance "$r" "$g" "$b")
threshold="0.1"
echo "$r $g $b"
if [ "$(echo "$luminance" | awk -v threshold="$threshold" '{print ($1 > threshold)}')" -eq 1 ]; then
color1="black"
color2="#333"
else
color1="white"
color2=lightgrey
fi
magick -size 1400x846 \
xc:"$hex_color" \
-font "${pkgs.gentium}/share/fonts/truetype/GentiumBookPlus-Bold.ttf" \
-fill "$color1" \
-pointsize 150 -gravity west \
-annotate +100-160 "$compact_word" \
-font "${pkgs.gentium}/share/fonts/truetype/GentiumBookPlus-Regular.ttf" \
-fill "$color2" \
-pointsize 60 -gravity west \
-annotate +100+00 "$transliteration" \
-fill "$color1" \
-annotate +100+120 "$definition" \
-fill "$color2" \
-pointsize 40 -gravity southwest \
-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$TELEGRAM_TOKEN/sendPhoto" \
-F "chat_id=\"$chat_id\"" \
-F "photo=@$photo_path" \
-F parse_mode=Markdown \
-F caption="$telegram_caption"
mastodon_upload_response=$(curl -X POST "https://botsin.space/api/v2/media" \
-H "Authorization: Bearer $MASTODON_TOKEN" \
-F "file=@$photo_path" \
-F "description=$word $definition")
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=$mastodon_caption" \
-d "visibility=public" \
-d "media_ids[]=$mastodon_image_id"
'';
serviceConfig = {
Type = "oneshot";
DynamicUser = true;
StateDirectory = "tlgwotd";
PrivateTmp = true;
LoadCredential = [
"telegram-token:${config.age.secrets.telegram-token-kmein.path}"
"mastodon-token:${config.age.secrets.mastodon-token-tlgwotd.path}"
];
};
};
age.secrets = {
telegram-token-kmein.file = ../../secrets/telegram-token-kmein.age;
mastodon-token-tlgwotd.file = ../../secrets/mastodon-token-tlgwotd.age;
};
niveum.passport.services = [
{
title = "Thesaurus Linguae Graecae Word of the Day";
description = "sends <a href=\"https://stephanus.tlg.uci.edu/\">TLG</a>'s word of the day to Telegram.";
link = "https://t.me/tlgwotd";
}
];
}

72
configs/bots/transits.nix Normal file
View File

@@ -0,0 +1,72 @@
{
config,
pkgs,
lib,
...
}: let
toSymbols = pkgs.writers.writeDash "to-symbols" ''
${pkgs.gnused}/bin/sed '
s/\bTri\b//;
s/\bOpp\b//;
s/\bSqu\b//;
s/\bSex\b//;
s/\bCon\b//;
s/Sun//g;
s/Moon//g;
s/Mercury//g;
s/Venus//g;
s/Mars//g;
s/Jupiter//g;
s/Saturn//g;
s/Uranus//g;
s/Neptune//g;
s/Pluto//g;
s/North Node//g;
s/\bLeo\b//g;
s/\bCan\(cer\)\?\b//g;
s/\bGem\(ini\)\?\b//g;
s/\bVir\(go\)\?\b//g;
s/\bLib\(ra\)\?\b//g;
s/\bTau\(rus\)\?\b//g;
s/\bAri\(es\)\?\b//g;
s/\bSco\(rpio\)\?\b//g;
s/\bPis\(ces\)\?\b//g;
s/\bSag\(ittarius\)\?\b//g;
s/\bAqu\(arius\)\?\b//g;
s/\bCap\(ricorn\)\?\b//g;
s/-->//g;
s/\s\+/ /g; # replace multiple spaces by one
s/^.*\([ 0-9]\{2\}:[0-9]\{2\}\) /\1 /; # remove date at beginning of line (but not everything up to v/c X:XX)
s/^\s*//
'
'';
in {
niveum.bots.transits = {
enable = true;
time = "*:0/1";
mastodon = {
enable = true;
tokenFile = config.age.secrets.mastodon-token-transits.path;
};
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')
(
cd ${pkgs.astrolog}/bin
# ./astrolog -Yt -Yd -q 10 22 1999 6:32 -zN Kassel -td $date -R Uranus Neptune Pluto "North Node"
./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" || :
'');
};
age.secrets = {
mastodon-token-transits.file = ../../secrets/mastodon-token-transits.age;
};
}