Skip to content

Commit fd7bac7

Browse files
authored
Merge pull request #35 from ChanderG/add_telegram_alert
add telegram alert feature
2 parents e5a6603 + 4132eec commit fd7bac7

File tree

5 files changed

+101
-42
lines changed

5 files changed

+101
-42
lines changed

README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
Tmux plugin to notify you when processes are complete.
1010

11-
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.
11+
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.
1212

1313
## Use cases
1414

1515
- 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.
16-
- Working in different containers (Docker) -> can't choose the shell -> and can't use a shell level feature
17-
- Working over ssh, but your tmux is on the client-side
16+
- Working in different containers (Docker) -> can't choose the shell -> and can't use a shell-level feature
17+
- Working over ssh, but your Tmux is on the client-side
1818

1919
## Install
2020

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

2929
## Usage
3030

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

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

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

3737
## Pre-requisites
3838

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

4848
### Enable verbose notification
4949

50-
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.
50+
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.
5151

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

5454
#### Change the verbose notification message
5555

56-
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:
56+
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:
5757

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

65-
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).
65+
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).
66+
67+
### Enable telegram channel notifications
68+
69+
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.
70+
71+
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:
72+
73+
- `prefix + ctrl + M`: Start monitoring pane and notify in bash and telegram when it finishes.
74+
75+
- `prefix + ctrl + alt + M`: Start monitoring a pane, return it in focus and notify in bash and telegram when it finishes.
76+
77+
> **Note**
78+
> 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.
6679
6780
### Change monitor update period
6881

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

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

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

77-
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.
91+
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.
7892

7993
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.
8094

scripts/helpers.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Additional functions that are used in the main scripts.
44

