#!/bin/bash #****************************************** # TalkLib.sh - A library for interacting with Nextcloud Spreed app # # This script provides functions interact with Nextcloud Spreed/Talk app. # It uses the Nextcloud OCS API to retrieve data from server in JSON format. # by Oliver Zienert - Date: 2025-06-17 #****************************************** #this constants is used to control the debug output #Set libDebug to true to enable debug output, false to disable libDebug=false # logging functions # These functions are used to log messages to the console # Usage: info "sender" "Your message here" function info() { echo -e "[\033[1;34mINFO::$1\033[0m] - $2" >&2 } # Usage: error "sender" "Your message here" function error() { echo -e "[\033[1;31mERROR::$1\033[0m] - $2" >&2 } # Usage: debug "sender" "Your message here" (if libDebug is true) function debug() { if [ "$libDebug" == true ]; then echo -e "[\033[1;33mDEBUG::$1\033[0m] - $2" >&2 fi } #****************************************** # Server functions for Nextcloud Spreed app # These functions are used to interact with the Nextcloud Spreed app # They make HTTPS (http is not working) requests to the Nextcloud server and return JSON responses. # !! These functions do not validate parameters, so ensure that the parameters are correct before calling them. #****************************************** # Function to get all rooms of a user from Nextcloud Spreed app # Usage: getAllRoomsOfUser # Returns: JSON response containing rooms details or an empty string on failure #+ function getAllRoomsOfUser() { NC_URL=$1 NC_USER=$2 NC_APP_PASSWORD=$3 debug "getAllRoomsOfUser" "Getting all rooms for user: $NC_USER" response=$(curl --proto 'https' --proto-redir "https" -s -u "$NC_USER:$NC_APP_PASSWORD" "$NC_URL/ocs/v2.php/apps/spreed/api/v4/room?format=json" \ -H "OCS-APIRequest: true") echo $response } # Function to get details of a specific room from Nextcloud Spreed app # Usage: getRoomDetails # Returns: JSON response containing room details or an empty string on failure #- funktioniert noch nicht function getRoomDetails() { NC_URL=$1 NC_USER=$2 NC_APP_PASSWORD=$3 ROOM_ID=$4 debug "getRoomDetails" "Getting details for Room: $ROOM_ID" response=$(curl --proto 'https' --proto-redir "https" -s -u "$NC_USER:$NC_APP_PASSWORD" \ "$NC_URL/ocs/v2.php/apps/spreed/api/v4/room/$ROOM_ID?format=json" \ -H "OCS-APIRequest: true") debug "getRoomDetails" "Response: $response" echo $response } # Function to send a message to a specific room in Nextcloud Spreed app # Usage: sendMessage2Room #+ function sendMessage2Room() { NC_URL=$1 NC_USER=$2 NC_APP_PASSWORD=$3 ROOM_ID=$4 MESSAGE=$5 response=$(curl --proto 'https' --proto-redir 'https' -s -u "$NC_USER:$NC_APP_PASSWORD" -X POST \ -d "message=$MESSAGE" \ "$NC_URL/ocs/v2.php/apps/spreed/api/v1/chat/$ROOM_ID?format=json" \ -H "OCS-APIRequest: true" ) echo $response } # Function to get the context of a specific message in a room in Nextcloud Spreed app # Usage: getMessage # Returns: JSON response containing message context or an empty string on failure #funkioniert noch nicht function getMessage() { NC_URL=$1 NC_USER=$2 NC_APP_PASSWORD=$3 ROOM_ID=$4 MESSAGE_ID=$5 debug "getMessage" "Getting message context for Room: $ROOM_ID, Message ID: $MESSAGE_ID" response=$(curl --proto 'https' --proto-redir 'https' -s -u "$NC_USER:$NC_APP_PASSWORD" \ "$NC_URL/chat/$ROOM_ID/$MESSAGE_ID/context/?format=json" \ -H "OCS-APIRequest: true") echo $response } function getUsersInRoom() { NC_URL=$1 NC_USER=$2 NC_APP_PASSWORD=$3 ROOM_ID=$4 debug "getUsersInRoom" "Getting users in room: $ROOM_ID" response=$(curl --proto 'https' --proto-redir 'https' -s -u "$NC_USER:$NC_APP_PASSWORD" \ "$NC_URL//ocs/v2.php/apps/spreed/api/v4/room/$ROOM_ID/participants?format=json" \ -H "OCS-APIRequest: true") echo $response } # Function to send a file to a specific room in Nextcloud Spreed app # Usage: sendFile2Room #+ function sendFile2Room() { NC_URL=$1 NC_USER=$2 NC_APP_PASSWORD=$3 ROOM_ID=$4 LOCAL_FILENAME=$5 NC_FILENAME=$6 MESSAGE_ID=$7 debug "sendFile2Room" "Uploading file to Nextcloud: $LOCAL_FILENAME as $NC_FILENAME in room $ROOM_ID with message ID $MESSAGE_ID" #upload the file to Nextcloud curl -u $NC_USER:$NC_APP_PASSWORD -T $LOCAL_FILENAME "$NC_URL/remote.php/dav/files/$NC_USER/$NC_FILENAME" \ -H "OCS-APIRequest: true" \ -H "Content-Type: application/octet-stream" \ -H "X-Requested-With: XMLHttpRequest" if [ $? -ne 0 ]; then error "Failed to upload file $LOCAL_FILENAME to Nextcloud" return fi debug "sendFile2Room" "File $LOCAL_FILENAME uploaded successfully to Nextcloud as $NC_FILENAME" #publish the file to the room response=$(curl -s -u "$NC_USER:$NC_APP_PASSWORD" \ "$NC_URL/ocs/v2.php/apps/files_sharing/api/v1/shares?format=json" \ -X POST \ -H "OCS-APIRequest: true" \ -H "Content-Type: application/json" \ -d "{ \"shareType\": 10, \"shareWith\": \"$ROOM_ID\", \"path\": \"$NC_FILENAME\" }") if [ $? -ne 0 ]; then error "Failed to share file $NC_FILENAME with room $ROOM_ID" fi debug "sendFile2Room" "$response" echo $response } # Function to send a message with a file attachment to a specific room in Nextcloud Spreed app # Usage: sendMessageWithFile # Returns: JSON response containing the message ID or an error message on failure # This function first sends a message to the room, then uploads the file and attaches it to the message. # It returns the response from the file upload, which includes the message ID. # Note: The file must be accessible on the local filesystem where this script is run. # The file will be uploaded to Nextcloud and attached to the message. function sendMessageWithFile() { NC_URL=$1 NC_USER=$2 NC_APP_PASSWORD=$3 ROOM_ID=$4 LOCAL_FILENAME=$5 NC_FILENAME=$6 MESSAGE=$7 debug "sendMessageWithFile" "Sending message with file to room: $ROOM_ID" debug "sendMessageWithFile" "Local filename: $LOCAL_FILENAME" debug "sendMessageWithFile" "Serverfilename: $NC_FILENAME" debug "sendMessageWithFile" "Message: $MESSAGE" #erzeuge Message an den die Datei angehängt werden soll und speichere die ID der Nachricht in mesgId mesgId=$(sendMessage2Room "$NC_URL" "$NC_USER" "$NC_APP_PASSWORD" "$ROOM_TOKEN" "$MESSAGE" | jq -r '.ocs.data.id' ) debug "sendMessageWithFile" "Message sent with ID: $mesgId" #sende die Datei an die Nachricht #[todo check mesgId] debug "sendMessageWithFile" "uploading file $LOCAL_FILENAME to $NC_FILENAME" response=$(sendFile2Room "$NC_URL" "$NC_USER" "$NC_APP_PASSWORD" "$ROOM_TOKEN" "$LOCAL_FILENAME" "$NC_FILENAME" "$mesgId") echo $response } # Function to mark a message as read in a specific room in Nextcloud Spreed app # Usage: markMessageAsRead #+ function markMessageAsRead() { NC_URL=$1 NC_USER=$2 NC_APP_PASSWORD=$3 ROOM_ID=$4 MESSAGE_ID=$5 response=$(curl --proto 'https' --proto-redir 'https' -s -u "$NC_USER:$NC_APP_PASSWORD" -X POST \ "$NC_URL/ocs/v2.php/apps/spreed/api/v1/chat/$ROOM_ID/read?format=json" \ -H "OCS-APIRequest: true" \ -d "lastReadMessage=$MESSAGE_ID") echo $response } #erstellt einen neuen Raum mit dem angegebenen Benutzer function createOne2OneRoom() { NC_URL=$1 NC_USER=$2 NC_APP_PASSWORD=$3 TARGET_USER=$4 response=$(curl --proto 'https' --proto-redir 'https' -s -u "$NC_USER:$NC_APP_PASSWORD" -X POST \ "$NC_URL/ocs/v2.php/apps/spreed/api/v4/room?format=json" \ -H "OCS-APIRequest: true" \ -d "invite=$TARGET_USER" \ -d "type=1" ) echo $response } # Function to get details of a specific User from Nextcloud # Usage: getDetailsOfUser # Returns: JSON response containing Useraccount details or an empty string on failure function getDetailsOfUser() { NC_URL=$1 NC_USER=$2 NC_APP_PASSWORD=$3 NC_USER2Check=$4 debug "getDetailsOfUser" "Getting Account Details for user: $NC_USER2Check" response=$(curl --proto 'https' --proto-redir "https" -s -u "$NC_USER:$NC_APP_PASSWORD" \ "$NC_URL/ocs/v1.php/cloud/users/{$NC_USER2Check}?format=json" \ -H "OCS-APIRequest: true") echo $response } #abhängigkeits-Check for cmd in curl jq; do if ! command -v "$cmd" >/dev/null 2>&1; then error "TalkLib" "Benötigtes Programm '$cmd' ist nicht installiert. Bitte installieren und erneut versuchen." exit 1 fi done