Back to Home

Telegram bot polling linux server

bot · telegram bots · linux · python

Telegram bot polling linux server

Recently I have been fond of Python. I wanted to write something more substantial than codes like helloworld. Since he also looked with interest towards the telegram bots, the idea was born to create a bot that would run commands or scripts on a remote server (linux) and return the result to a telegram. What for? Conveniently! No need to log in to the server to get information about the load on the processor, free memory or disk space. You can even run scripts.

And so we study python and api telegram bot , register our bot in telegrams, download ready-made scripts, run them on our server and change config.py for ourselves.

Let's go in order:

1) Registration of the telegram bot. We find the father of all bots - @BotFather. We write to him:

/newbot

In response to his message, enter the name of your new bot. It must have the word bot at the end.

moi_novii_bot

If the name is not taken and it is entered correctly, then you will get a token - you will need to copy it to the config.py file of the script:

token = ' '

2) Download the script. This is the first test version of the script - in order to assess the capabilities of the first python itself, and secondly - the telegram bot. Any suggestions and suggestions are welcome - I will finish. You can add something yourself - the request will also be shared. What are the features of the program (you can see them by typing in your bot / help):

  • view network settings (runs the ifconfig command on the server)
  • get information about disk space (runs the df -h command on the server)
  • get memory information (runs the free -m command on the server)
  • get information about the load on the processor (runs the mpstat command on the server)
  • get information about the size of the folder specified in config.py (runs the du -sh folder name command on the server)
  • checks the presence and size of the file in the folder (runs the ls -lh file command on the server). I make a backup of base 1c in the mounted network folder with the file name by date - therefore, such a need arose.

What else do I plan to implement: launching any script (possibly without outputting the entire execution, but only the final result), improve the output of the result - more readable, collecting statistics in the database and displaying graphs of loads, etc.

To run the script, you need python 3 and python-telegram-bot. I have centOS. There are already 2 versions of python. We put next to it 3 python and a library for the bot:

wget http://www.python.org/ftp/python/3.3.2/Python-3.6.0.tar.xz
yum install xz
tar -xpJf Python-3.6.0.tar.xz
cd Python-3.6.0
yum groupinstall "Development tools"
./configure
make
make install
ln -s /usr/local/bin/python3 /usr/bin/python3
pip3 install python-telegram-bot --upgrade

The composition of the script:

bot - the bash script file that starts python3 bot.py
bot.py - the bot script itself. For those who are familiar with python, welcome inside.
config.py - stores settings. Enter the token received in the telegram there. Then run the script.

In the telegram application, enter:

/id

This way you get your personal id. It must be entered in the line (instead of 123456789) admin = ['123456789']. This is done for security reasons, so that other users can enter only users from certain telegram accounts. It is possible to register several id separated by commas: admin = ['123456789', '987654321'].

In the dir1 line, we specify the path to the folder whose volume we would like to control (for me this is the path to the folder with the pgsql databases)

In the dir_backup line - the path to the folder where the file lies, the volume (or presence) of which must be controlled. I have a file of the form 20170218.tar.gz. By default, it is the option with the file name of the month_date.tar.gz that is checked. If you want to change the mask of the file being checked, then you need to find and edit the line in the bot.py file

filebackup = config.dir_backup + cur_year + cur_month + cur_day + '.tar.gz'  #формируем имя файла для поиска

Keep in mind that you will not need to restart the script when fixing the config.py file. All settings are re-read by the script every time again.

It would be nice to add this script to startup. For CentOS 7:

touch /etc/systemd/system/telegram-bot.service
chmod 664 /etc/systemd/system/telegram-bot.service

The contents of this file are:

[Unit]
Description=Telegram bot
After=network.target
[Service]
Type=simple
User= от имени кого запускать
ExecStart=путь к файлу bot.sh (в этом файле тогда надо прописать полный путь до bot.py)
[Install]
WantedBy=multi-user.target

We start a new service:

systemctl start telegram-bot.service

Add it to startup:

systemctl enable telegram-bot.service

Check the status:

systemctl status telegram-bot.service

We can enter commands. We start with / help.

Link to the archive with the script

Thank you, riot26 for laying out on githab:
gist.github.com/riot26/bb55e8a19fae0b58d687040c54cbc148

Read Next