From d671725397cfa7ba986b7d64379d0b48c15c1361 Mon Sep 17 00:00:00 2001 From: Bruno Carlin Date: Sat, 4 Jul 2020 00:06:54 +0200 Subject: [PATCH] [matterhorn] add notifications --- matterhorn/.config/matterhorn/config.ini | 2 +- matterhorn/.config/matterhorn/notify.sh | 69 ++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100755 matterhorn/.config/matterhorn/notify.sh diff --git a/matterhorn/.config/matterhorn/config.ini b/matterhorn/.config/matterhorn/config.ini index e209e43..60d840e8 100644 --- a/matterhorn/.config/matterhorn/config.ini +++ b/matterhorn/.config/matterhorn/config.ini @@ -150,7 +150,7 @@ tokencmd: secret-tool lookup matterhorn token # an example Linux notification script. See docs/notification-scripts.md # for details on the notification script API. # -# activityNotifyCommand = /path/to/notify +activityNotifyCommand = ~/.config/matterhorn/notify.sh # Background activity display: Matterhorn communicates with the # Mattermost server using asynchronous background thread processing. diff --git a/matterhorn/.config/matterhorn/notify.sh b/matterhorn/.config/matterhorn/notify.sh new file mode 100755 index 0000000..0914816 --- /dev/null +++ b/matterhorn/.config/matterhorn/notify.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash + +# Positional parameters passed to this script by Matterhorn: +mentioned="${1?}" +sender="${2?}" +message="${3?}" + +# Script options + +# notify_URGENCIES +# +# The first word is the urgency for items where you are not mentioned. +# The second word is the urgency for items where you are mentioned. +# Use "none" to not be notified; otherwise use "low", "normal", or +# "critical". +notify_URGENCIES="normal critical" + +# The desktop notification category +notify_CATEGORY="im.received" + +# Notification header +notify_HEAD="Message from $sender" + +# Notification APP +notify_APPNAME="Matterhorn" + +# Escape backslashes in the message +message="${message//\\/\\\\}" + +# Escape double quotes in the message +message="${message//\"/\\\"}" + +# Notification body +notify_BODY="$message" + +getUrgencyHelper() { + if [ "$mentioned" == "1" ] + then + echo "$1" + else + if [ "$mentioned" == "2" ] + then + echo "$2" + else + echo "Error: mentioned value '$mentioned' unexpected" > /dev/stderr + exit 1 + fi + fi +} + +getUrgency() { + # We are using arguments as a poor man's bash array for portability + # shellcheck disable=SC2086 + getUrgencyHelper $notify_URGENCIES +} + +urgency=$(getUrgency) + +if [ -n "$urgency" ] +then + test "$urgency" = "none" || + notify-send --urgency "$urgency" \ + --category "$notify_CATEGORY" \ + --app-name "$notify_APPNAME" \ + -- "$notify_HEAD" "$notify_BODY" + + mpv "/usr/share/sounds/freedesktop/stereo/message.oga" >/dev/null 2>&1 & + mpv "/usr/share/sounds/freedesktop/stereo/message-new-instant.oga" >/dev/null 2>&1 & +fi