matrix-commander als Kommandozeilen-basierter Matrix-Client

Autor : Gerd Raudenbusch
Stand : 08.02.2024

Der matrix-commander ist ein Chatclient für das Matrix-Protokoll. Damit kann man ziemlich alle Client-Operationen skripten, um zum Beispiel eine Bridge oder einen Bot zu implementieren.

Installation

Man installiert ihn am besten mit dem folgenden Befehl :

sudo apt update && sudo apt -y upgrade && sudo apt install -y python3 python3-pip && sudo pip3 install matrix-commander

Inbetriebnahme

Die folgenden Schritte sind zur Inbetriebnahme nötig :

Senden

Nun ist der Kommandozeilen-Client betriebsbereit und man kann z. B. mit matrix-commander -m "First message!" eine Nachricht oder mit matrix-commander -f <dateiname>" eine Datei senden.

Empfangen

Der folgende Befehl dient zum Empfang der Nachrichten in eine Datei : matrix-commander --listen-forever --listen-self >./matrixchat.txt 2>/dev/null

Proof of concept bot

Das folgende Shellskript implementiert einen Chatbot, der mit "Voila!" antwortet, wenn man "Ping" sendet :

#!/bin/bash

MYPID="$$"
startbg () {
	eval "$(realpath ${0}) >/dev/null 2>&1 &"
	name=$(basename ${0})
        ALLPIDS=$(pgrep ${name})
        PIDS=$(echo "${ALLPIDS}" | grep -v "${MYPID}" | tr '\n' ' ')
	echo "Started $PIDS"
}

stopbg () {
	name=$(basename ${0})
	ALLPIDS=$(pgrep ${name})
	PIDS=$(echo "${ALLPIDS}" | grep -v "${MYPID}" | tr '\n' ' ')
	for pid in ${PIDS}; do 
		echo "Killing ${pid}"
		kill -s SIGKILL ${pid}
	done
}

if [ "${1}" == "start" ]; then
	startbg
	exit $?
elif [ "${1}" == "stop" ]; then
	stopbg
	exit $?
elif [ "${1}" == "restart" ]; then
	stopbg
	startbg
	exit $?
fi

botmessages="/var/log/matrixbot-messages.txt"
rm -f ${botmessages}

python3 --version >/dev/null 2>&1
if [ "$?" != "0" ]; then
	echo "Must install python3"
	sudo apt update && sudo apt -y upgrade && sudo apt install -y python3 python3-pip
fi

matrix-commander --version >/dev/null 2>&1
if [ "$?" != "0" ]; then
	echo "Must install matrix-commander"
	sudo pip3 install matrix-commander
fi

inotifywait >/dev/null 2>&1
if [ "$?" != "1" ]; then
	echo "Must install inotify tools"
	sudo apt update && sudo apt -y upgrade && sudo apt-get install inotify-tools
fi

processmsg () {
	if [ "${1}" == "Ping" ]; then
		echo "${0} : PROCESSING ${1}"
		matrix-commander -m "Voilà!" >/dev/null 2>&1
	else
		echo "The message '${1}' has no scripted reactions"
	fi
}

killold () {
	echo "Removing old instances of matrix-commander..."
	kill -9 $(ps axn | grep matrix-commander | grep -v grep | sed "s/^ *//g" | cut -d " " -f1 | tr '\n' ' ')
}

trap 'killold' SIGINT SIGTERM

if [ ! -f ${HOME}/.config/matrix-commander/credentials.json ]; then
	echo "There are no credentials saved, yet. Please create a default room for the bot and register credentials :"
	matrix-commander --login password
	if [ "$?" == "0" ] && [ -f ./credentials.json ]; then
		mkdir -p ${HOME}/.config/matrix-commander
		mv -i ./credentials.json $HOME/.config/matrix-commander
		mkdir -p ${HOME}/.local/share/matrix-commander 
		mv -i ./store ${HOME}/.local/share/matrix-commander
	else
		echo "Exitting due to errors"
		exit 1
	fi
else
	echo "Logging in"
	matrix-commander --credentials ${HOME}/.config/matrix-commander/credentials.json >/dev/null 2>&1
	if [ "$?" != "0" ]; then
		echo "Error logging in"
		exit 1
	fi
	echo "Starting listener for messages, dropping files in ${botmessages}"
	matrix-commander --listen forever --listen-self >${botmessages} 2>/dev/null &
	while inotifywait -e modify "${botmessages}" >/dev/null 2>&1; do
		lastline="$(tail -1 ${botmessages})"
		operation="$(echo ${lastline} | cut -d "|" -f1)"
		sender="$(echo ${lastline} | cut -d "|" -f2 | sed "s/^ //g; s/sender //g")"
		timestamp="$(echo ${lastline} | cut -d "|" -f3 | sed "s/^ //g")"
		message="$(echo ${lastline} | cut -d "|" -f4 | sed "s/^ //g")"
		if [ "$(echo "${operation}" | grep -c "Message received")" != "0" ]; then
			echo "${0}: [${timestamp} ${sender}] : ${message}"
			processmsg "${message}"
		fi
	done
fi


Zurück zur Hauptseite