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

bots: post on matrix as well

This commit is contained in:
2024-10-08 22:35:41 +02:00
parent a28fca11a7
commit 136d4e699f
2 changed files with 35 additions and 4 deletions

View File

@@ -14,17 +14,20 @@
chatIds = ["-1001760262519"]; chatIds = ["-1001760262519"];
parseMode = "Markdown"; parseMode = "Markdown";
}; };
mastodon = { matrix = {
enable = true; enable = true;
homeserver = "c.im"; homeserver = "matrix.4d2.org";
tokenFile = config.age.secrets.mastodon-token-logotheca.path; tokenFile = config.age.secrets.matrix-token-lakai.path;
language = "de"; chatIds = [
"!zlwCuPiCNMSxDviFzA:4d2.org"
];
}; };
command = "${niveumPackages.literature-quote}/bin/literature-quote"; command = "${niveumPackages.literature-quote}/bin/literature-quote";
}; };
age.secrets = { age.secrets = {
mastodon-token-logotheca.file = ../../secrets/mastodon-token-logotheca.age; mastodon-token-logotheca.file = ../../secrets/mastodon-token-logotheca.age;
matrix-token-lakai.file = ../../secrets/matrix-token-lakai.age;
}; };
niveum.passport.services = [ niveum.passport.services = [

View File

@@ -17,6 +17,8 @@ with lib; let
"telegram-token:${bot.telegram.tokenFile}" "telegram-token:${bot.telegram.tokenFile}"
] ++ lib.optionals (bot.mastodon.enable) [ ] ++ lib.optionals (bot.mastodon.enable) [
"mastodon-token:${bot.mastodon.tokenFile}" "mastodon-token:${bot.mastodon.tokenFile}"
] ++ lib.optionals (bot.matrix.enable) [
"matrix-token:${bot.matrix.tokenFile}"
]; ];
}; };
wants = ["network-online.target"]; wants = ["network-online.target"];
@@ -25,6 +27,17 @@ with lib; let
if [ -n "$QUOTE" ]; then if [ -n "$QUOTE" ]; then
echo $QUOTE >&2 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) '' ${lib.optionalString (bot.mastodon.enable) ''
export MASTODON_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/mastodon-token")" export MASTODON_TOKEN="$(cat "$CREDENTIALS_DIRECTORY/mastodon-token")"
${pkgs.curl}/bin/curl -X POST "https://${bot.mastodon.homeserver}/api/v1/statuses" \ ${pkgs.curl}/bin/curl -X POST "https://${bot.mastodon.homeserver}/api/v1/statuses" \
@@ -56,6 +69,21 @@ in {
enable = mkEnableOption "Mastodon and Telegram bot"; enable = mkEnableOption "Mastodon and Telegram bot";
time = mkOption {type = types.str;}; time = mkOption {type = types.str;};
command = 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 { mastodon = mkOption {
default = {}; default = {};
type = types.submodule { type = types.submodule {