DEV Community

Cover image for SSH Login Alert Bot
Mr Vi
Mr Vi

Posted on • Originally published at github.com

SSH Login Alert Bot

How to use

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

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

Clone the repo

git clone https://github.com/vi-dev0/login-alert-bot.git
Enter fullscreen mode Exit fullscreen mode

Add token and chat ID

Add bot token and chat ID in telegram-send.sh

#!/bin/bash

GROUP_ID=<group_id>
BOT_TOKEN=<bot_token>
Enter fullscreen mode Exit fullscreen mode

Add permissions

chmod +x telegram-send.sh
chmod +x login-notify.sh
Enter fullscreen mode Exit fullscreen mode

Move to scripts folder

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

You can test it:

telegram-send "Test message"

Move login-notify.sh 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.

Contact

Mr Vi - @vi_dev0

Project Link: Login Alert Bot

Top comments (0)