pub / pp-notifier

Instant Messenger Notification daemon for the PinePhone
git clone https://src.jayvii.de/pub/pp-notifier.git
Home | Log | Files | Exports | Refs | README | RSS

commit 3e642a338a85bfc60059c584eda462dd1ef3c96e
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Sun, 22 Aug 2021 14:26:56 +0200

Move to new Repository

Diffstat:
AMakefile | 6++++++
AREADME.md | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Atelegram-notifier.service | 9+++++++++
Atelegram-notifier.sh | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 126 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,6 @@ +install: + mkdir -p ${HOME}/.local/bin + mkdir -p ${HOME}/.config/systemd/user + install -m 700 ./telegram-notifier.sh ${HOME}/.local/bin/ + install -m 755 ./telegram-notifier.service ${HOME}/.config/systemd/user/ + systemctl --user daemon-reload diff --git a/README.md b/README.md @@ -0,0 +1,50 @@ +# Telegram Notifier + +## What does it do? + +It checks your current telegram messages via +`[telegram-cli]`(https://github.com/vysheng/tg) and notifies you about +any potential messages. + +## Why? + +The Telegram Desktop client is somewhat ressource intensive, particularly +on the CPU, leading to high(er) power consumption. With this lightweight +notifier script, you are not required to have the desktop client running +in the background at all times. + +## Are there system requirements? + +Yes. + +Checking Telegram Messages: `[telegram-cli]`(https://github.com/vysheng/tg) +``` +# Debian +apt install telegram-cli +``` + +Set it up via commandline: +``` +telegram-cli +``` + +Sending Notifications: `gdbus` (likely installed already) + +## Use Telegram Notifier + +Make sure, you fullfill the above requirements and set up `telegram-cli` +with your account. + +Pull this source directory, and run the Makefile: +``` +git pull https://src.jayvii.de/Hobby/PinePhoneScripts +cd PinePhoneScripts/telegram-notifier +make install +``` + +This will place `telegram-notifier.sh` into `~/.local/bin/telegram-notifier.sh` +and places the systemd-service file under your user's directory. You can enable +& start telegram-notifier with: +``` +systemctl --user enable telegram-notifier --now +``` diff --git a/telegram-notifier.service b/telegram-notifier.service @@ -0,0 +1,9 @@ +[Unit] +Description=Telegram Notifier + +[Service] +ExecStart=%h/.local/bin/telegram-notifier.sh +Restart=always + +[Install] +WantedBy=default.target diff --git a/telegram-notifier.sh b/telegram-notifier.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +POLLTIME=30 +TIMEOUT=25 +REPLACEID=0 # initial value should be 0 + +function check_telegram { + telegram-cli --disable-colors \ + --exec dialog_list | \ + grep -E "[1-9][0-9]* unread" +} + +function kill_tgc { + sleep $1 + if [[ ! -z $(pidof telegram-cli) ]]; then + pkill telegram-cli + fi +} + +function notify_telegram { + gdbus call --session \ + --dest=org.freedesktop.Notifications \ + --object-path=/org/freedesktop/Notifications \ + --method=org.freedesktop.Notifications.Notify \ + "Telegram" $2 "telegram" "Telegram" "$1" \ + '[]' '{"category": <"im.received">, "desktop-entry": <"telegram">}' \ + 3000 + echo 1 > /sys/devices/platform/leds/leds/pinephone\:blue/brightness +} + +function unnotify_tg { + gdbus call --session \ + --dest=org.freedesktop.Notifications \ + --object-path=/org/freedesktop/Notifications \ + --method=org.freedesktop.Notifications.CloseNotification \ + $1 + echo 0 > /sys/devices/platform/leds/leds/pinephone\:blue/brightness +} + +while :; do + + if [[ -z $(pidof telegram-desktop) ]]; then + + # kill after some time + kill_tgc $TIMEOUT & + + UNREADMSG=$(check_telegram) + + if [[ ! -z "$UNREADMSG" ]]; then + REPLACEID=$(notify_telegram "$UNREADMSG" "$REPLACEID" | sed -e 's/^.*\s//' -e 's/[^0-9]//g') + else + unnotify_tg $REPLACEID + REPLACEID=0 + fi + fi + + sleep $POLLTIME + +done + +# EOF