Skip to content

Commit

Permalink
feat: add the ability to send notification through Pushover (#57)
Browse files Browse the repository at this point in the history
This commit adds the ability for people to send notifications through [pushover](https://pushover.net/).

---------

Co-authored-by: Mathieu Tanguay <[email protected]>
  • Loading branch information
lunfel and mathieu-pillar authored Jan 26, 2025
1 parent 06f7038 commit 8698223
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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'`).
Expand Down
31 changes: 31 additions & 0 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <bot_id> <chat_id> <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 <token> <user_id> <title> <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() {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions scripts/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 8698223

Please sign in to comment.