DEV Community

Cover image for Send message to Telegram on any SSH login
Konstantin Bogomolov
Konstantin Bogomolov

Posted on • Originally published at bogomolov.tech

Send message to Telegram on any SSH login

In this article, you will walk through creation a simple shell script to send messages to Telegram messenger. 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 /start. Then follow instructions to create bot and get token to access the HTTP API.

Create Channel

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:

https://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" } } ] } 
Enter fullscreen mode Exit fullscreen mode

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>' 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Then add script to this file. Set your group id and token in script.

#!/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" 'https://api.telegram.org/bot'$BOT_TOKEN'/sendMessage' > /dev/null 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Now you can test it

./telegram-send.sh "Test message" 
Enter fullscreen mode Exit fullscreen mode

In order to use this script from everywhere and type telegram-send instead ./telegram-send.sh add it to /usr/bin/ folder

sudo mv telegram-send.sh /usr/bin/telegram-send 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Now you can test it

telegram-send "Test message" 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Add this code to script

#!/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" 
Enter fullscreen mode Exit fullscreen mode

Then move this script to /etc/profile.d/ folder

sudo mv login-notify.sh /etc/profile.d/login-notify.sh 
Enter fullscreen mode Exit fullscreen mode

Now re-login to your web server and check it works.

Top comments (4)

Collapse
 
mrrcollins profile image
Ryan Collins

I love using Telegram for notifications! That's one of my favorite uses for Telegram. I started, as you did, with writing a script to send notifications. And then I found someone had written telegram-notify. Nicolas has written up the directions setting up the script.

I like it because it has added more features, such as easily sending Emojis and files with the script.

Collapse
 
s9k96 profile image
Shubham Saxena

Hey, great post. Can you tell me how did you put these styled code snippets here?

Collapse
 
bogkonstantin profile image
Konstantin Bogomolov • Edited

Thanks. It is simple, just put code between back ticks with a language name, like this:

 ```javascript ...your code here... ``` 
Collapse
 
s9k96 profile image
Shubham Saxena

Awesome, thanks