Compare commits
2 Commits
dcab88019a
...
a278a8281b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a278a8281b | ||
|
|
c96b544d70 |
66
README.md
66
README.md
@ -1,3 +1,67 @@
|
|||||||
# gotifyer
|
# gotifyer
|
||||||
|
|
||||||
Gotifyer is a Bash script allows you to send notifications using the Gotify service in Docker. It checks whether the Gotify container is running, sends a notification, and logs the results.
|
## Overview
|
||||||
|
This Bash script allows you to send notifications using the Gotify service. It checks the health of the Gotify server before sending notifications and logs the results.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
- **curl**: This script uses `curl` to send HTTP requests.
|
||||||
|
- **Bash**: Ensure your system has Bash installed.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
Before using the script, you need to set the following configuration variables:
|
||||||
|
|
||||||
|
- `GOTIFY_URL`: The URL of your Gotify server endpoint for sending messages.
|
||||||
|
- Example: `"http://gotify.example.com/message"`
|
||||||
|
|
||||||
|
- `APP_TOKEN`: Your application token from Gotify, which authorizes the script to send notifications.
|
||||||
|
|
||||||
|
- `MESSAGE_TITLE`: The title of the notification you want to send.
|
||||||
|
|
||||||
|
- `MESSAGE`: The content of the notification you want to send.
|
||||||
|
|
||||||
|
- `LOG_FILE`: The file where the logs will be stored. The default is `gotify_notifications.log`.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
1. Clone or download the script to your local machine.
|
||||||
|
2. Open the script in a text editor and replace the configuration variables with your own values.
|
||||||
|
3. Make the script executable:
|
||||||
|
```bash
|
||||||
|
chmod +x gotify_notification.sh
|
||||||
|
```
|
||||||
|
4. Run the script:
|
||||||
|
```bash
|
||||||
|
./gotify_notification.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
## How It Works
|
||||||
|
1. The script checks the health of the Gotify server using the `/health` endpoint.
|
||||||
|
2. If the server is healthy, it sends a notification using the `curl` command.
|
||||||
|
3. The script logs the result of the notification attempt, including the time, message, and HTTP response status.
|
||||||
|
|
||||||
|
## Logging
|
||||||
|
Logs are written to the specified `LOG_FILE`. Each log entry includes a timestamp, making it easy to track notification attempts.
|
||||||
|
|
||||||
|
### Example Log Entry
|
||||||
|
```
|
||||||
|
2024-10-20 12:34:56 - Notification sent successfully: Your notification message.
|
||||||
|
```
|
||||||
|
|
||||||
|
## Error Handling
|
||||||
|
- If the Gotify server is not healthy, the script logs an error message and exits without sending a notification.
|
||||||
|
- If the notification fails to send, the script logs the HTTP status code received from the Gotify server.
|
||||||
|
|
||||||
|
## Customization
|
||||||
|
Feel free to modify the script as needed. You can change the notification's priority by adjusting the `priority` field in the JSON payload. Valid priorities are:
|
||||||
|
- 0: Min
|
||||||
|
- 1: Low
|
||||||
|
- 2: Normal
|
||||||
|
- 3: High
|
||||||
|
- 4: Emergency
|
||||||
|
- 5: Very High (default)
|
||||||
|
|
||||||
|
## License
|
||||||
|
This script is licensed under the GNU General Public License v3.0 or later. You are free to use, modify, and distribute this script under the same license. For more details, please refer to the [GNU General Public License](LICENSE).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Let me know if you need any further assistance!
|
||||||
60
gotifyer.sh
Normal file
60
gotifyer.sh
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
GOTIFY_URL="<YOUR_SERVER_URL>/message" # Example: "http://gotify.example.com/message"
|
||||||
|
APP_TOKEN="<YOUR_APP_TOKEN>"
|
||||||
|
MESSAGE_TITLE="<YOUR_MESSAGE_TITLE>"
|
||||||
|
MESSAGE="<YOUR_MESSAGE>"
|
||||||
|
HOSTNAME=$(hostname) # Get the hostname of the machine
|
||||||
|
TITLE="$HOSTNAME - $MESSAGE_TITLE" # Title with hostname prefixing the message
|
||||||
|
PRIORITY=5 # Notification priority (1-5)
|
||||||
|
LOG_FILE="gotify_notifications.log"
|
||||||
|
|
||||||
|
# Function to log messages
|
||||||
|
log_message() {
|
||||||
|
local message="$1"
|
||||||
|
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" >> "$LOG_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to check if the Gotify server is healthy
|
||||||
|
check_gotify_health() {
|
||||||
|
local base_url="${GOTIFY_URL%/*}" # Extract base URL (removes /message)
|
||||||
|
local health_url="$base_url/health" # Construct the health check URL
|
||||||
|
local response
|
||||||
|
|
||||||
|
# Make a request to the health endpoint and capture the response
|
||||||
|
response=$(curl --silent --write-out "%{http_code}" --output /dev/null "$health_url")
|
||||||
|
|
||||||
|
# Check if the response code is 200 (OK)
|
||||||
|
if [ "$response" -eq 200 ]; then
|
||||||
|
# Get the actual health status
|
||||||
|
health_status=$(curl --silent "$health_url")
|
||||||
|
echo "$health_status"
|
||||||
|
else
|
||||||
|
echo "Health check failed. HTTP status code: $response"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if the Gotify server is healthy
|
||||||
|
health_check_result=$(check_gotify_health)
|
||||||
|
if [[ $health_check_result != *"\"health\":\"green\""* ]]; then
|
||||||
|
log_message "Gotify server is down or not healthy: $health_check_result. Exiting."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Send notification
|
||||||
|
response=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$GOTIFY_URL" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-H "X-Gotify-Key: $APP_TOKEN" \
|
||||||
|
-d "{
|
||||||
|
\"title\": \"$TITLE\",
|
||||||
|
\"message\": \"$MESSAGE\",
|
||||||
|
\"priority\": $PRIORITY
|
||||||
|
}")
|
||||||
|
|
||||||
|
# Check response and log result
|
||||||
|
if [ "$response" -eq 200 ]; then
|
||||||
|
log_message "Notification sent successfully: $MESSAGE"
|
||||||
|
else
|
||||||
|
log_message "Failed to send notification. HTTP status code: $response. Title: $TITLE, Message: $MESSAGE, Priority: $PRIORITY"
|
||||||
|
fi
|
||||||
Loading…
Reference in New Issue
Block a user