Do you ever miss important notifications while working on your desktop? Have you ever wished you could receive Linux desktop notifications on your phone or from anywhere in the world?
In this guide, we’ll set up a bash script that monitors your Linux desktop notifications using D-Bus and forwards them to Telegram for remote monitoring.
This tutorial uses the D-Bus notification system, which is the standard notification system used by most Linux desktop environments including GNOME, KDE, XFCE, and i3.
Prerequisites
Before we begin, make sure you have the following installed on your Linux system:
sudo apt install jq curl
sudo pacman -S jq curl
sudo dnf install jq curl
Project Repository: Linux Notifications to Telegram
Step 1: Setup Telegram Bot API
First, you need to create a Telegram bot and get your chat ID.
📖 Create Your Telegram Bot
- Open Telegram and search for BotFather
- Send
/newbotto create a new bot - Follow the prompts to name your bot (e.g., “Desktop Notifications”)
- BotFather will give you an API Token - copy this
- Search for your new bot and click Start to activate it
📖 Get Your Chat ID
- Search for userinfobot on Telegram
- Start a conversation with it
- It will reply with your Chat ID - copy this
Step 2: Install the Script
Clone the repository to your local machine:
mkdir -p ~/.local/bin
cd ~/.local/bin
git clone --depth 1 https://gitlab.com/krafi/linux-notifications-to-telegram.git
cd linux-notifications-to-telegram
Step 3: Configure the Script
Open the run.sh file in a text editor:
nano run.sh
Edit these two variables with your Telegram credentials:
TELEGRAM_API_KEY="YOUR_TELEGRAM_API_KEY"
TELEGRAM_CHAT_ID="YOUR_TELEGRAM_CHAT_ID"
Replace YOUR_TELEGRAM_API_KEY and YOUR_TELEGRAM_CHAT_ID with the values you obtained from BotFather and userinfobot.
Make the scripts executable:
chmod +x run.sh
chmod +x i3_autostart.sh
Step 4: Test the Setup
Run the script to test it:
./run.sh
Now, trigger a notification on your desktop (e.g., send a file, get a system alert, or use the notify-send command):
notify-send "Test Notification" "This is a test from your Linux desktop!"
You should receive this notification in your Telegram chat within seconds.
Press Ctrl+C to stop the script when you’re done testing.
Step 5: Configure Autostart
To run the script automatically when you log in, you have several options:
# For i3 window manager
# Add this line to your ~/.config/i3/config:
exec --no-startup-id ~/.local/bin/linux-notifications-to-telegram/run.sh
# Or if using the autostart script:
exec --no-startup-id ~/.local/bin/linux-notifications-to-telegram/i3_autostart.sh
# Create a user systemd service
mkdir -p ~/.config/systemd/user
nano ~/.config/systemd/user/telegram-notifications.service
[Unit]
Description=Forward Linux desktop notifications to Telegram
[Service]
Type=simple
ExecStart=%h/.local/bin/linux-notifications-to-telegram/run.sh
Restart=on-failure
[Install]
WantedBy=default.target
# Enable and start the service
systemctl --user enable telegram-notifications.service
systemctl --user start telegram-notifications.service
# Create a desktop autostart file
nano ~/.config/autostart/telegram-notifications.desktop
[Desktop Entry]
Type=Application
Name=Telegram Notifications
Exec=/home/yourusername/.local/bin/linux-notifications-to-telegram/run.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Adjust the path /home/yourusername/ to match your home directory.
How It Works
The script uses dbus-monitor to listen for notifications on the D-Bus system message bus. When a notification is sent to your desktop, the script captures it and forwards it to Telegram.
#!/bin/bash
# Configuration
TELEGRAM_API_KEY="YOUR_TELEGRAM_API_KEY"
TELEGRAM_CHAT_ID="YOUR_TELEGRAM_CHAT_ID"
# Function to send message to Telegram
send_to_telegram() {
local message="$1"
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_API_KEY/sendMessage" \
-d chat_id="$TELEGRAM_CHAT_ID" \
-d text="$message" \
-d parse_mode="HTML"
}
# Monitor D-Bus for notifications
dbus-monitor "interface='org.freedesktop.Notifications'" | \
while read -r line; do
if echo "$line" | grep -q "method call"; then
# Read the notification body
read -r line
read -r line
read -r line
read -r line
# Extract notification title and body
title=$(echo "$line" | sed 's/.*string "//;s/"$//')
read -r line
body=$(echo "$line" | sed 's/.*string "//;s/"$//')
# Send to Telegram if we have content
if [ -n "$title" ] || [ -n "$body" ]; then
message="<b>$title</b>%0A$body"
send_to_telegram "$message"
fi
fi
done
📖 Understanding the D-Bus Parsing
The script reads lines from dbus-monitor output. The notification data typically appears:
- Line 1: Notification title
- Line 2: Notification body
We use sed to extract the string values between the quotes.
Troubleshooting
📖 Not receiving notifications
-
Check if dbus-monitor is working:
dbus-monitor "interface='org.freedesktop.Notifications'"Trigger a notification and see if anything appears.
-
Verify your credentials:
echo $TELEGRAM_API_KEY echo $TELEGRAM_CHAT_ID -
Test your bot directly:
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_API_KEY/sendMessage" \ -d chat_id="$TELEGRAM_CHAT_ID" \ -d text="Test message"
📖 Script exits immediately
Some desktop environments may close the script if D-Bus isn’t available. Try running it in the background:
nohup ./run.sh > /dev/null 2>&1 &
📖 Notifications delayed
The script processes notifications in real-time. If you experience delays, check your network connection to Telegram’s servers.
With this setup, you’ll never miss important desktop notifications, even when you’re away from your computer! The notifications will be forwarded to your Telegram account instantly.
Discussion
0 commentsJoin the Discussion
Sign in to post comments and join the conversation.
No comments yet. Be the first to share your thoughts!