In this article, you will walk through the creation of a simple Bash shell script to send messages to Telegram messenger using the Curl command. Then you will use this script to send a notification on every ssh login into your server. Create telegram bot To send a message to Telegram group or channel, you should first create your own bot. Just open Telegram, find @BotFather and type . Then follow instructions to create bot and get token to access the HTTP API. /start Create Channel Create a new Channel in Telegram and . So your bot could send messages to the Channel. add your bot as a member In order to get Channel Id, first, post any message to the Channel. Then use this link template to get Channel Id: https://api.telegram.org/bot<YourBOTToken>/getUpdates Here is a response example: { : , : [ { : , : { : , : { : , // this is your channel id : , : }, : , : } } ] } "ok" true "result" "update_id" 123 "channel_post" "message_id" 48 "chat" "id" -123123123 "title" "Notifications" "type" "channel" "date" 1574485277 "text" "test" Script to send message In order to send a message we could use simple command: curl 'https://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 and could send messages with this simple command. telegram-send Lets create file telegram-send.sh touch telegram-send.sh Then add script to this file. Set your group id and token in script. GROUP_ID=<group_id> BOT_TOKEN=<bot_token> [ == ]; 0 [ -z ] 0 [ -ne 1 ]; 0 curl -s --data --data > /dev/null #!/bin/bash # this 3 checks (if) are not necessary but should be convenient if " " $1 "-h" then echo "Usage: `basename ` \"text message\"" $0 exit fi if " " $1 then echo "Add message text as second arguments" exit fi if " " $# then echo "You can pass only one argument. For string with spaces put it on quotes" exit fi "text= " $1 "chat_id= " $GROUP_ID 'https://api.telegram.org/bot' $BOT_TOKEN '/sendMessage' It is not a good practice to store your token in that place, but for now, it is ok. Also, you could limit actions your bot could do in the Channel only to send messages. To run this script we should add permission chmod +x telegram-send.sh Now you can test it ./telegram-send.sh "Test message" In order to use this script from everywhere and type instead add it to /usr/bin/ folder telegram-send ./telegram-send.sh sudo mv telegram-send.sh /usr/bin/telegram-send Owner of all files in /usr/bin is root user. So let's do the same with our script: sudo chown root:root /usr/bin/telegram-send Now you can test it telegram-send "Test message" Send notification on SSH login All files with .sh extension in /etc/profile.d/ folder will be executed whenever a bash login shell is entered or the desktop session loads. Let's add a new script to send the notification. touch login-notify.sh Add this code to script login_ip= login_date= login_name= message= $ $ $ telegram-send #!/bin/bash # prepare any message you want " " $(echo $SSH_CONNECTION | cut -d " " -f 1) " " $(date +"%e %b %Y, %a %r") " " $(whoami) # For new line I use $'\n' here "New login to server" '\n' " " $login_name '\n' " " $login_ip '\n' " " $login_date #send it to telegram " " $message Then move this script to /etc/profile.d/ folder sudo mv login-notify.sh /etc/profile.d/login-notify.sh Now re-login to your web server and check it works. Previously published at https://bogomolov.tech/Telegram-notification-on-SSH-login/