visit
To send a message to Telegram group or channel, you should first create your own bot. Just open Telegram, find @BotFather and type
/start
. Then follow instructions to create bot and get token to access the HTTP API.Create a new Channel in Telegram and add your bot as a member. So your bot could send messages to the Channel.
In order to get Channel Id, first, post any message to the Channel. Then use this link template to get Channel Id://api.telegram.org/bot<YourBOTToken>/getUpdates
Here is a response example: {
"ok":true,
"result": [
{
"update_id":123,
"channel_post": {
"message_id":48,
"chat": {
"id":-123123123, // this is your channel id
"title":"Notifications",
"type":"channel"
},
"date":1574485277,
"text":"test"
}
}
]
}
curl '//api.telegram.org/bot<YourBOTToken>/sendMessage?chat_id=<channel_id>&text=<text>'
But in programming, it is good practice to hide the low-level implementation. So we will create a Linux terminal command
telegram-send
and could send messages with this simple command. Lets create file
telegram-send.sh
touch telegram-send.sh
#!/bin/bash
GROUP_ID=<group_id>
BOT_TOKEN=<bot_token>
# this 3 checks (if) are not necessary but should be convenient
if [ "$1" == "-h" ]; then
echo "Usage: `basename $0` \"text message\""
exit 0
fi
if [ -z "$1" ]
then
echo "Add message text as second arguments"
exit 0
fi
if [ "$#" -ne 1 ]; then
echo "You can pass only one argument. For string with spaces put it on quotes"
exit 0
fi
curl -s --data "text=$1" --data "chat_id=$GROUP_ID" '//api.telegram.org/bot'$BOT_TOKEN'/sendMessage' > /dev/null
chmod +x telegram-send.sh
./telegram-send.sh "Test message"
In order to use this script from everywhere and type
telegram-send
instead ./telegram-send.sh
add it to /usr/bin/ foldersudo mv telegram-send.sh /usr/bin/telegram-send
sudo chown root:root /usr/bin/telegram-send
telegram-send "Test message"
touch login-notify.sh
#!/bin/bash
# prepare any message you want
login_ip="$(echo $SSH_CONNECTION | cut -d " " -f 1)"
login_date="$(date +"%e %b %Y, %a %r")"
login_name="$(whoami)"
# For new line I use $'\n' here
message="New login to server"$'\n'"$login_name"$'\n'"$login_ip"$'\n'"$login_date"
#send it to telegram
telegram-send "$message"
sudo mv login-notify.sh /etc/profile.d/login-notify.sh
Previously published at