In part four of the series on writing a telegram bot in python, let’s run it on the server. For variety and verisimilitude, I chose to host it on a VPS rather than Heroku. First of all, Heroku is very rarely used in production. Its paid rates are much higher than the cost of renting a server. Secondly, large companies give virtual machines for free for a year. This is enough not to pay for the server for 4 years.
Table of Contents
Getting a VPS
As I have already written, it is possible to get VPS for free for a year. Choose any of them:
- AWS Amazon, Amazon EC2 product.
- Azure Microsoft, Linux Virtual Machines product.
- Google Cloud, Compute Engine product.
- Alibaba Cloud, Elastic Compute Service product.
Just don’t activate all at once, it’s a one-time offer. I will not describe in detail how to deploy a VPS, these platforms have high level documentation. If you have difficulties with English and translators, start with Azure. They have a lot of Russian documentation. Let me just say that it is highly desirable to choose Ubuntu 18.04 OS. The process of getting a free period and creating a virtual machine is quite thorny. If you have never done it before, be prepared to spend 1-2 hours to get acquainted with cloud solutions. I will use a VPS with hourly rate from reg.ru. It’s a cheap and easy solution. For training and demonstration purposes, you can run it for a few hours at a price of 0.32 ₽/hr. And the permanent operation of such a bot will cost 215 rubles per month.
Connecting to a virtual machine
To connect to a VPS you need to know ip (IPv4), login (usually “root”) and password. With Linux and MacOS you can connect from the terminal. Enter the command, login and ip server. ssh [email protected]
For windows you can download Ubuntu terminal. If this option is not suitable, use PuTTY (port: 22). This is what the console looks like. It requires a “yes” and a password to connect. After logging in I found out what version of python is installed with the
python -V
command. It is 3.6.9 out of the box and the project is on 3.8.5, I need to upgrade.
Preparing the server
- Install the required version of python. Carefully enter these commands one by one, this is a custom installation process.
$ sudo apt update
2. Create a project. Set up and create a virtual environment. Execute the commands one at a time:
$ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
$ wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz
$ tar -xf Python-3.8.5.tgz
$ cd Python-3.8.5
$ ./configure --enable-optimizations
$ make # ~15 minutes
$ sudo make altinstall
$ cd /home A bit of detail. I downloaded the archive, unzipped and installed python 3.8.5. Will be ready to wait for the make
command to run. Type python3.8 -V
and make sure you can proceed: /home# python3.8 -V
Python 3.8.5 $ python3.8 -m pip install --upgrade pip
Get
$ pip install virtualenv
$ mkdir fonlinebot
$ cd fonlinebot
$ virtualenv venv
$ source venv/bin/activate
$ python -V
$ deactivate We installed pip and virtualenv. Then we created the folder "fonlinebot" and created a virtual environment in it and tested it. 3. Let's install and run the Redis-server. To install and test in Ubuntu, enter these commands:
$ sudo apt install redis-server
$ redis-cli
127.0.0.1:6379> pingPONG
, that means redis is running. I will not set the remote access and password in this guide. This service is only available to a specific machine, which completely covers the task. 4. Environment variables. Now we need to hide the token and the API key in variables. Run the command nano /etc/environment
and paste these lines with your values in quotes. fonlinebot_token="replace_to_token"
Then press CTRL+O -> Enter -> CTRL+X to save. Restart the machine:
fonlinebot_api_key="replace_for_api_key"
sudo reboot
to change the settings. 5. Preparing the bot code. Edit the file config.py. Let’s uncomment the logging settings and set environment variables.
Hiding secret information (tokens, passwords) is mandatory. Do not post files with passwords to github, stackoverflow, or send them to third-party developers. Half of the work is done. Now we need to upload the files to the server.
# fonlinebot/config.py
#...
import datetime
import os
formatter = '[%(asctime)s] %(levelname)8s --- %(message)s (%(filename)s:%(lineno)s)'
logging.basicConfig(
filename=f'bot-from-{datetime.datetime.now().date()}.log',
filemode='w',
format=formatter,
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.WARNING
)
TOKEN = os.environ.get("fonlinebot_token")
#...
'x-rapidapi-key': os.environ.get("fonlinebot_api_key"),
#...
Uploading files to VPS
Download and install WinSCP. This is a program for uploading projects to VPS. An alternative option is Filezilla. An option to improve your skills is Git. Open and establish a connection to the server: Next, move the project files (without venv and Pycharm files) to the home/fonlinebot/ folder.
Done? Let’s run the bot from the server.
$ cd /home/fonlinebot/ # go to the project folder
$ source venv/bin/activate # activate the environment
$ pip install -r requirements.txt # install dependencies
# python main.py # run the bot
Now go to Telegram and test it works. Does it respond? Ok, stop it (ctrl+c) and deactivate the virtual environment(deactivate
) Finish direct project. After closing the terminal, the bot will stop. After restarting the server, it will not start. Let’s set up autonomous operation.
Continuous operation of the bot
Create your own service for continuous operation of the bot and restart in case of failure. nano /lib/systemd/system/fonlinebot.service
With the settings: [Unit]
Then press CTRL+O -> Enter -> CTRL+X to save. What are these settings? Service settings, these are the ones we care about:
Description=Football online bot
After=network.target
[Service]
EnvironmentFile=/etc/environment
ExecStart=/home/fonlinebot/venv/bin/python main.py
ExecReload=/home/fonlinebot/venv/bin/python main.py
WorkingDirectory=/home/fonlinebot/
KillMode=process
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
Description
– The service description.EnvironmentFile
is the path to the file with the variables.ExecStart
andExecReload
are commands to start and restart the bot.WorkingDirectory
– the path to the folder where the main.py launcher file is located.
To start the service, run these 2 commands. systemctl enable fonlinebot
Now let’s go back to the bot and see how it responds. If the bot doesn’t respond, check the status and logs. It’s hard to anticipate an error here:
systemctl start fonlinebotsystemctl status fonlinebot # status
The project is ready, great job! All stages of development on Gitlab.
journalctl -u fonlinebot.service # logs
Conclusion
We did a lot of work: we created the bot, configured its interaction with the external api and uploaded it to the servers. In the process we touched buttons, menus, callbacks and bugs. Here are a few ideas for continuing the project:
- more detailed match statistics,
- admin area for receiving errors and sending messages to all users,
- collecting statistics on user activity.
- switch to webhook.
Good luck!