Last active
November 8, 2025 17:01
-
-
Save phdelodder/994fd201518fc9ff8cc668049903584c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ============================================================================== | |
| # REQUIRED INPUT VARIABLES (set these) | |
| # ============================================================================== | |
| # The file path on your local Home Automation system (e.g., from the camera) | |
| IMAGE_PATH="$1" | |
| # The name or token of your Nextcloud Talk conversation | |
| # For a group chat: This is usually the name of the chat room. | |
| # For a 1:1 chat: This is the username of the other party. | |
| YOUR_TALK_CHAT_ID="$2" | |
| # The optional follow-up message. Leave empty ("") to use the default fallback message. | |
| FOLLOW_UP_MESSAGE="$3" | |
| # If empty, the message will be set to: "New file has been shared: [filename]" | |
| # ============================================================================== | |
| # NEXTCLOUD AUTH & PATH CONFIGURATION (set these) | |
| # ============================================================================== | |
| NEXTCLOUD="https://nextcloud" | |
| USER="User" | |
| PASSWORD="Password" | |
| # The destination FOLDER inside your Nextcloud Files (e.g., 'Snapshots') | |
| REMOTE_FOLDER="/sharing" | |
| # ============================================================================== | |
| # LOGGING CONFIGURATION | |
| # ============================================================================== | |
| # Define the path to your log file | |
| LOG_FILE="/var/log/nextcloud_talk_notification.log" | |
| # Define the logging function to replace echo | |
| log() { | |
| # Prepend the current timestamp and append the message to the log file | |
| echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" | |
| } | |
| # Note: 'tee -a' prints to the screen (stdout) AND appends to the log file. | |
| # If you want to ONLY log to the file (silent execution), use: | |
| # echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" | |
| # ============================================================================== | |
| # SCRIPT LOGIC (Automated Path Generation) | |
| # ============================================================================== | |
| # 1. Extract the filename from the local path (e.g., "lastDetection.png") | |
| FILENAME=$(basename "$IMAGE_PATH") | |
| # 2. Construct the full remote path | |
| # Example: /Unifi-EVA/lastDetection.png | |
| REMOTE_PATH="$REMOTE_FOLDER/$FILENAME" | |
| # 3. SET FALLBACK MESSAGE | |
| # Check if the user-defined message is empty. If so, set the generic message. | |
| if [[ -z "$FOLLOW_UP_MESSAGE" ]]; then | |
| FOLLOW_UP_MESSAGE="New file has been shared: $FILENAME" | |
| fi | |
| log "--- Nextcloud Talk Image Notification ---" | |
| log "Image Source: $IMAGE_PATH" | |
| log "Image Filename: $FILENAME" | |
| log "Nextcloud Destination: $REMOTE_PATH" | |
| log "Talk Target: $YOUR_TALK_CHAT_ID" | |
| # ============================================================================== | |
| # SCRIPT EXECUTION | |
| # ============================================================================== | |
| # 1. DELETE the old remote file (WebDAV) | |
| log "1. Attempting to delete old remote file at $REMOTE_PATH..." | |
| delete_response=$(curl -s -X DELETE -u "$USER:$PASSWORD" \ | |
| "$NEXTCLOUD/remote.php/dav/files/$USER$REMOTE_PATH" -w "%{http_code}") | |
| if [[ "$delete_response" -eq 204 || "$delete_response" -eq 404 ]]; then | |
| log " -> Delete successful or file not found (HTTP $delete_response). Proceeding with upload." | |
| elif [[ "$delete_response" -eq 404 ]]; then | |
| # This is the check for "Does the file exist?" | |
| log " -> Old file does not exist (HTTP 404). Proceeding with upload." | |
| else | |
| log " -> WARNING: Delete returned unexpected HTTP $delete_response." | |
| fi | |
| # 2. UPLOAD the Image (WebDAV) | |
| log "2. Uploading image to $REMOTE_PATH..." | |
| upload_response=$(curl -s -X PUT -u "$USER:$PASSWORD" \ | |
| "$NEXTCLOUD/remote.php/dav/files/$USER$REMOTE_PATH" \ | |
| -T "$IMAGE_PATH" -w "%{http_code}") | |
| if [[ "$upload_response" -ge 200 && "$upload_response" -le 204 ]]; then | |
| log " -> Upload successful (HTTP $upload_response)" | |
| else | |
| log " -> ERROR: Upload failed with HTTP $upload_response. Aborting." | |
| exit 1 | |
| fi | |
| # 3. WAIT for Preview Generation (CRITICAL STEP for thumbnail) | |
| log "3. Waiting 3 seconds for Nextcloud thumbnail generation..." | |
| sleep 3 | |
| # 4. SHARE the file to the Talk Chat (Files Sharing API) | |
| log "4. Sharing image to chat via Files Sharing API..." | |
| share_response=$(curl -s -X POST -u "$USER:$PASSWORD" \ | |
| -H "Content-Type: application/json" \ | |
| -H "Accept: application/json" \ | |
| -H "OCS-APIRequest: true" \ | |
| -d "{\"shareWith\":\"$YOUR_TALK_CHAT_ID\",\"shareType\":10,\"path\":\"$REMOTE_PATH\"}" \ | |
| "$NEXTCLOUD/ocs/v2.php/apps/files_sharing/api/v1/shares" -w "%{http_code}"\ | |
| -o /dev/null) | |
| if [[ "$share_response" -ne 200 ]]; then | |
| log " -> ERROR: Share failed with HTTP $share_response. Image may not be visible." | |
| fi | |
| # 5. SEND FOLLOW-UP MESSAGE | |
| # This message (either user-defined or the fallback) is necessary to force a client refresh. | |
| log "5. Sending follow-up message: \"$FOLLOW_UP_MESSAGE\"" | |
| curl -s -X POST -u "$USER:$PASSWORD" \ | |
| -H "Content-Type: application/json" \ | |
| -H "OCS-APIRequest: true" \ | |
| -d "{\"message\":\"$FOLLOW_UP_MESSAGE\"}" \ | |
| "$NEXTCLOUD/ocs/v2.php/apps/spreed/api/v1/chat/$YOUR_TALK_CHAT_ID" \ | |
| -o /dev/null | |
| log "--- Script Finished ---" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment