How to Get Linux Desktop Notifications on Telegram

A bash script that forwards your Linux desktop notifications to Telegram using the D-Bus notification system and Telegram Bot API.

Rafi
Written by Rafi
📅
Published March 18, 2023
⏱️
Read Time 1 min
📊
Difficulty Intermediate

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.

ℹ️ Info

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:

Press on a tab to see code
sudo apt install jq curl
sudo pacman -S jq curl
sudo dnf install jq curl
💡 Tip

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
  1. Open Telegram and search for BotFather
  2. Send /newbot to create a new bot
  3. Follow the prompts to name your bot (e.g., “Desktop Notifications”)
  4. BotFather will give you an API Token - copy this
  5. Search for your new bot and click Start to activate it
📖 Get Your Chat ID
  1. Search for userinfobot on Telegram
  2. Start a conversation with it
  3. 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"
⚠️ Warning

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.

ℹ️ Info

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:

Press on a tab to see code
# 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
ℹ️ Info

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.

Press on a tab to see code
#!/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
  1. Check if dbus-monitor is working:

    dbus-monitor "interface='org.freedesktop.Notifications'"
    

    Trigger a notification and see if anything appears.

  2. Verify your credentials:

    echo $TELEGRAM_API_KEY
    echo $TELEGRAM_CHAT_ID
    
  3. 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.

💡 Tip

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.

Knowledge Check

Test your knowledge about Linux desktop notifications to Telegram

Discussion

0 comments
Reading Progress
4 min left 0%
Welcome back! Sign in to join the discussion.

Please verify your email to sign in.

Enter the 6-digit code from your verification email.

Didn't receive the email?

Remember your password?

Create an account to comment and join the community.
Letters, numbers, and underscores only

Check your email! We've sent a verification code.

Enter the 6-digit code to complete your registration, or click the link in your email.

Didn't receive the email?

Wrong email?

Enter your email address and we'll send you a code to reset your password.

Remember your password?

Enter the 6-digit code from your email and create a new password.

Didn't receive code?

Welcome aboard!

Your account has been created successfully.

Welcome back! Sign in to join the discussion.

Please verify your email to sign in.

Enter the 6-digit code from your verification email.

Didn't receive the email?

Remember your password?

Create an account to comment and join the community.
Letters, numbers, and underscores only

Check your email! We've sent a verification code.

Enter the 6-digit code to complete your registration, or click the link in your email.

Didn't receive the email?

Wrong email?

Enter your email address and we'll send you a code to reset your password.

Remember your password?

Enter the 6-digit code from your email and create a new password.

Didn't receive code?

Welcome aboard!

Your account has been created successfully.