Recently fond of Python. I wanted to write something more significant than codes like helloworld. Since the telegram looked with interest towards the bots, an idea was born to create a bot that would run commands or scripts on a remote server (linux) and return the result to telegrams. What for? Conveniently! No need to log in to a 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 a telegram, 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. 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 receive a token - you will need to copy it into the config.py file of the script:
token = ' '
2) Download the script. This is the first test version of the script - in order to evaluate the capabilities of the python itself first, and secondly the telegram bot. Any wishes and suggestions are welcome - finish. You can add something yourself - please share too. What are the features of the program (you can see them typing / help in your bot):
- view network settings (runs the ifconfig command on the server)
- get disk space information (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 command du -sh folder name on the server)
- checks the presence and size of the file in the folder (runs the ls -lh file on the server). I make a backup of the base 1c in the mounted network folder with the file name by date - so there is such a need.
What else 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 outputting graphs for loads, etc.
Python 3 and python-telegram-bot are required to run the script. I have a centOS. There already is version 2 of the python. Put alongside 3 python and a library for bot operation:
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
Script composition:
bot - bash-script file that launches python3 bot.py
bot.py - the bot script itself. For those who are familiar with python - welcome inside.
config.py - stores settings. There you enter the token received in the telegram. Then run the script.
In the application telegram enter:
/id
So you get your personal id. It must be entered in the string (instead of 123456789) admin = ['123456789']. This is done for security purposes, so that other users can only enter users from certain telegram accounts. You can enter several id through comma: admin = ['123456789', '987654321'].
In the line dir1 we write the path to the folder the volume of which we would like to control (I have this path to the folder with the pgsql databases)
In the line dir_backup - the path to the folder where the file is located, the volume (or the presence) of which must be monitored. I have a file of the form 20170218.tar.gz. By default, it is the version with the name of the year.mon.gd file 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 later when you repair the config.py file, you will not need to restart the script. All settings are re-read by the script each time anew.
It would be nice to add this script to autoload. For CentOS 7:
touch /etc/systemd/system/telegram-bot.service chmod 664 /etc/systemd/system/telegram-bot.service
Content of this file:
[Unit] Description=Telegram bot After=network.target [Service] Type=simple User= ExecStart= bot.sh ( bot.py) [Install] WantedBy=multi-user.target
We are starting a new service:
systemctl start telegram-bot.service
Add it to autoload:
systemctl enable telegram-bot.service
Checking status:
systemctl status telegram-bot.service
We can enter commands. We start with / help.
→
Link to the archive with the scriptThank you,
riot26 for the display on githab:
gist.github.com/riot26/bb55e8a19fae0b58d687040c54cbc148