commit d27f3b3935e096b9abac9ec790971d1d34d43078
parent 4c17f9ea380f523d24f46ae50423f8840642c99e
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date: Thu, 19 Aug 2021 23:27:01 +0200
Add Telegram Notifier
Diffstat:
4 files changed, 114 insertions(+), 0 deletions(-)
diff --git a/telegram-notifier/Makefile b/telegram-notifier/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/telegram-notifier/README.md b/telegram-notifier/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/telegram-notifier.service b/telegram-notifier/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/telegram-notifier.sh b/telegram-notifier/telegram-notifier.sh
@@ -0,0 +1,49 @@
+#!/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
+}
+
+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')
+ fi
+ fi
+
+ sleep $POLLTIME
+
+done
+
+# EOF