From e59d8d0a37db29f0ff88ca7a2b24e8c2c86f279e Mon Sep 17 00:00:00 2001 From: r000t Date: Mon, 16 Dec 2024 15:20:38 -0500 Subject: [PATCH] Initial commit with actual script and README --- README.md | 39 ++++++++++++++++++++++++++++++- ding | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100755 ding diff --git a/README.md b/README.md index b8600af..5713766 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,40 @@ # ding -The fastest way to set up push notifications on-demand \ No newline at end of file +The fastest way to set up push notifications on-demand using [ntfy.sh](https://ntfy.sh/). +Install the ntfy.sh app on your phone, drop this script into your $PATH, set the topic on both ends, and you're done. +Follow any long running command with `ding` to get a push notification when it finishes. + +It's that easy. ntfy.sh does not require registration. + +## Installation + +Copy script to a directory in your $PATH, such as `/usr/local/bin` +`mv ding /usr/local/bin/ding` + + +Edit the `NTFY_TOPIC` variable at the top of the file to a topic of your choosing. +```bash +# Define your ntfy.sh topic +NTFY_TOPIC="your-topic-name" +``` +Treat this like a password. Anybody who knows the topic can see and send notifications. + +## How to Use It + +Follow any long-running command with `ding`. + +`make && make install; ding` + +## Options + +### Title + +Any text after `ding` will become the title of the notification. + +`ding Task Completed.` + +### Priority + +Priority in ntfy.sh represents a "preset" for how intrusive the notification is. +`--min`, `--low`, `--high`, and `--max` correspond to these options. +Read the [message priority](https://docs.ntfy.sh/publish/#message-priority) docs for more information. diff --git a/ding b/ding new file mode 100755 index 0000000..80c9268 --- /dev/null +++ b/ding @@ -0,0 +1,69 @@ +#!/bin/bash + +# Define your ntfy.sh topic +NTFY_TOPIC="YOUR_TOPIC_GOES_HERE" +NTFY_BASE_URL="https://ntfy.sh" + +# Default titles, tags, and messages +SUCCESS_TITLE="Ding: Succeess" +SUCCESS_TAG="heavy_check_mark" +SUCCESS_MESSAGE="Command completed successfully." +FAILURE_TITLE="Ding: Failure" +FAILURE_TAG="x" +FAILURE_MESSAGE="Command encountered an error." + +# Parse arguments for priority or overrides +CUSTOM_MESSAGE="" +CUSTOM_TITLE="" +while [[ $# -gt 0 ]]; do + case "$1" in + --message) + CUSTOM_MESSAGE="$2" + shift 2 + ;; + --max) + PRIORITY="5" + shift + ;; + --high) + PRIORITY="4" + shift + ;; + --low) + PRIORITY="2" + shift + ;; + --min) + PRIORITY="1" + shift + ;; + *) + CUSTOM_TITLE="$1" + shift + ;; + esac +done + +# Capture the exit status of the last command +STATUS_CODE=$? + +# Determine the notification content +if [ $STATUS_CODE -eq 0 ]; then + TITLE="${CUSTOM_TITLE:-$SUCCESS_TITLE}" + TAG="$SUCCESS_TAG" + MESSAGE="${CUSTOM_MESSAGE:-$SUCCESS_MESSAGE}" +else + TITLE="${CUSTOM_TITLE:-$FAILURE_TITLE}" + TAG="$FAILURE_TAG" + MESSAGE="${CUSTOM_MESSAGE:-$FAILURE_MESSAGE} Exit code: $STATUS_CODE." +fi + +# Send the notification +curl -X POST "$NTFY_BASE_URL/$NTFY_TOPIC" \ + -H "Title: $TITLE" \ + -H "Tags: $TAG" \ + -H "Priority: $PRIORITY" \ + -d "$MESSAGE" + +# Optional: echo the notification for debugging +echo "Notification sent with title: $TITLE, tag: $TAG, message: $MESSAGE, priority: $PRIORITY, and status code: $STATUS_CODE"