55
# Get tmux option
6+
# Usage: get_tmux_option <option> <default_value>
67
get_tmux_option() {
78
local option="$1"
89
local default_value="$2"
@@ -15,13 +16,66 @@ get_tmux_option() {
1516
}
1617

1718
# Set tmux option
19+
# Usage: set_tmux_option <option> <value>
1820
set_tmux_option() {
1921
local option="$1"
2022
local value="$2"
2123
tmux set-option -gq "$option" "$value"
2224
}
2325

2426
# Escape globbing charaters
27+
# Usage: escape_glob_chars <string>
2528
escape_glob_chars() {
2629
echo "$1" | sed 's/[.[\*^$()+?{|]/\\&/g'
2730
}
31+
32+
# Check if verbose option is enabled
33+
verbose_enabled() {
34+
local verbose_value="$(get_tmux_option "$verbose_option" "$verbose_default")"
35+
[ "$verbose_value" == "on" ]
36+
}
37+
38+
# Check if the telegram alert all option is enabled
39+
telegram_all_enabled() {
40+
local alert_all="$(get_tmux_option "$tmux_notify_telegram_all" "$tmux_notify_telegram_all_default")"
41+
[ "$alert_all" == "on" ]
42+
}
43+
44+
# Check if telegram bot id and chat id are set
45+
telegram_available() {
46+
local telegram_id="$(get_tmux_option "$tmux_notify_telegram_bot_id" "$tmux_notify_telegram_bot_id_default")"
47+
local telegram_chat_id="$(get_tmux_option "$tmux_notify_telegram_channel_id" "$tmux_notify_telegram_channel_id_default")"
48+
[ -n "$telegram_id" ] && [ -n "$telegram_chat_id" ]
49+
}
50+
51+
# Send telegram message
52+
# Usage: send_telegram_message <bot_id> <chat_id> <message>
53+
send_telegram_message() {
54+
curl "https://api.telegram.org/bot$1/sendMessage?chat_id=$2&text=$3" &> /dev/null
55+
}
56+
57+
# Send notification
58+
# Usage: notify <message> <send_telegram>
59+
notify() {
60+
# Switch notification method based on OS
61+
if [[ "$OSTYPE" =~ ^darwin ]]; then # If macOS
62+
osascript -e 'display notification "'"$1"'" with title "tmux-notify"'
63+
else
64+
# notify-send does not always work due to changing dbus params
65+
# see https://superuser.com/questions/1118878/using-notify-send-in-a-tmux-session-shows-error-no-notification#1118896
66+
notify-send "$1"
67+
fi
68+
69+
# Send telegram message if telegram variables are set, and telegram alert all is
70+
# enabled or if the $2 argument is set to true
71+
if telegram_available && (telegram_all_enabled || [ "$2" == "true" ]); then
72+
telegram_bot_id="$(get_tmux_option "$tmux_notify_telegram_bot_id" "$tmux_notify_telegram_bot_id_default")"
73+
telegram_chat_id="$(get_tmux_option "$tmux_notify_telegram_channel_id" "$tmux_notify_telegram_channel_id_default")"
74+
send_telegram_message $telegram_bot_id $telegram_chat_id "$1" &> /dev/null
75+
fi
76+
77+
# trigger visual bell
78+
# your terminal emulator can be setup to set URGENT bit on visual bell
79+
# for eg, Xresources -> URxvt.urgentOnBell: true
80+
tmux split-window "echo -e \"\a\" && exit"
81+
}

scripts/notify.sh

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
# Usage: notify <refocus> <telegram>
23
## -- Start monitoring script
34

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

1112
## Functions
1213

13-
# Send notification
14-
notify() {
15-
# Switch notification method based on OS
16-
if [[ "$OSTYPE" =~ ^darwin ]]; then # If macOS
17-
osascript -e 'display notification "'"$1"'" with title "tmux-notify"'
18-
else
19-
# notify-send does not always work due to changing dbus params
20-
# see https://superuser.com/questions/1118878/using-notify-send-in-a-tmux-session-shows-error-no-notification#1118896
21-
notify-send "$1"
22-
fi
23-
24-
# trigger visual bell
25-
# your terminal emulator can be setup to set URGENT bit on visual bell
26-
# for eg, Xresources -> URxvt.urgentOnBell: true
27-
tmux split-window "echo -e \"\a\" && exit"
28-
}
29-
3014
# Handle cancelation of monitor job
3115
on_cancel()
3216
{
@@ -42,12 +26,6 @@ on_cancel()
4226
}
4327
trap 'on_cancel' TERM
4428

45-
# Check if verbose option is enabled
46-
verbose_enabled() {
47-
local verbose_value="$(get_tmux_option "$verbose_option" "$verbose_default")"
48-
[ "$verbose_value" != "on" ]
49-
}
50-
5129
## Main script
5230

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

6240
# Construct tnotify finish message
63-
if verbose_enabled; then # If @tnotify-verbose is disabled
64-
complete_message="Tmux pane task completed!"
65-
else # If @tnotify-verbose is enabled
41+
if verbose_enabled; then # If @tnotify-verbose is enabled
6642
verbose_msg_value="$(get_tmux_option "$verbose_msg_option" "$verbose_msg_default")"
6743
complete_message=$(tmux display-message -p "$verbose_msg_value")
44+
else # If @tnotify-verbose is disabled
45+
complete_message="Tmux pane task completed!"
6846
fi
6947

7048
# Create bash suffix list
@@ -86,12 +64,12 @@ if [[ ! -f "$PID_FILE_PATH" ]]; then # If pane not yet monitored
8664
# if so, break and notify
8765
if echo "$output" | tail -n2 | grep -e $prompt_suffixes &> /dev/null; then
8866
# tmux display-message "$@"
89-
if [[ "$1" == "refocus" ]]; then
67+
if [[ "$1" == "true" ]]; then
9068
tmux switch -t \$"$SESSION_ID"
9169
tmux select-window -t @"$WINDOW_ID"
9270
tmux select-pane -t %"$PANE_ID"
9371
fi
94-
notify "$complete_message"
72+
notify "$complete_message" $2
9573
break
9674
fi
9775

scripts/variables.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ export verbose_msg_default="(#S, #I:#P) Tmux pane task completed!"
2727
# Monitor checker interval
2828
export monitor_sleep_duration="@tnotify-sleep-duration"
2929
export monitor_sleep_duration_default=10
30+
31+
# Telegram notification settings
32+
export tmux_notify_telegram_bot_id="@tnotify-telegram-bot-id"
33+
export tmux_notify_telegram_channel_id="@tnotify-telegram-channel-id"
34+
export tmux_notify_telegram_all="@tnotify-telegram-all"
35+
export tmux_notify_telegram_bot_id_default=""
36+
export tmux_notify_telegram_channel_id_default=""
37+
export tmux_notify_telegram_all_default="off"

tnotify.tmux

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ fi
2323
tmux unbind-key m
2424
tmux unbind-key M
2525
tmux unbind-key M-m
26+
tmux unbind-key C-m
27+
tmux unbind-key C-M-m
28+
2629
tmux bind-key m run-shell -b "$CURRENT_DIR/scripts/notify.sh"
2730
tmux bind-key M run-shell -b "$CURRENT_DIR/scripts/cancel.sh"
28-
tmux bind-key M-m run-shell -b "$CURRENT_DIR/scripts/notify.sh refocus"
31+
tmux bind-key M-m run-shell -b "$CURRENT_DIR/scripts/notify.sh true"
32+
tmux bind-key C-m run-shell -b "$CURRENT_DIR/scripts/notify.sh false true"
33+
tmux bind-key C-M-m run-shell -b "$CURRENT_DIR/scripts/notify.sh true true"

0 commit comments

Comments
 (0)