From 86982232675550416e3986516014b3f150abfc1d Mon Sep 17 00:00:00 2001 From: Mathieu Tanguay Date: Sun, 26 Jan 2025 04:27:17 -0500 Subject: [PATCH] feat: add the ability to send notification through Pushover (#57) This commit adds the ability for people to send notifications through [pushover](https://pushover.net/). --------- Co-authored-by: Mathieu Tanguay --- README.md | 17 ++++++++++++++++- scripts/helpers.sh | 31 +++++++++++++++++++++++++++++++ scripts/variables.sh | 8 ++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 82e2d1e..182e5d8 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Tmux plugin to notify you when processes are finished. * [Change monitor update period](#change-monitor-update-period) * [Add additional shell suffixes](#add-additional-shell-suffixes) * [Enable telegram channel notifications](#enable-telegram-channel-notifications) + * [Enable Pushover notifications](#enable-pushover-notifications) * [Execute custom notification commands](#execute-custom-notification-commands) * [How does it work](#how-does-it-work) * [Other use cases](#other-use-cases) @@ -110,7 +111,7 @@ The Tmux notify script uses your shell prompt suffix to check whether a command > \[!WARNING]\ > This feature requires [wget](https://www.gnu.org/software/wget/) to be installed on your system. -By default, the tool only sent operating system notifications. It can, however, also send a message to a user-specified telegram channel. +By default, the tool only sends operating system notifications. It can, however, also send a message to a user-specified telegram channel. > Put `set -g @tnotify-telegram-bot-id 'your telegram bot id'` and `set -g @tnotify-telegram-channel-id 'your channel id'` in the `.tmux.conf` config file to enable this. @@ -125,6 +126,20 @@ Additionally, you can use the `set -g @tnotify-telegram-all 'on'` option to send > \[!NOTE]\ > You can get your telegram bot id by creating a bot using [BotFather](https://core.telegram.org/bots#6-botfather) and your channel id by sending your channel invite link to the `@username_to_id_bot` bot. +### Enable Pushover notifications + +> \[!WARNING]\ +> This feature requires [curl](https://curl.se/) to be installed on your system. + +By default, the tool only sends operating system notifications. It can, however, also send a message to a user-specified pusher user or group. + +> Put `set -g @tnotify-pushover-token 'your pushover application token'` and `set -g @tnotify-pushover-user 'your pushover user or group identifier'` in the `.tmux.conf` config file to enable this. + +> You may optionally put `set -g @tnotify-pushover-title 'The title of the message'` to override the default title + +> \[!NOTE]\ +> You can create a free pushover account at [pushover.net](https://pushover.net/). + ### Execute custom notification commands You can execute a custom command after a process has finished by putting `set -g @tnotify-custom-cmd 'your custom command here'` in the `.tmux.conf` file. The custom command is executed in the pane where the process has finished. If you want to execute multiple commands, you can also put them in a bash script and execute this script (i.e. `set -g @tnotify-custom-cmd 'bash /path/to/script.sh'`). diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 5c96d59..4957e44 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -48,12 +48,36 @@ telegram_available() { [ -n "$telegram_id" ] && [ -n "$telegram_chat_id" ] } +# Check if pushover token and pushover user are set +pushover_available() { + local pushover_token="$(get_tmux_option "$tmux_notify_pushover_token" "$tmux_notify_pushover_token_default")" + local pushover_user="$(get_tmux_option "$tmux_notify_pushover_user" "$tmux_notify_pushover_user_default")" + [ -n "$pushover_token" ] && [ -n "$pushover_user" ] +} + # Send telegram message # Usage: send_telegram_message send_telegram_message() { wget --spider "https://api.telegram.org/bot$1/sendMessage?chat_id=$2&text=${3// /%20}" &> /dev/null } +# Send a message over https://pushover.net/ +# Usage: send_pushover_message <message> +# token is the application token on pushover.net +# user_id is the user or group id of whom will receive the notification +# the title of the message: https://pushover.net/api#registration +# message is the message sent +send_pushover_message() { + curl -X POST --location "https://api.pushover.net/1/messages.json" \ + -H "Content-Type: application/json" \ + -d "{ + \"token\": \"$1\", + \"user\": \"$2\", + \"message\": \"$4\", + \"title\": \"$3\" + }" &> /dev/null +} + # Send notification # Usage: notify <message> <title> <send_telegram> notify() { @@ -81,6 +105,13 @@ notify() { telegram_chat_id="$(get_tmux_option "$tmux_notify_telegram_channel_id" "$tmux_notify_telegram_channel_id_default")" send_telegram_message $telegram_bot_id $telegram_chat_id "$1" fi + + if pushover_available; then + local pushover_token="$(get_tmux_option "$tmux_notify_pushover_token" "$tmux_notify_pushover_token_default")" + local pushover_user="$(get_tmux_option "$tmux_notify_pushover_user" "$tmux_notify_pushover_user_default")" + local pushover_title="$(get_tmux_option "$tmux_notify_pushover_title" "$tmux_notify_pushover_title_default")" + send_pushover_message "$pushover_token" "$pushover_user" "$pushover_title" "$1" + fi # trigger visual bell # your terminal emulator can be setup to set URGENT bit on visual bell diff --git a/scripts/variables.sh b/scripts/variables.sh index 50156c7..d8f28b4 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -39,3 +39,11 @@ export tmux_notify_telegram_all="@tnotify-telegram-all" export tmux_notify_telegram_bot_id_default="" export tmux_notify_telegram_channel_id_default="" export tmux_notify_telegram_all_default="off" + +# Pushover notification settings +export tmux_notify_pushover_token="@tnotify-pushover-token" +export tmux_notify_pushover_user="@tnotify-pushover-user" +export tmux_notify_pushover_title="@tnotify-pushover-title" +export tmux_notify_pushover_token_default="" +export tmux_notify_pushover_user_default="" +export tmux_notify_pushover_title_default="Tmux Notify"