Skip to content

Instantly share code, notes, and snippets.

@HarHarLinks
Last active September 22, 2023 13:08
Show Gist options
  • Save HarHarLinks/5eedfbac6e0bc19f41b58be6087524c0 to your computer and use it in GitHub Desktop.
Save HarHarLinks/5eedfbac6e0bc19f41b58be6087524c0 to your computer and use it in GitHub Desktop.
Send the same server notice to a number of users of your synapse matrix.org homeserver at once
<h1>this is a title</h3>\n
<p>here comes a paragraph</p>\n
<p>and another</p>\n
#!/usr/bin/env bash
if [ $# -lt 4 ]; then
echo "usage:"
echo "./server-notice-broadcast.sh homeserver token targetfile message [html]."
echo " homeserver is the protocol, domain and optional port your server is listening on, e.g. https://matrix-client.matrix.org."
echo " token is an access token of an admin user of your server. Find it in Element Settings -> Help & About -> Access Token."
echo " targetfile is a simple text file with one complete MXID (@name:domain.tld) per line. The notice will be sent to these accounts."
echo " message is the message you want to send. It may contain newline (\n) and html (e.g. <h1></h1>) markup. It may also be a path to file that contains a message."
echo " html is an optional flag. If your message contains html, set this flag to anything but 0 to format your message."
exit
fi
homeserver=$1
token=$2
targetfile=$3
message=$4
# optional argument html
if [ $# == 5 ]; then
html=$5
else
html=0
fi
if ! command -v curl >/dev/null 2>&1; then
echo "please install curl to your $PATH"
exit 1
fi
if ! command -v sed >/dev/null 2>&1; then
echo "please install sed to your $PATH"
exit 1
fi
# read message from file if a path is given
if [ -f "$message" ]; then
message="$(cat "$message")"
message="${message//[$'\n\r']}"
# clean up html for plain body
if [ "$html" != 0 ]; then
htmlmessage="$message"
message="$(echo "$message" | sed 's/<[^>]*>//gm')"
fi
fi
# construct body and send to each user
while read -r user;
do
body="{\"user_id\":\"$user\",\"content\":{\"msgtype\":\"m.text\",\"body\":\"$message\""
if [ "$html" != 0 ]; then
body="$body,\"format\": \"org.matrix.custom.html\",\"formatted_body\": \"$htmlmessage\""
fi
body="$body}}"
# print call for verification
echo --data "$body" --header 'Content-Type: application/json' "$homeserver/_synapse/admin/v1/send_server_notice?access_token=$token"
curl --data "$body" --header 'Content-Type: application/json' "$homeserver/_synapse/admin/v1/send_server_notice?access_token=$token"
done < "$targetfile"
exit 0
@admin:domain.tld
@user:domain.tld
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment