diff --git a/machines/gerd/services/notify/app.py b/machines/gerd/services/notify/app.py index 4a478e3..ffb7c73 100644 --- a/machines/gerd/services/notify/app.py +++ b/machines/gerd/services/notify/app.py @@ -32,6 +32,7 @@ CONFIG_PORT = int(getenv("PORT", 8080)) CONFIG_DATABASE_PATH = getenv("DATABASE_PATH", "notifications.db") +CONFIG_MATRIX_BOT_NAME = getenv("MATRIX_BOT_NAME", "unset") CONFIG_MATRIX_BOT_TOKEN = getenv("MATRIX_BOT_TOKEN") CONFIG_MATRIX_HOST = getenv("MATRIX_HOST") @@ -41,33 +42,44 @@ CONFIG_MAIL_USERNAME = getenv("MAIL_USERNAME") CONFIG_MAIL_PASSWORD = getenv("MAIL_PASSWORD") CONFIG_MAIL_DOMAIN = getenv("MAIL_DOMAIN") CONFIG_MAIL_HOST = getenv("MAIL_HOST") -CONFIG_MAIL_PORT = int(getenv("MAIL_PORT")) +CONFIG_MAIL_PORT = int(getenv("MAIL_PORT", "465")) CONFIG_MAIL_MODE = getenv("MAIL_MODE", "ssl") +script_example = rf"""#!/usr/bin/env bash +#!/usr/bin/env nix-shell +#!nix-shell --pure -i python3 -p "python3.withPackages (ps: with ps; [ requests ])" +import requests +import argparse +import sys +import os -script_example = r"""#!/usr/bin/env bash -BODY="$1" -TITLE=${2:-Notification} -JQ_EXPR=${3:-.} -TYPE=${4:-matrix} -TOKEN="$(cat ~/.config/notify/token)" -# TOKEN="$(cat /run/agenix/notify-token)" -URL="||URL||/notify" +parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) +parser.add_argument("--title", default="", help="Subject/title of the notification") +parser.add_argument("--body", default="", help="`-` for stdin") +parser.add_argument("--jq", default=".", help="Jq filter to use") +parser.add_argument("--type", default="matrix", help="mail or matrix") +parser.add_argument("--token", help="token to use") +parser.add_argument("--token-file", help="file to read token from") +parser.add_argument("--url", default="{CONFIG_URL}/notify", help="Notify endpoint") +args = parser.parse_args() -# get stdin if needed -if [ "$BODY" = "-" ]; then - BODY="$(cat -)" -fi +token: str = args.token +if args.token_file: + token = open(args.token_file, "r").read().strip() +if not token: + exit("No token or tokenfile specified, or empty") -# make request -curl -H "Authorization: Bearer $TOKEN" "$URL" \ - --get \ - --data-urlencode "title=$TITLE" \ - --data-urlencode "body=$BODY" \ - --data-urlencode "jq=$JQ_EXPR" \ - --data-urlencode "type=$TYPE" -""".replace( - "||URL||", "https://notify.fricloud.dk" +data = args.body +if data == "-" and not sys.stdin.isatty(): + data = "\n".join(sys.stdin.readlines()) + +req = requests.post(args.url, headers={{"Authorization": f"Bearer {{token}}"}}, data=data) +print(req.text) +""" + +script_example_with_token = script_example.replace( + '--token"', + '--token", default="||TOKEN||"', ) @@ -149,9 +161,66 @@ def index():
-

-{script_example}
-    
+

+ This notification service has support for both matrix and email. + Matrix notifications will be sent from {CONFIG_MATRIX_BOT_NAME}, be sure to invite them to the room/space if it is private. + If using email, they will only be sent to your member email, and can't be sent anywhere else. + You'll receive them from {CONFIG_MAIL_USERNAME}@{CONFIG_MAIL_DOMAIN}. +

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ParamDescriptionDefaultExample
titleTitle of notificationNotificationCompilation Finished!
bodyBody of notificationemptyCompilation result: success (default is nothing)
typetype of notificationmatrixmatrix
room_idMatrix room IDdefault room!yREJWHUMJhGROiHbtu:fricloud.dk or #na-offtopic:rend.al
+
+ curl +
curl "{CONFIG_URL}/notify" -H "Authorization: Bearer {token}"
+ curl w/ specific body/title +
curl "{CONFIG_URL}/notify?title=MyTitle&body=MyBody" -H "Authorization: Bearer {token}"
+ Python +
{script_example}
+ Python w/ hardcoded token (DO NOT SHARE) +
{script_example_with_token}
+ + Nix Python Script HARDCODED TOKEN (DO NOT SHARE) +
pkgs.writers.writePython3Bin "notify" {{
+    libraries = with pkgs.python3Packages; [ requests ];
+    doCheck = false;
+  }} ''{script_example_with_token}'';
+
+