From f15f94dd9c4be9ae4c854bc8cc015790a6295f37 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 12 Jun 2021 01:11:43 +0200 Subject: [PATCH] matrix-notifier: add initial script This has been tested to work on my homeserver. --- matrix-notifier | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 matrix-notifier diff --git a/matrix-notifier b/matrix-notifier new file mode 100755 index 0000000..9bf6b4c --- /dev/null +++ b/matrix-notifier @@ -0,0 +1,59 @@ +#!/usr/bin/env bash + +print_err() { + printf "%s\n" "$1" >&2 +} + +# Blessed Stack Overflow +rawurlencode() { + (set +x + local string="${1}" + local strlen=${#string} + local encoded="" + local pos c o + + for ((pos=0 ; pos/dev/null | + jq 'any(.flows[].type; .== "m.login.password")')" != "true" ]; then + print_err "Login method not supported" + exit 1 +fi + + +LOGIN_JSON="$(printf '%s\n%s' "$USER" "$PASS" | + jq -Rn '[inputs] | {type: "m.login.password", user: .[0], password: .[1]}')" &>/dev/null +TOKEN="$(curl -XPOST \ + -d "$LOGIN_JSON" \ + "$ADDRESS/_matrix/client/r0/login" 2>/dev/null| jq .access_token --raw-output)" + +if [ -z "$TOKEN" ] || [ "$TOKEN" == "null" ]; then + print_err "Error during login" + exit 1 +fi + +MESSAGE_JSON="$(printf '%s' "$MESSAGE" | + jq --raw-input --slurp '{msgtype: "m.text", body: .}')" +curl -XPOST \ + -d "$MESSAGE_JSON" \ + "$ADDRESS/_matrix/client/r0/rooms/$(rawurlencode "$ROOM")/send/m.room.message?access_token=$(rawurlencode "$TOKEN")" 2>/dev/null