Files
matrix/hookshot/deploy.sh

56 lines
2.1 KiB
Bash
Raw Normal View History

#!/bin/bash
# Deploy hookshot transformation functions to Matrix room state.
# Each .js file in this directory maps to a webhook by the same name (case-sensitive).
#
# Usage:
# ./deploy.sh # deploy all hooks
# ./deploy.sh proxmox.js # deploy one hook
#
# Requirements:
# MATRIX_TOKEN - access token with power level >= 50 in the target room
# MATRIX_SERVER - homeserver URL (default: https://matrix.lotusguild.org)
# MATRIX_ROOM - room ID where hooks are registered
MATRIX_SERVER="${MATRIX_SERVER:-https://matrix.lotusguild.org}"
MATRIX_ROOM="${MATRIX_ROOM:-!GttT4QYd1wlGlkHU3qTmq_P3gbyYKKeSSN6R7TPcJHg}"
if [ -z "$MATRIX_TOKEN" ]; then
echo "Error: MATRIX_TOKEN is not set." >&2
exit 1
fi
DIR="$(cd "$(dirname "$0")" && pwd)"
deploy_hook() {
local file="$1"
local filename="$(basename "$file" .js)"
# Capitalize first letter of each hyphen-separated word, preserving hyphens
# e.g. proxmox -> Proxmox, uptime-kuma -> Uptime-Kuma, tinker-tickets -> Tinker-Tickets
local state_key="$(python3 -c "import sys; s='$filename'; print('-'.join(w.capitalize() for w in s.split('-')))")"
local fn="$(cat "$file")"
local encoded_room="$(python3 -c "import urllib.parse; print(urllib.parse.quote('$MATRIX_ROOM'))")"
local encoded_key="$(python3 -c "import urllib.parse; print(urllib.parse.quote('$state_key'))")"
local response
response=$(curl -sf -X PUT \
"$MATRIX_SERVER/_matrix/client/v3/rooms/$encoded_room/state/uk.half-shot.matrix-hookshot.generic.hook/$encoded_key" \
-H "Authorization: Bearer $MATRIX_TOKEN" \
-H "Content-Type: application/json" \
--data-binary "$(python3 -c "import json; print(json.dumps({'name': '$state_key', 'transformationFunction': open('$file').read()}))")" \
2>&1)
if echo "$response" | python3 -c "import json,sys; d=json.load(sys.stdin); exit(0 if 'event_id' in d else 1)" 2>/dev/null; then
echo "$state_key"
else
echo "$state_key: $response"
fi
}
if [ -n "$1" ]; then
deploy_hook "$DIR/$1"
else
for f in "$DIR"/*.js; do
deploy_hook "$f"
done
fi