If you’re a Linux user, you may be familiar with bash shell scripts. These scripts are a powerful tool for automating tasks and processes. In this article, we’ll show you how to use bash shell scripts to send messages or notifications to Telegram on Linux.
Step 1: Create a Telegram Bot
The first step is to create a Telegram bot by following these steps:
- Open Telegram and search for the BotFather account.
- Start a chat with BotFather and type /newbot to create a new bot.
- Follow the prompts and enter a name and username for your bot.
- BotFather will provide you with a unique API token for your bot. Save this token, as you’ll need it later to send messages to your bot.
Step 2: Install the Required Libraries
To interact with the Telegram API, we’ll need to install the “curl” library on our Linux system. You can do this by running the following command:
sudo apt-get install curl
Step 3: Create the Bash Shell Script
Now it’s time to create the bash shell script that will send messages or notifications to your Telegram bot. Open a text editor and create a new file called “send_telegram.sh”. Then copy and paste the following code into the file:
#!/bin/bash TOKEN="your_bot_token" CHAT_ID="your_chat_id" MESSAGE="Hello, world!" curl -s -X POST https://api.telegram.org/bot$TOKEN/sendMessage -d chat_id=$CHAT_ID -d text="$MESSAGE" > /dev/null
Replace “your_bot_token” with the API token provided by BotFather, and “your_chat_id” with the ID of the chat where you want to receive the message. You can find your chat ID by sending a message to your bot and then using the following command in your web browser:
Look for the “chat” object in the response and copy the “id” value. Finally, customize the message variable to include the message you want to send.
Step 4: Test the Script
Save the file and make it executable by running the following command:
chmod +x send_telegram.shThen run the script by typing the following command:
./send_telegram.shIf everything is set up correctly, you should receive a message from your bot in your Telegram app.
Step 5: Automate Notifications
Now that you have a working script, you can automate notifications by running the script as a cron job or using other automation tools like Ansible. Simply set up the script to run at specific intervals or trigger it when certain events occur.
Using bash shell scripts to send messages or notifications to Telegram on Linux can save you time and effort. By following these simple steps, you can set up your own notification system to stay informed and up-to-date with the latest information. From setting up a Telegram bot to testing and automating notifications, this guide covers everything you need to get started with sending Telegram messages using bash shell scripts in Linux.