Skip to content

Commit

Permalink
matrix notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
EnigmaCurry committed Jun 21, 2024
1 parent df010a1 commit 1c7ec0f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
7 changes: 4 additions & 3 deletions bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ modules=(
bugs
path
terminal
prompt-basic
prompt
bash_command_timer
keychain
completion
prompt-basic
prompt
theme
alias
editor
git
rust
video
matrix
bash_command_timer
local
unset_funcs
)
Expand Down
61 changes: 59 additions & 2 deletions config/bash/bash_command_timer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ BCT_WRAP=0

# Only print time when its greater than BCT_THRESHOLD seconds:
# Set to 0 to always print it.
BCT_THRESHOLD=2
BCT_LOG_THRESHOLD=2

# Perform this action when commands complete and that take longer than BCT_ACTION_THRESHOLD seconds.
BCT_ACTION_MSG='Command completed on ${HOSTNAME} :: ${output_str}'
BCT_ACTION_COMMAND='echo -n ""'
BCT_ACTION_COMMAND='matrix-alert "Command completed on ${HOSTNAME} :: ${output_str}"'
BCT_ACTION_THRESHOLD=-1

# IMPLEMENTATION
# ==============
Expand Down Expand Up @@ -168,7 +174,8 @@ function BCTPostCommand() {
local num_mins=$(($command_time % $HOUR / $MIN))
local num_secs=$(($command_time % $MIN / $SEC))
local num_msecs=$(($command_time % $SEC / $MSEC))
local threshold=$(($BCT_THRESHOLD * $SEC))
local threshold=$(($BCT_LOG_THRESHOLD * $SEC))
local action_threshold=$(($BCT_ACTION_THRESHOLD * $SEC))
local time_str=""
if [ $num_days -gt 0 ]; then
time_str="${time_str}${num_days}d "
Expand Down Expand Up @@ -214,6 +221,12 @@ function BCTPostCommand() {
# Finally, print output.
echo -e "${output_str_colored}"
fi

if (( ${action_threshold} >= 0 )) && (( "$command_time" > ${action_threshold} )); then
export output_str
local cmd="$(echo "${BCT_ACTION_COMMAND}" | envsubst '$HOSTNAME$output_str')"
(eval "${cmd}" >/dev/null 2>&1)
fi
}


Expand Down Expand Up @@ -250,3 +263,47 @@ else
fi
unset -f BCTRegisterCallbacksWithBashPreexec
unset -f BCTRegisterCallbacksDirectly

notify-completion() {
ARG="$1"; shift
if [[ -z "$ARG" ]]; then
echo "### Send Matrix notifications after command completion:"
echo "## Help:"
echo "## notify-completion on ## Always notify on completion"
echo "## notify-completion 300 ## Notify for commands that take longer than 300 seconds"
echo "## notify-completion off ## Turn off all notifications"
echo
case "${BCT_ACTION_THRESHOLD}" in
-1) echo "Bash command timer matrix notifications are DISABLED.";;
*) echo "Bash command timer matrix notifications are ENABLED."
echo "# BCT_ACTION_THRESHOLD=${BCT_ACTION_THRESHOLD}"
echo "Notifiction threshold (seconds): ${BCT_ACTION_THRESHOLD}";;
esac
return
elif [[ "$ARG" == "off" ]]; then
BCT_ACTION_THRESHOLD=-1
echo "Bash command timer matrix notifications disabled."
echo "To enable notifications, provide the TIME argument: notify-completion 300"
return
elif [[ -z "${MATRIX_ALERT_WEBHOOK}" ]]; then
echo "You need to configure MATRIX_ALERT_WEBHOOK (in ~/.bashrc.local)" >/dev/stderr
echo "## # MATRIX_ALERT_WEBHOOK=https://matrix.example.com/hookshot/...."
return 1
elif [[ "${BCT_ACTION_COMMAND}" == 'echo -n ""' ]]; then
echo "You need to configure BCT_ACTION_COMMAND (in ~/.bashrc.local)" >/dev/stderr
echo "## BCT_ACTION_COMMAND='matrix-alert "Command completed on \${HOSTNAME} :: \${output_str}"'"
return 1
elif [[ "$ARG" == "on" ]]; then
BCT_ACTION_THRESHOLD=0
else
if ! [[ "${ARG}" =~ ^[0-9]+$ ]] ; then
echo "Error: TIME is not a number." >/dev/stderr
return
else
BCT_ACTION_THRESHOLD="${ARG}"
fi
fi
echo "# BCT_ACTION_THRESHOLD=${BCT_ACTION_THRESHOLD}"
echo "Bash command timer matrix notifications are ENABLED."
echo "Notifiction threshold (seconds): ${BCT_ACTION_THRESHOLD}"
}
18 changes: 18 additions & 0 deletions config/bash/matrix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Send notifications to Matrix:

# Configure your Matrix server:
## https://matrix-org.github.io/matrix-hookshot/latest/hookshot.html
## https://github.com/spantaleev/matrix-docker-ansible-deploy/blob/master/docs/configuring-playbook-bridge-hookshot.md

matrix-alert() {
if [[ -n "${MATRIX_ALERT_WEBHOOK}" ]]; then
(set -x; curl -X POST "${MATRIX_ALERT_WEBHOOK}" --json '{"text":"'"$*"'"}' >/dev/null 2>&1)
else
echo "Missing MATRIX_ALERT_WEBHOOK env var." >/dev/stderr
return 1
fi
}

## Put this in your ~/.bashrc.local :
## MATRIX_ALERT_WEBHOOK=https://matrix.enigmacurry.com/hookshot/webhooks/XXXXXXX
## BCT_ACTION_COMMAND='matrix-send "Command completed on ${HOSTNAME} :: ${output_str}"'

0 comments on commit 1c7ec0f

Please sign in to comment.