Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 01-en.md for telegram_message_after_login #162

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions community-tutorials/telegram_message_after_login/01-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ author_name: Michael Luckas
author_url: https://github.com/extremmichi
author_image:
author_bio:
tags: [shell, ssh, linux,message, login]
tags: [shell, ssh, linux, message, login]
netcup_product_url: https://www.netcup.eu/bestellen/produkt.php?produkt=2991
language: en
available_languages: [en]
---
# Introduction

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.
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 on your server.

I am doing this on a Debian installation, but this process should also work on other Linux distributions.

Expand All @@ -27,48 +27,50 @@ You need a running vServer or Root Server from netcup with superuser access.
## Step 1 - Installing Telegram
First, install Telegram on your smartphone and open the app.

## Step 2 Creating the bot
## Step 2 - Creating the bot
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:
For example, I used this name / username combination:

`Superduperbot for the bot
Superduperbot_bot for the username`
`Superduperbot` for the bot name
`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`

## Step 3 Configuring the bot
## Step 3 - Configuring the bot

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:
Also send at least one normal message to the bot, e.g. `foobar` to get an ID in the next step.

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.
Write down the row of numbers coming after "id". This is our Telegram "ID" and will be needed in the next step.


## Step 4 Creating the notification script
## Step 4 - Creating the notification script

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
```
#!/bin/bash

TOKEN="123456090:ABCDEFGHIJK-ABCDEFGHIJK"
ID="123456789"
Expand All @@ -77,7 +79,8 @@ Copy the following into the terminal window. Please replace the values under TOK
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`
curl -s -X POST $URL -d chat_id=$ID -d text="$MESSAGE" >/dev/null
```

Last, make the script executable by running this command:

Expand Down