[go: up one dir, main page]

0% found this document useful (0 votes)
153 views11 pages

Get Server Notification On Mobile PDF

This document describes how to set up a Telegram notification bot to send server alerts and notifications to a Telegram account. It explains how to create a bot using the Telegram BotFather, obtain the bot token and chat ID, and then send messages from the Linux command line or a bash script using curl requests to the Telegram API. This allows configuring the bot to notify about events like SSH logins by adding code to the server's profile script to execute on login and send a notification message to Telegram.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views11 pages

Get Server Notification On Mobile PDF

This document describes how to set up a Telegram notification bot to send server alerts and notifications to a Telegram account. It explains how to create a bot using the Telegram BotFather, obtain the bot token and chat ID, and then send messages from the Linux command line or a bash script using curl requests to the Telegram API. This allows configuring the bot to notify about events like SSH logins by adding code to the server's profile script to execute on login and send a notification message to Telegram.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Get Server Notification on Mobile

Monitoring the server is crucial to have uninterrupted services. There are different ways
to configure alerts from the server. Here is an interesting way to get the notification
from the server right on your Telegram Messenger application.

In these notes describes how to create a personal notification bot that can send messages
from the Linux command-line through the Telegram API.

Telegram is a relatively new messaging app that touts security and privacy as its main
features. It has clients for a huge raft of platforms, and best of all it has a very nice bot
API for developers.

Creating Telegram Bot - By using ‘botfather’ in the Telegram, we can create a new bot.
Open Web Browser

https://web.telegram.org/#/login

Select Your Country And Type Your Mobile Number


Click to Next Button, and Enter OTP- Next

Go to message field and type ‘/start’.


Type ‘/newbot’ without quotes in the message field and hit enter

Now bot father asks you to name the new bot. Give any name of your choice. In this
example, it is named as ‘Ashutosh’.
Bot requires a unique username. Type a username of your choice. If it is already taken,
try a different one.

Once a username is successfully generated, you’ll get a congratulatory message from


botfather with the token id of the bot. This token ID is used in the Telegram API to
send messages.
Now update your token id
To get the chat ID, use the url https://api.telegram.org/bot$token/getupdates (Replace
$token with actual token ID obtained during bot creation) in the browser which returns
with string of values.

Next we need to get the chat ID. Search for the newly created bot in the search field and
start a chat session.
Go to message field and type ‘/start’.

Again Refresh your browser, and update your token id

In the screenshot, the highlighted section has the chat id.


To test if the bot is working, run the URL
https://api.legram.org/bot$token/sendMessage?chat_id=$chaitid&text=Hello+World
in the browser. (Replace $token and $chatid with corresponding ids).

The message ‘Hello World’ will be received in the Telegram.


Note- You can use android, iPhone, iPad, WP, macOS, and PC/MAC/Linux devices for
receive telegram message.

https://telegram.org

Linux Command Line


Send a message from the Linux command-line interface, using ‘curl’

# curl -s --max-time 10 -d "chat_id=<Chat_ID>&disable_web_page_preview=1&text=Hi


How Are You? " https://api.telegram.org/bot<TOKEN>/sendMessage

Note- Replace $token and $chatid with corresponding ids and text_Message.
Now Message Successfully Received.
Bash Script
Here is a bash script that can monitor server alert when any user ssh login is successful.
It has been tested on Centos 6,7. The telegram API is used to send attachment instead of
just text message. Resource usage statistics can be sent in attachment instead of plain
message.

/etc/profile is executed at every login (for bash shell users). The if statement will only
return true if the user has logged in via ssh.

# vim /etc/profile -> Paste script end of the file ‘/etc/profile’

MESS="$(head /logg)"
MESSAGE="$(echo)"
CHATID="**********"
TOKEN="*********************************************************"

if [ -n "$SSH_CLIENT" ]; then

TEXT="$(date): ssh login to ${USER}@$(hostname )"


TEXT="$TEXT from $(echo $SSH_CLIENT|awk '{print $1}')"
echo $TEXT > /log

curl -s --max-time 10 -d
"chat_id=$CHATID&disable_web_page_preview=1&text=$MESSAGE$MESS"
https://api.telegram.org/bot$TOKEN/sendMessage >/dev/null

fi

:wq (Save & Quit)

Note- Update Your Chat_id and Token.


Now Check…Login Server @SSH

# ssh <Server IP Address or Name>

Enter User Name:


Password:

Telegram successfully send message…

Ashutosh

You might also like