145 lines
5.8 KiB
Bash
Executable File
145 lines
5.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
## Copyright (C) 2019 Michael Neese
|
|
##
|
|
## This program is free software: you can redistribute it and/or modify it
|
|
## under the terms of the GNU General Public License as published
|
|
## by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
##
|
|
## This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
## without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
## See the GNU General Public License for more details.
|
|
##
|
|
## You should have received a copy of the GNU General Public License along with this program.
|
|
## If not, see http://www.gnu.org/licenses/.
|
|
|
|
## This script sends a message to a matrix room <https://matrix.org>.
|
|
## Requires a matrix synapse (https://github.com/matrix-org/synapse) server as endpoint and jq on the system which runs the script.
|
|
## Usable for e.g. cronjobs, nagios notifications or ci pipelines.
|
|
|
|
AUTHOR="Michael Neese <madic@geekbundle.org>"
|
|
MATRIX_USER_TEMP=""
|
|
MATRIX_PASS_TEMP=""
|
|
MATRIX_SERVER_TEMP=""
|
|
MATRIX_ROOM_ID_TEMP=""
|
|
MATRIX_ACCESS_TOKEN_TEMP=""
|
|
# Allowed values: 0 and 1
|
|
DEBUG=${DEBUG:-"0"}
|
|
|
|
# Check if jq is installed
|
|
if ! [ -x "$(command -v jq)" ]; then
|
|
echo 'Error: jq is not installed.'
|
|
exit 1
|
|
fi
|
|
|
|
USAGE() {
|
|
echo "Usage: COMMAND | ${0##*/} [OPTION]
|
|
Send a (multiline) message to a matrix room.
|
|
This script expects data to be piped in on STDIN.
|
|
Options:
|
|
-u Matrix User Name
|
|
-p Matrix User Password
|
|
-a Matrix Access Token
|
|
-s Matrix Server FQDN
|
|
-r Matrix RoomID
|
|
-f Optional configuration file
|
|
-h Show help message
|
|
You can either provide an access token or username / password.
|
|
If no access token is provided, this script will get one for you from the server.
|
|
But keep in mind, that every time this script runs without an access token, a new access token is created and fills up your device list.
|
|
With to many devices in your device list, there is a potential getting blacklisted.
|
|
The recommended way is to provide an access token.
|
|
Getting an access token from the server:
|
|
curl -s -X POST -d '{ \"type\":\"m.login.password\", \"user\":\"\$MATRIX_USER\", \"password\":\"\$MATRIX_PASS\" }' \"https://\$MATRIX_SERVER/_matrix/client/r0/login\" | jq -r '.access_token'
|
|
Before you can send a message to a room, the user sending the message already needs to have joined it.
|
|
Examples:
|
|
echo \"Testmessage 1\" | ${0##*/} -f /etc/${0##*/}.conf
|
|
echo \"Testmessage 2\" | ${0##*/} -f /etc/${0##*/}.conf -r \!QtykxKocfZaZOUrTwp:matrix.org
|
|
Please keep an eye on the format of the RoomID!
|
|
Configuration file:
|
|
The configuration file can contain the following values.
|
|
MATRIX_USER=TestUser
|
|
MATRIX_PASS=TestPassword
|
|
MATRIX_ACCESS_TOKEN=MD..blah
|
|
MATRIX_SERVER=matrix.org
|
|
# RoomID from the #matrix:matrix.org Channel
|
|
MATRIX_ROOM_ID=\"!QtykxKocfZaZOUrTwp:matrix.org\"
|
|
These values can be overriden by command line parameters.
|
|
Cron Job:
|
|
It's also possible to use this script to send the output of a cronjob to a matrix room.
|
|
Example:
|
|
SHELL=/bin/bash
|
|
0 4 * * * root /usr/local/sbin/random_script | ${0##*/} -f /etc/${0##*/}.conf
|
|
0 5 * * * OUTPUT=\"# \$(hostname -f) - random_script\" && OUTPUT=\"\$OUTPUT\n$(random_script)\" && echo -e \"\$OUTPUT\" | ${0##*/} -f /etc/${0##*/}.conf
|
|
The last example allows appending e.g. the fqdn of the server and custom text to a message.
|
|
With this approach it's possible to send the same message from different servers to the same room and still be able to keep track from which server the message originates.
|
|
Report bugs to: $AUTHOR
|
|
Homepage: <https://www.geekbundle.org>"
|
|
exit 1
|
|
}
|
|
|
|
while getopts ":f:u:a:p:s:r:h" opt; do
|
|
case "$opt" in
|
|
f)
|
|
CONFIG_FILE="$OPTARG"
|
|
;;
|
|
u)
|
|
MATRIX_USER_TEMP="$OPTARG"
|
|
;;
|
|
p)
|
|
MATRIX_PASS_TEMP="$OPTARG"
|
|
;;
|
|
a)
|
|
MATRIX_ACCESS_TOKEN_TEMP="$OPTARG"
|
|
;;
|
|
s)
|
|
MATRIX_SERVER_TEMP="$OPTARG"
|
|
;;
|
|
r)
|
|
MATRIX_ROOM_ID_TEMP="$OPTARG"
|
|
;;
|
|
h)
|
|
USAGE
|
|
;;
|
|
*)
|
|
USAGE
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Test if stdin is not empty
|
|
[ -t 0 ] && USAGE
|
|
|
|
# Read parameters from config file
|
|
if [ -f "$CONFIG_FILE" ]; then source "$CONFIG_FILE"; fi
|
|
|
|
# Override config file values with the values from the command line
|
|
if [ -n "$MATRIX_USER_TEMP" ]; then MATRIX_USER="$MATRIX_USER_TEMP"; fi
|
|
if [ -n "$MATRIX_PASS_TEMP" ]; then MATRIX_PASS="$MATRIX_PASS_TEMP"; fi
|
|
if [ -n "$MATRIX_ACCESS_TOKEN_TEMP" ]; then MATRIX_ACCESS_TOKEN="$MATRIX_ACCESS_TOKEN_TEMP"; fi
|
|
if [ -n "$MATRIX_SERVER_TEMP" ]; then MATRIX_SERVER="$MATRIX_SERVER_TEMP"; fi
|
|
if [ -n "$MATRIX_ROOM_ID_TEMP" ]; then MATRIX_ROOM_ID="$MATRIX_ROOM_ID_TEMP"; fi
|
|
|
|
# Test if all required variables are not empty
|
|
if [ -z "$MATRIX_USER" ] || [ -z "$MATRIX_PASS" ] || [ -z "$MATRIX_SERVER" ] || [ -z "$MATRIX_ROOM_ID" ]; then USAGE; fi
|
|
|
|
# Get Matrix Access Token if none provided
|
|
if [ -z "$MATRIX_ACCESS_TOKEN" ]; then
|
|
MATRIX_ACCESS_TOKEN=$(curl -s -X POST -d "{ \"type\":\"m.login.password\", \"user\":\"$MATRIX_USER\", \"password\":\"$MATRIX_PASS\" }" "https://$MATRIX_SERVER/_matrix/client/r0/login" | jq -r '.access_token')
|
|
fi
|
|
|
|
# Some debug code
|
|
if [ "$DEBUG" -eq 1 ]; then
|
|
echo "curl -s -X POST -d \"{ \"type\":\"m.login.password\", \"user\":\"$MATRIX_USER\", \"password\":\"$MATRIX_PASS\" }\" \"https://$MATRIX_SERVER/_matrix/client/r0/login\" | jq -r '.access_token'"
|
|
echo -e "\nMatrix Access Token: $MATRIX_ACCESS_TOKEN"
|
|
fi
|
|
|
|
# Send Message and output errors on DEBUG=1
|
|
if [ "$DEBUG" -eq 1 ]; then
|
|
curl -X POST -d "$(jq -Rs --arg msgtype m.text '{$msgtype, body:.}')" "https://$MATRIX_SERVER/_matrix/client/r0/rooms/$MATRIX_ROOM_ID/send/m.room.message?access_token=$MATRIX_ACCESS_TOKEN"
|
|
fi
|
|
|
|
# Send message without any output if DEBUG < 1
|
|
if [ "$DEBUG" -lt 1 ]; then
|
|
curl -s -o /dev/null -X POST -d "$(jq -Rs --arg msgtype m.text '{$msgtype, body:.}')" "https://$MATRIX_SERVER/_matrix/client/r0/rooms/$MATRIX_ROOM_ID/send/m.room.message?access_token=$MATRIX_ACCESS_TOKEN"
|
|
fi |