From 00184ac2f66d7912c094d6a41f68529da304f44e Mon Sep 17 00:00:00 2001 From: rickstaa Date: Sat, 3 Jun 2023 10:07:22 +0200 Subject: [PATCH 1/9] feat: add telegram notification feature This commit gives users the ability to enable telegram notifications through the `tnotify-telegram-bot-id` and `tnotify-telegram-channel-id` tmux configuration variables. --- README.md | 4 ++++ scripts/helpers.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ scripts/notify.sh | 23 ----------------------- scripts/variables.sh | 6 ++++++ 4 files changed, 52 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 52b6c53..b4d5c9c 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,10 @@ The tmux notify script uses your shell prompt suffix to check whether a command 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. +### Enable telegram channel notifications + +To enable telegram channel notifications, put `set -g @tnotify-telegram-bot-id 'your telegram bot id'` and `set -g @tnotify-telegram-channel-id 'your channel name'` in the `.tmux.conf` config file. 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. + ## How does it work The pretty naive approach. Checks if pane content ends with the bash prompt suffixes mentioned above every 10 seconds. diff --git a/scripts/helpers.sh b/scripts/helpers.sh index ffe3d57..c506a7c 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -25,3 +25,45 @@ set_tmux_option() { escape_glob_chars() { echo "$1" | sed 's/[.[\*^$()+?{|]/\\&/g' } + +# 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 + + # Send telegram message if telegram bot id and chat id are set + if telegram_enabled; 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" + 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" +} + +# Check if verbose option is enabled +verbose_enabled() { + local verbose_value="$(get_tmux_option "$verbose_option" "$verbose_default")" + [ "$verbose_value" != "on" ] +} + +# Check if telegram bot id and chat id are set +telegram_enabled() { + 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 +send_telegram_message(){ + curl "https://api.telegram.org/bot$1/sendMessage?chat_id=$2&text=$3" +} diff --git a/scripts/notify.sh b/scripts/notify.sh index 56a4ec1..c1a4c74 100755 --- a/scripts/notify.sh +++ b/scripts/notify.sh @@ -10,23 +10,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() { @@ -42,12 +25,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 diff --git a/scripts/variables.sh b/scripts/variables.sh index 23d9770..cc7cbeb 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -27,3 +27,9 @@ 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 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_bot_id_default="" +export tmux_notify_telegram_channel_id_default="" From 0303fb37d420bb4b8ddfc5f6044b9a25d566cc22 Mon Sep 17 00:00:00 2001 From: rickstaa Date: Sat, 3 Jun 2023 10:07:22 +0200 Subject: [PATCH 2/9] feat: add telegram notification feature This commit gives users the ability to enable telegram notifications through the `tnotify-telegram-bot-id` and `tnotify-telegram-channel-id` tmux configuration variables. --- README.md | 4 ++++ scripts/helpers.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ scripts/notify.sh | 23 ----------------------- scripts/variables.sh | 6 ++++++ 4 files changed, 52 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 52b6c53..f7c7fdb 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,10 @@ The tmux notify script uses your shell prompt suffix to check whether a command 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. +### Enable telegram channel notifications + +To enable telegram channel notifications, 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. 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. + ## How does it work The pretty naive approach. Checks if pane content ends with the bash prompt suffixes mentioned above every 10 seconds. diff --git a/scripts/helpers.sh b/scripts/helpers.sh index ffe3d57..a02fd5d 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -25,3 +25,45 @@ set_tmux_option() { escape_glob_chars() { echo "$1" | sed 's/[.[\*^$()+?{|]/\\&/g' } + +# 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 + + # Send telegram message if telegram bot id and chat id are set + if telegram_enabled; 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" + 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" +} + +# Check if verbose option is enabled +verbose_enabled() { + local verbose_value="$(get_tmux_option "$verbose_option" "$verbose_default")" + [ "$verbose_value" != "on" ] +} + +# Check if telegram bot id and chat id are set +telegram_enabled() { + 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 +send_telegram_message(){ + curl "https://api.telegram.org/bot$1/sendMessage?chat_id=$2&text=$3" &> /dev/null +} diff --git a/scripts/notify.sh b/scripts/notify.sh index 56a4ec1..c1a4c74 100755 --- a/scripts/notify.sh +++ b/scripts/notify.sh @@ -10,23 +10,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() { @@ -42,12 +25,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 diff --git a/scripts/variables.sh b/scripts/variables.sh index 23d9770..cc7cbeb 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -27,3 +27,9 @@ 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 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_bot_id_default="" +export tmux_notify_telegram_channel_id_default="" From 1bc3c7cb8c7d26035b97b25e3b4f02bff335f048 Mon Sep 17 00:00:00 2001 From: rickstaa Date: Sat, 3 Jun 2023 10:34:00 +0200 Subject: [PATCH 3/9] refactor: improve docstrings --- scripts/helpers.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index a02fd5d..d50693f 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -3,6 +3,7 @@ # Additional functions that are used in the main scripts. # Get tmux option +# Usage: get_tmux_option