diff --git a/configs/bots/logotheca.nix b/configs/bots/logotheca.nix index 24ee3b2..6a07791 100644 --- a/configs/bots/logotheca.nix +++ b/configs/bots/logotheca.nix @@ -14,17 +14,20 @@ chatIds = ["-1001760262519"]; parseMode = "Markdown"; }; - mastodon = { + matrix = { enable = true; - homeserver = "c.im"; - tokenFile = config.age.secrets.mastodon-token-logotheca.path; - language = "de"; + homeserver = "matrix.4d2.org"; + tokenFile = config.age.secrets.matrix-token-lakai.path; + chatIds = [ + "!zlwCuPiCNMSxDviFzA:4d2.org" + ]; }; command = "${niveumPackages.literature-quote}/bin/literature-quote"; }; age.secrets = { mastodon-token-logotheca.file = ../../secrets/mastodon-token-logotheca.age; + matrix-token-lakai.file = ../../secrets/matrix-token-lakai.age; }; niveum.passport.services = [ diff --git a/modules/telegram-bot.nix b/modules/telegram-bot.nix index a024cbb..96a8026 100644 --- a/modules/telegram-bot.nix +++ b/modules/telegram-bot.nix @@ -17,6 +17,8 @@ with lib; let "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}" ]; }; wants = ["network-online.target"]; @@ -25,6 +27,17 @@ with lib; let if [ -n "$QUOTE" ]; then echo $QUOTE >&2 + ${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}') + ${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)} + ''} + ${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" \ @@ -56,6 +69,21 @@ in { 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; + }; + }; + }; + }; mastodon = mkOption { default = {}; type = types.submodule {