/~ Terminal Tips

various terminal tips


TABLE OF CONTENTS


Tilde Notifications

This article describes how to send notifications from your Tilde.town to your mobile or desktop.

This can be used for various purposes, like when you have new mail, when mentioned in IRC while away. The choice where you use it is up to you.

First we will cover how to set this up, then I will list a couple of cases where it can be implemented.

NOTE #1: We will be using the ntfy.sh service. The code running ntfy.sh is open source and you can self-host if privacy is an issue for you. For this article we'll use the free plan hosted by ntfy.sh

 

NOTE #2: You do not need a ntfy.sh account to use these services. We will be using it anonymously.

Setup

Because our notify script will contain our secret token, we must set its permissions so that only we can read it.

Your token can be any string, but you'll want something that can't readily be guessed, because ntfy.sh uses a pub-sub pattern and anybody can subscribe to a channel the token is known or guessed.

1) Generate your unique token

echo "insert unintelligible rambling here" | sha1sum

2) Create the script, it's contents runs a curl command silently, redirecting output into the void, as ~/.bin/send-notify.sh

#!/usr/bin/env sh
curl --silent -d "$*" ntfy.sh/YOUR-TOKEN-HERE > /dev/null

make it executable and readable only to you

chmod 700 ~/.bin/send-notify.sh

3) Open the ntfy.sh web app and/or install the mobile client for your device. In either case, subscribe to a topic using your generated token.

Finally let's test it!

~/.bin/send-notify.sh "hello from your shell"

Mail notify

We can now send a notification when we have new mail via a cron job:

0 * * * * mail -e && ~/.bin/send-notify.sh "you have mail"

But we can do better! Why send ourselves a repeating notify every hour? Surely we only need to send a notify once for each new message.

To achieve this let's create script ~/.bin/checkmail.sh

#!/usr/bin/env bash
# Send a notification on new mail.
# - get the count of mail in our inbox
# - test for special case of "no messages"
# - test if it differs from the last known count
# - write the latest count to file for next time

latest=$(from -c)
cache="$HOME/.cache/checkmail.txt"
empty="There are 0 messages in your incoming mailbox."
lastknown=$empty

if [[ -f "$cache" ]]; then
    # read the last known stored in file
    lastknown=$(cat $cache)
fi

if [[ "$latest" == "$empty" ]]; then
    #echo "mailbox is empty"
    exit 0
elif [[ "$latest" == "$lastknown" ]]; then
    #echo "no new mail"
    exit 0
else
    ~/.bin/send-notify.sh "you have new mail"
fi

# write the latest to file
echo "$latest" > $cache

Finally make your script executable and update the cron job:

0 * * * * ~/.bin/checkmail.sh

Weechat notify

The Weechat script away_action runs a command if you are mentioned while your away status is set. We can call our notify script using this method.

In Weechat, input /script to open the scripts panel (you may need to enable scripts the first time you run this, Weechat will instruct you how to proceed.)

Scroll down the list with the arrow keys until you reach "away_action", press i to install it.

Finally configure the plugin's command that will run when you are mentioned:

/set plugins.var.python.away_action.command /exec ~/.bin/send-notify.sh

Weechat passes the nick who mentioned you as an argument.


Please mail me if you find bugs on this page :) wesbat@tilde.town