Compare commits

...

5 commits

Author SHA1 Message Date
eyjhb
d31149cfd0
notify: added bash alias example 2025-03-12 00:04:44 +01:00
eyjhb
583583ae5f
notify: added support for quick URLs 2025-03-12 00:04:36 +01:00
eyjhb
2380f6694d
notify: better documentation 2025-03-12 00:04:14 +01:00
eyjhb
5564c8cbfc
notify: no need to include dashes in token 2025-03-12 00:03:15 +01:00
eyjhb
09b3d1510b
notify: fewer words for a token 2025-03-12 00:02:58 +01:00

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.
@ -222,9 +221,24 @@ def index():
<td>default room</td>
<td>!yREJWHUMJhGROiHbtu:fricloud.dk or #na-offtopic:rend.al</td>
</tr>
<tr>
<th scope="row">token</th>
<td>Authorization Token</td>
<td>empty</td>
<td>enable-trade-decide or enabletradedecide</td>
</tr>
<tr>
<th scope="row">jq</th>
<td>jq filter</td>
<td>.</td>
<td>[.[].commits | .name ] | join("\\n")</td>
</tr>
</tbody>
</table>
<hr>
bash alias
<pre class="border"><code>notify() {{ curl "{CONFIG_URL}/notify/{token}" -g --data-urlencode "body=${{1:-}}"; }}</code></pre>
curl
<pre class="border"><code>curl "{CONFIG_URL}/notify" -H "Authorization: Bearer {token}"</code></pre>
curl w/ specific body/title
@ -240,6 +254,27 @@ def index():
doCheck = false;
}} ''{script_example_with_token}'';
</code></pre>
<hr>
<h2>Notes</h2>
<p>
<b>jq</b> is very powerful, and can easily be used to turn webhook data into useful information in a notification.
Just append your `jq=<url_encoded_query>`, to your notification URL, and then watch the magic.
Below is an example for doing it with Forgejo, when new commits are pushed.
</p>
<pre class="border"><code>(.total_commits | tostring)
+ " commits pushed to "
+ .repository.full_name
+ "\n\n" +
# format commits
([.commits.[]
| "- "
+ (.message | gsub("[\n\t]"; ""))
+ " (" + .author.name + ")" ]
| join("\n"))
+ "\n\n"
+ "Changes: " + .compare_url</code></pre>
<!--
<p class="fs-5">You've successfully loaded up the Bootstrap starter example. It includes <a href="https://getbootstrap.com/">Bootstrap 5</a> via the <a href="https://www.jsdelivr.com/package/npm/bootstrap">jsDelivr CDN</a> and includes an additional CSS and JS file for your own code.</p>
@ -260,18 +295,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,
)
@ -291,7 +331,8 @@ def send_notification():
con = get_db()
cur = con.cursor()
res = cur.execute(
"SELECT username FROM tokens WHERE token = ?", (token,)
"SELECT username FROM tokens WHERE REPLACE(token, '-', '') = ? OR token = ? ",
(token, token),
).fetchone()
if not res:
@ -361,7 +402,7 @@ def set_user_default_matrix_room(username: str, roomid: str):
con.commit()
def generate_token(num_words: int = 5) -> str:
def generate_token(num_words: int = 3) -> str:
mnemo = Mnemonic("english")
words = mnemo.generate(strength=256)
return "-".join(words.split(" ")[0:num_words])