Product used: VPS 200 G10s iv
from 3.25€ / month | go to product >
In this tutorial you will learn how to set up a Telegram bot using the Telegram API. This will allow you to receive a message on Telegram after each login attempt on your server.
I am doing this on a Debian installation, but this process should also work on other Linux distributions.
You need a running vServer or Root Server from netcup with superuser access.
First, install Telegram on your smartphone and open the app.
Then search for the user "botfather" in the contacts. We create a new bot by sending "botfather" the following message:
/newbot
"botfather" will ask for the name of the bot. You can choose any name you want, but note that the name must be unique. Next, choose a username. This must end with the suffix "_bot".
For example, I used these names:
Superduperbot for the bot Superduperbot_bot for the username
"botfather" will now create the bot and generate a token. We will need this later, so write down the token.
Here is an example of how the token looks:
123456090:ABCDEFGHIJK-ABCDEFGHIJK
Now, search for the newly created bot in your Telegram contacts. Next, start the bot by clicking on start or sending the message:
/start
Next, we point our Browser to the address shown below. Replace "TOKEN" with the token you got from "botfather" in the previous step:
https://api.telegram.org/bot"TOKEN"/getUpdates
Write down the row of numbers coming after "id". This is our "Telegram_id" and will be needed in the next step.
Head over to your netcup server and create a script under "/etc/profile.d" named something like "telegram_message_on_login.sh" by running the command shown below. Note that any script in "/etc/profile.d" will be executed during each login to your server.
nano /etc/profile.d/telegram_message_on_login.sh
Copy the following into the terminal window. Please replace the values under TOKEN and ID with your specific data:
`#!/bin/bash
TOKEN="123456090:ABCDEFGHIJK-ABCDEFGHIJK"
ID="123456789"
HOSTNAME=$(hostname -f)
DATE="$(date +"%d.%b.%Y--%H:%M")"
MESSAGE=" $USER logged in at $DATE on $HOSTNAME !"
URL="https://api.telegram.org/bot$TOKEN/sendMessage"
curl -s -X POST $URL -d chat_id=$ID -d text="$MESSAGE" >/dev/null`
Last, make the script executable by running this command:
chmod +x /etc/profile.d/telegram_message_on_login.sh
From now on you will receive a message via Telegram everytime someone logs into your server.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
By making a contribution to this project, I certify that:
The contribution was created in whole or in part by me and I have the right to submit it under the license indicated in the file; or
The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same license (unless I am permitted to submit under a different license), as indicated in the file; or
The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.
I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the license(s) involved.