How to Automatically Send a Telegram Message from the Linux Terminal Using curl

If you manage a Linux server, you rely on cron jobs and bash scripts to handle automated tasks like database backups, system updates, or heavy video rendering. When these long-running scripts finish—or worse, when they crash—you need to know immediately. Checking log files manually is inefficient; a true server administrator wants a push notification directly to their smartphone.

While you could configure a complex SMTP server to send email alerts, emails are often delayed or trapped in spam filters. A much faster, more reliable, and completely free alternative is to use the Telegram Messenger API. With a few simple steps, you can configure your Linux terminal to send instantaneous push notifications to your phone using a basic curl command.

Step 1: Create a Telegram Bot

To send messages programmatically, you need a dedicated bot account.

  1. Open the Telegram app on your phone or desktop.
  2. Search for @BotFather (the official Telegram bot manager) and start a chat.
  3. Type the command /newbot and follow the prompts to give your bot a name and a unique username.
  4. When finished, BotFather will give you an HTTP API Token (a long string of numbers and letters, e.g., 123456789:ABCdefGHIjklMNOpqrSTUvwxYZ). Copy this token to a safe place; it is the password to your bot.

Step 2: Get Your Personal Chat ID

Your bot needs to know exactly who to send the message to. You need your unique Telegram Chat ID.

  1. In the Telegram search bar, look for your newly created bot (using the username you just made) and send it a simple message like “Hello”.
  2. Now, open a web browser and paste the following URL, replacing <YOUR_BOT_TOKEN> with the token from Step 1:
    https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
  3. Press Enter. You will see a raw JSON output. Look for the section labeled "chat":{"id": followed by a 9 or 10-digit number. Copy that number; this is your Chat ID.

Step 3: Send a Message Using curl

Now that you have your Bot Token and your Chat ID, you can send a message directly from your Linux terminal using the curl command (which comes pre-installed on almost all distributions).

  1. Open your Linux terminal.
  2. Type the following command, replacing the placeholders with your actual Token and Chat ID:
    curl -s -X POST https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendMessage -d chat_id=<YOUR_CHAT_ID> -d text="Backup completed successfully!"
  3. Press Enter.

Within half a second, your phone will buzz with a new Telegram message from your bot stating “Backup completed successfully!”

Step 4: Integrate into a Bash Script

To make this truly useful, you can embed this one-liner at the very end of any automated script. For example, if you have a nightly backup script (backup.sh), simply append the curl command to the bottom.

#!/bin/bash
# ... (Your complex backup commands here) ...

# Send a notification when finished
curl -s -X POST https://api.telegram.org/bot123456789:ABCdefGHIjklMNOpqrSTUvwxYZ/sendMessage -d chat_id=987654321 -d text="Server 01: Nightly MySQL backup is complete." > /dev/null

(Note: Adding > /dev/null at the end prevents the raw JSON response from cluttering your terminal output when the script runs).

RELATED POSTS

  • How to Use the df Command to Instantly Check Total Free Hard Drive Space in Linux
  • How to Use the Linux lsattr Command to View File Attributes
  • How to Sort Data in Linux Using the sort Command
  • How to Delete a Directory in the Linux Terminal
  • How to Use the diff Command to Compare Two Text Files Line by Line in Linux
  • Get the best tech tips delivered straight to your inbox.

    Join thousands of readers mastering Apple, Google, Microsoft, and Linux.