aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOxbian <got.dacs@slmail.me>2023-04-24 11:31:49 +0200
committerOxbian <got.dacs@slmail.me>2023-04-24 11:31:49 +0200
commitc62fce738c590a85427491a77673582dfc4d784e (patch)
tree2836e5663fdd457da8d3f671b58feafa84173649
parentc1f6a7c3c7f7998164f20efcb2d007ca6e8e824d (diff)
downloadmatrix-sender-c62fce738c590a85427491a77673582dfc4d784e.tar.gz
matrix-sender-c62fce738c590a85427491a77673582dfc4d784e.zip
Adding matrix.sh & readme
-rw-r--r--README.md62
-rwxr-xr-xmatrix.sh134
2 files changed, 196 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..efa3dce
--- /dev/null
+++ b/README.md
@@ -0,0 +1,62 @@
+# Matrix Sender
+------
+
+This script allows you to send simple unencrypted message & html to matrix.
+It can be usefull for logging / cronjobs error or monitoring.
+
+## Install
+
+This script **is dependent of jq & curl**.
+
+To install the script, just clone this repo:
+```bash
+git clone https://github.com/oxbian/matrix-sender.git
+```
+
+## Usage
+
+First you need to edit the script with your homeserver url & the roomID
+
+After this you need to get your token
+```bash
+./matrix.sh -t <username> <password>
+```
+
+Once all is setup, you can send messages:
+- Simple message
+```bash
+./matrix.sh -s <message>
+```
+
+- HTML formatted message
+```bash
+./matrix.sh -html <message>
+```
+
+For help you can use:
+```bash
+./matrix.sh -h
+```
+
+```bash
+./matrix.sh --help
+```
+
+**Exemple**
+```bash
+./matrix.sh -s 'Hello world!'
+```
+
+```bash
+./matrix.sh -html '<h1 class="test"> t e s t </h1>'
+```
+
+## Contributing
+
+If you want to contribute, make a pull request with your contribution.
+
+## License
+
+This project is under the GPLv3 license, you can use it in your project but not in closed sources ones.
+Sharing project is what make the world live, think about it.
+
diff --git a/matrix.sh b/matrix.sh
new file mode 100755
index 0000000..6012126
--- /dev/null
+++ b/matrix.sh
@@ -0,0 +1,134 @@
+#!/usr/bin/env bash
+# Script to send message to matrix
+
+set -x
+
+# Matrix homeserver URL
+MATRIX_HOMESERVER=''
+
+# MATRIX room ID
+MATRIX_ROOM_ID=''
+
+# MATRIX token
+MATRIX_TOKEN=''
+
+# Check if variable are set
+check_var() {
+ if [ "$MATRIX_HOMESERVER" == '' ]; then
+ error "MATRIX_HOMESERVER variable not set"
+ fi
+ if [ "$MATRIX_ROOM_ID" == '' ]; then
+ error "MATRIX_ROOM_ID variable not set"
+ fi
+}
+
+# Check if token is set
+check_token() {
+ if [ "$MATRIX_TOKEN" == '' ]; then
+ error "MATRIX_TOKEN variable empty, use $0 -t <username> <password>"
+ fi
+}
+
+# Display an error and leave
+error() {
+ echo $@
+ exit 1
+}
+
+# Clean an input
+clean() {
+ local input=$@
+ printf '%s' "$input" | jq -Rs '.'
+}
+
+# Make a request, $1 the CURL request
+request() {
+ local request=$@
+ local errorcode="$(jq -r '.errcode' <<< "$request")"
+
+ if [[ "$errorcode" != "" && "$errorcode" != "null" ]]; then
+ error "[x] Erreur: "$(jq -r '.error' <<< "$request")""
+ fi
+}
+
+# Get the token of the account
+# usage: ./matrix.sh -t <username> <password>
+# get_token <username> <password>
+get_token() {
+ check_var
+ local token=$(curl -XPOST -H "Content-Type: application/json" --data "{\"type\": \"m.login.password\", \"identifier\": {\"user\": \"$1\", \"type\": \"m.id.user\"}, \"password\": \"$2\"}" \
+ "https://${MATRIX_HOMESERVER#https://}/_matrix/client/r0/login")
+ request $token
+
+ token=$(echo "$token" | jq -r '.access_token')
+ sed -i -e "s#^MATRIX_TOKEN=.*#MATRIX_TOKEN=\'${token}\'#g" $0
+}
+
+# Send a message
+# usage: ./matrix.sh -s <message>
+# usage: ./matrix.sh -html <message>
+# send_message <message>
+send_message() {
+ check_var
+ check_token
+ local id=$(date +%s%N) # Message id
+ local message=$(clean "$@")
+
+ if $HTML; then
+ local data="{\"msgtype\": \"m.text\", \"body\": $message, \"format\": \"org.matrix.custom.html\", \"formatted_body\": $message}"
+ else
+ local data="{\"body\": $message, \"msgtype\": \"m.text\"}"
+ fi
+ request $(curl -XPUT -H "Authorization: Bearer ${MATRIX_TOKEN}" -H "Content-Type: application/json" --data "$data" \
+ "https://${MATRIX_HOMESERVER#https://}/_matrix/client/r0/rooms/$MATRIX_ROOM_ID/send/m.room.message/$id")
+ echo "[v] Message sent"
+}
+
+# Show the help menu
+help() {
+ echo "Usage: $0 <actions> <options>"
+ echo ""
+ echo "Actions:"
+ echo " -h, --help show this menu"
+ echo " -t <username> <password> get the token of the account and save it"
+ echo " -s <message> send a message"
+ echo " -html <html> send an html message"
+}
+
+# Main
+hash curl >/dev/null 2>&1 || error "Curl is required!"
+hash jq >/dev/null 2>&1 || error "jq is required!"
+
+action=""
+case $1 in
+-t)
+ get_token $2 $3
+ exit 0
+ ;;
+
+-s)
+ action="send"
+ shift
+ ;;
+
+-html)
+ action="html"
+ shift
+ ;;
+
+-h|--help|*)
+ help
+ exit 0
+ ;;
+esac
+
+MESSAGE=$@
+case $action in
+send)
+ send_message "$MESSAGE"
+ ;;
+html)
+ HTML="true"
+ send_message "$MESSAGE"
+ ;;
+esac
ArKa projects. All rights to me, and your next child right arm.