Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save unclechu/a4df6148d88fac0969404c9827012be8 to your computer and use it in GitHub Desktop.
Save unclechu/a4df6148d88fac0969404c9827012be8 to your computer and use it in GitHub Desktop.
A script that helps to switch [Matrix] notifications for a room to "mentions only" mode from CLI
#!/usr/bin/env bash
#
# A script that helps to switch [Matrix] notifications for a room
# to "mentions only" mode from CLI.
#
# Author: Viacheslav Lotsmanov
# License: Public Domain
#
set -e
ec() { printf '%s' "$1"; }
echo -n 'Enter server (keep empty for "matrix.org"): ' && read server
if [[ -z $server ]]; then
server=matrix.org
elif ec "$server" | grep -vP '^[a-z0-9.]+$' >/dev/null; then
echo 'Entered server is incorrect!' >&2
exit 1
fi
echo -n 'Enter auth token: ' && read token
[[ -z $token ]] && (echo 'Entered auth token is empty!' >&2; exit 1)
(ec "$token" | grep -P '^[a-zA-Z0-9+/=_-]+$' >/dev/null) || \
(echo 'Entered auth token is incorrect!' >&2; exit 1)
echo -n 'Enter room internal id: ' && read room
[[ -z $room ]] && (echo 'Entered room internal id is empty!' >&2; exit 1)
(ec "$room" | grep -P '^![a-zA-Z]+:[a-z0-9.]+$' >/dev/null) || \
(echo 'Entered room id is incorrect!' >&2; exit 1)
export CURLOPT_HTTP_VERSION=CURL_HTTP_VERSION_2_0
echo "Enabling push rules for room $room…"
curl -XPUT \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $token" \
--data '{"enabled":true}' \
"https://$server/_matrix/client/r0/pushrules/global/room/$room/enabled"
echo $"\"Mentions only\" notifications mode for room $room…"
curl -XPUT \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $token" \
--data '{"actions":["dont_notify"]}' \
"https://$server/_matrix/client/r0/pushrules/global/room/$room"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment