Skip to content

Commit

Permalink
Merge pull request #35 from ChanderG/add_telegram_alert
Browse files Browse the repository at this point in the history
add telegram alert feature
  • Loading branch information
rickstaa authored Jun 3, 2023
2 parents e5a6603 + 4132eec commit fd7bac7
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 42 deletions.
40 changes: 27 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

Tmux plugin to notify you when processes are complete.

Notification is via libnotify and visual bell raised in the tmux window. Visual bells can be mapped (in the terminal level) to X11 urgency bit and handled by your window manager.
Notifications are done via libnotify, and visual bells are raised in the tmux window. Visual bells can be mapped (in the terminal level) to X11 urgency bit and handled by your window manager.

## Use cases

- When you have already started a process in a pane and wish to be notified, that is when you can't use a manual trigger.
- Working in different containers (Docker) -> can't choose the shell -> and can't use a shell level feature
- Working over ssh, but your tmux is on the client-side
- Working in different containers (Docker) -> can't choose the shell -> and can't use a shell-level feature
- Working over ssh, but your Tmux is on the client-side

## Install

Expand All @@ -28,11 +28,11 @@ Use `prefix + I` to install.

## Usage

`prefix + m`: Start monitoring a pane and notify when it finishes.
- `prefix + m`: Start monitoring a pane and notify when it finishes.

`prefix + alt + m`: Start monitoring a pane, return it in focus and notify when it finishes.
- `prefix + alt + m`: Start monitoring a pane, return it in focus and notify when it finishes.

`prefix + M`: Cancel monitoring of a pane.
- `prefix + M`: Cancel monitoring of a pane.

## Pre-requisites

Expand All @@ -47,13 +47,13 @@ Use `prefix + I` to install.

### Enable verbose notification

By default, the notification text is set to `Tmux pane task completed!`. We have also included a verbose output option. When enabled, information about the pane, window, and session the task has completed is given.
The notification text is defaulted to `Tmux pane task completed!`. We have also included a verbose output option. Information about the pane, window, and session the task has completed is given when enabled.

Put `set -g @tnotify-verbose 'on'` in the `.tmux.conf` config file to enable this.
To enable this, put `set -g @tnotify-verbose 'on'` in the `.tmux.conf` config file.

#### Change the verbose notification message

To change the verbose notification text, put `set -g @tnotify-verbose-msg 'put your notification text here'` in the `.tmux.conf` config file. You can use all the tmux variables in your notification text. Some useful tmux aliases are:
To change the verbose notification text, put `set -g @tnotify-verbose-msg 'put your notification text here'` in the `.tmux.conf` config file. You can use all the Tmux variables in your notification text. Some useful Tmux aliases are:

- `#D`: Pane id
- `#P`: Pane index
Expand All @@ -62,19 +62,33 @@ To change the verbose notification text, put `set -g @tnotify-verbose-msg 'put y
- `#I`: Window index
- `#W`: Window name

For the full list of aliases and variables you are referred to the `FORMATS` section of the [tmux manual](http://man7.org/linux/man-pages/man1/tmux.1.html).
For the complete list of aliases and variables, you are referred to the `FORMATS` section of the [tmux manual](http://man7.org/linux/man-pages/man1/tmux.1.html).

### Enable telegram channel notifications

By default, the notification is only sent to the operating system. We have also included a telegram channel notification option. When enabled, a notification is sent to a user-specified telegram channel.

Put both `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. Additionally, you can use the `set -g @tnotify-telegram-all 'on'` option to send all notifications to telegram. After enabling this option, the following key bindings are available:

- `prefix + ctrl + M`: Start monitoring pane and notify in bash and telegram when it finishes.

- `prefix + ctrl + alt + M`: Start monitoring a pane, return it in focus and notify in bash and telegram when it finishes.

> **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.
### Change monitor update period

By default, the monitor sleep period is set to 10 seconds. This means that tmux-notify checks the pane activity every 10 seconds.

Put `set -g @tnotify-sleep-duration 'desired duration" in the`.tmux.conf\` file to change this duration.
Put `set -g @tnotify-sleep-duration 'desired duration'` in the `.tmux.conf` file to change this duration.

**NOTE:** Keep in mind that there is a trade-off between notification speed (short sleep duration) and the amount of memory this tool needs.
> **Warning**
> Remember that there is a trade-off between notification speed (short sleep duration) and the amount of memory this tool needs.
### Add additional shell suffixes

The tmux notify script uses your shell prompt suffix to check whether a command has finished. It looks for the `$`, `#` and `%` suffixes by default. If you customise your shell to use different shell suffixes, you can add them by putting `set -g @tnotify-prompt-suffixes 'put your comma-separated bash suffix list here'` in the`.tmux.conf` file.
The Tmux notify script uses your shell prompt suffix to check whether a command has finished. By default, it looks for the `$`, `#` and `%` suffixes. If you customise your shell to use different shell suffixes, you can add them by putting `set -g @tnotify-prompt-suffixes 'put your comma-separated bash suffix list here'` in the `.tmux.conf` file.

Feel free to open [a pull](https://github.com/ChanderG/tmux-notify/pulls) request or [issue](https://github.com/ChanderG/tmux-notify/issues) if you think your shell prompt suffix should be included by default.

Expand Down
54 changes: 54 additions & 0 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Additional functions that are used in the main scripts.

# Get tmux option
# Usage: get_tmux_option <option> <default_value>
get_tmux_option() {
local option="$1"
local default_value="$2"
Expand All @@ -15,13 +16,66 @@ get_tmux_option() {
}

# Set tmux option
# Usage: set_tmux_option <option> <value>
set_tmux_option() {
local option="$1"
local value="$2"
tmux set-option -gq "$option" "$value"
}

# Escape globbing charaters
# Usage: escape_glob_chars <string>
escape_glob_chars() {
echo "$1" | sed 's/[.[\*^$()+?{|]/\\&/g'
}

# Check if verbose option is enabled
verbose_enabled() {
local verbose_value="$(get_tmux_option "$verbose_option" "$verbose_default")"
[ "$verbose_value" == "on" ]
}

# Check if the telegram alert all option is enabled
telegram_all_enabled() {
local alert_all="$(get_tmux_option "$tmux_notify_telegram_all" "$tmux_notify_telegram_all_default")"
[ "$alert_all" == "on" ]
}

# Check if telegram bot id and chat id are set
telegram_available() {
local telegram_id="$(get_tmux_option "$tmux_notify_telegram_bot_id" "$tmux_notify_telegram_bot_id_default")"
local telegram_chat_id="$(get_tmux_option "$tmux_notify_telegram_channel_id" "$tmux_notify_telegram_channel_id_default")"
[ -n "$telegram_id" ] && [ -n "$telegram_chat_id" ]
}

# Send telegram message
# Usage: send_telegram_message <bot_id> <chat_id> <message>
send_telegram_message() {
curl "https://api.telegram.org/bot$1/sendMessage?chat_id=$2&text=$3" &> /dev/null
}

# Send notification
# Usage: notify <message> <send_telegram>
notify() {
# Switch notification method based on OS
if [[ "$OSTYPE" =~ ^darwin ]]; then # If macOS
osascript -e 'display notification "'"$1"'" with title "tmux-notify"'
else
# notify-send does not always work due to changing dbus params
# see https://superuser.com/questions/1118878/using-notify-send-in-a-tmux-session-shows-error-no-notification#1118896
notify-send "$1"
fi

# Send telegram message if telegram variables are set, and telegram alert all is
# enabled or if the $2 argument is set to true
if telegram_available && (telegram_all_enabled || [ "$2" == "true" ]); then
telegram_bot_id="$(get_tmux_option "$tmux_notify_telegram_bot_id" "$tmux_notify_telegram_bot_id_default")"
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" &> /dev/null
fi

# trigger visual bell
# your terminal emulator can be setup to set URGENT bit on visual bell
# for eg, Xresources -> URxvt.urgentOnBell: true
tmux split-window "echo -e \"\a\" && exit"
}
34 changes: 6 additions & 28 deletions scripts/notify.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
# Usage: notify <refocus> <telegram>
## -- Start monitoring script

# Get current directory
Expand All @@ -10,23 +11,6 @@ source "${CURRENT_DIR}/variables.sh"

## Functions

# Send notification
notify() {
# Switch notification method based on OS
if [[ "$OSTYPE" =~ ^darwin ]]; then # If macOS
osascript -e 'display notification "'"$1"'" with title "tmux-notify"'
else
# notify-send does not always work due to changing dbus params
# see https://superuser.com/questions/1118878/using-notify-send-in-a-tmux-session-shows-error-no-notification#1118896
notify-send "$1"
fi

# trigger visual bell
# your terminal emulator can be setup to set URGENT bit on visual bell
# for eg, Xresources -> URxvt.urgentOnBell: true
tmux split-window "echo -e \"\a\" && exit"
}

# Handle cancelation of monitor job
on_cancel()
{
Expand All @@ -42,12 +26,6 @@ on_cancel()
}
trap 'on_cancel' TERM

# Check if verbose option is enabled
verbose_enabled() {
local verbose_value="$(get_tmux_option "$verbose_option" "$verbose_default")"
[ "$verbose_value" != "on" ]
}

## Main script

# Monitor pane if it is not already monitored
Expand All @@ -60,11 +38,11 @@ if [[ ! -f "$PID_FILE_PATH" ]]; then # If pane not yet monitored
tmux display-message "Monitoring pane..."

# Construct tnotify finish message
if verbose_enabled; then # If @tnotify-verbose is disabled
complete_message="Tmux pane task completed!"
else # If @tnotify-verbose is enabled
if verbose_enabled; then # If @tnotify-verbose is enabled
verbose_msg_value="$(get_tmux_option "$verbose_msg_option" "$verbose_msg_default")"
complete_message=$(tmux display-message -p "$verbose_msg_value")
else # If @tnotify-verbose is disabled
complete_message="Tmux pane task completed!"
fi

# Create bash suffix list
Expand All @@ -86,12 +64,12 @@ if [[ ! -f "$PID_FILE_PATH" ]]; then # If pane not yet monitored
# if so, break and notify
if echo "$output" | tail -n2 | grep -e $prompt_suffixes &> /dev/null; then
# tmux display-message "$@"
if [[ "$1" == "refocus" ]]; then
if [[ "$1" == "true" ]]; then
tmux switch -t \$"$SESSION_ID"
tmux select-window -t @"$WINDOW_ID"
tmux select-pane -t %"$PANE_ID"
fi
notify "$complete_message"
notify "$complete_message" $2
break
fi

Expand Down
8 changes: 8 additions & 0 deletions scripts/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ export verbose_msg_default="(#S, #I:#P) Tmux pane task completed!"
# Monitor checker interval
export monitor_sleep_duration="@tnotify-sleep-duration"
export monitor_sleep_duration_default=10

# Telegram notification settings
export tmux_notify_telegram_bot_id="@tnotify-telegram-bot-id"
export tmux_notify_telegram_channel_id="@tnotify-telegram-channel-id"
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"
7 changes: 6 additions & 1 deletion tnotify.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ fi
tmux unbind-key m
tmux unbind-key M
tmux unbind-key M-m
tmux unbind-key C-m
tmux unbind-key C-M-m

tmux bind-key m run-shell -b "$CURRENT_DIR/scripts/notify.sh"
tmux bind-key M run-shell -b "$CURRENT_DIR/scripts/cancel.sh"
tmux bind-key M-m run-shell -b "$CURRENT_DIR/scripts/notify.sh refocus"
tmux bind-key M-m run-shell -b "$CURRENT_DIR/scripts/notify.sh true"
tmux bind-key C-m run-shell -b "$CURRENT_DIR/scripts/notify.sh false true"
tmux bind-key C-M-m run-shell -b "$CURRENT_DIR/scripts/notify.sh true true"

0 comments on commit fd7bac7

Please sign in to comment.