notify: added support for quick URLs

This commit is contained in:
eyjhb 2025-03-12 00:04:36 +01:00
parent 2380f6694d
commit 583583ae5f
Signed by: eyjhb
GPG key ID: 609F508E3239F920

View file

@ -9,14 +9,8 @@ import os
import jq
import json
# TODO(eyJhb): add the following
# - example of bash alias
# - jq filter parameter documentation
# - jq filter example
# - easy way to run it ONLY with curl
# - e.g. curl notify.fricloud.dk/notify/<simple_token>/msg
app = Flask(__name__)
app.url_map.strict_slashes = False
import logging
@ -175,11 +169,16 @@ def index():
<input type="submit" class="btn btn-primary" name="action" value="Set Default Room ID">
<hr>
<div class="mb-3">
<label class="form-label">Token</label>
<input type="text" value="{token}" placeholder="token-not-generated" readonly class="form-control" >
<label class="form-label">Token (dashes are optional)</label>
<input type="text" value="{token}" placeholder="token-not-generated" readonly class="form-control" onclick="this.select();" >
</div>
<input type="submit" class="btn btn-primary" name="{generate_token_name}" value="{generate_token_value}">
</form>
<hr>
<div class="mb-3">
<label class="form-label">Quick URL</label>
<input type="text" value="curl {CONFIG_URL}/notify/{token.replace("-", "")}/mymessage" class="form-control" onclick="this.select();" >
</div>
<hr>
<p>
This notification service has support for both matrix and email.
@ -293,18 +292,23 @@ def index():
return tmpl
@app.route("/notify", methods=["GET", "POST"])
def send_notification():
@app.route("/notify", methods=["GET", "POST"], defaults={"token": "", "body": ""})
@app.route("/notify/<token>", methods=["GET", "POST"], defaults={"body": ""})
@app.route("/notify/<token>/<body>", methods=["GET", "POST"])
def send_notification(token: str, body: str):
# default to this
ntype = request.args.get("type", "matrix")
title = request.args.get("title", "Notification")
body = request.args.get("body", request.get_data().decode("utf-8"))
body = request.args.get("body", request.get_data().decode("utf-8") or body)
if not body:
body = " "
token = request.authorization.token or request.authorization.password
if request.authorization:
token = request.authorization.token or request.authorization.password
if not token:
return (
"No token found, please either specify with Authorization: Bearer <token> or HTTPBasic",
"No token found, please either specify with Authorization: Bearer <token>, HTTPBasic or in the URL path",
401,
)