Why GCP?
When writing telegrams to bots, I came across the question of how to quickly and free make the bot work constantly. Options with Heroku and Pythonanywhere have too small limits if you have more than one bot. So I decided to use GCP. The platform provides free $ 300 per year + huge discounts on the use of these funds (up to 94%).How to host your bot?
Step 1. Register for GCP
Go to the GCP website and click Get started for free .Enter your data and card. Money from the card will not be debited if you yourself do not activate a paid subscription.Step 2. Create a virtual machine
After registration, you will be on the main page of the service. You need to select the Compute Engine tab in the Resources section.You must create a new instance.If you will not deploy the database in a virtual machine, then you can take g1-small, otherwise I advise n1-standart.You will also need to choose an OS. I chose Debian GNU / Linux 9 (stretch).All VM created. Conventional deployment takes 1 to 5 minutes.Step 3. Configure the virtual machine
You can connect via SSH from your PC or interact through the platform.To do this, click on SSH.Your Linux terminal will open in a new window.Now let's move on to setting up. First, enter the command:sudo apt-get update
to update information on the latest package versions.After we enter:sudo apt-get install python3-setuptools
sudo apt-get install python3-pip
Python itself does not need to be installed, it already exists.Now you need to install all the necessary libraries. There is a small nuance, all libraries must be installed twice:pip3 install ‘name_of_package’
for use via python3 command, andsudo pip3 install ‘name_of_package’
for systemd. This utility will help you launch the bot and restart it if it crashes.The easiest way to run the bot through python3, but it will turn off if you disconnect. You can use screen, but the bot will not restart itself. You can also use crontab with port checking, but it seems to me that this option is more complicated than systemd.Step 4. Fill the bot to the server
There are two ways to fill your bot. If you are not friends with Git, you can simply archive the bot in .tar and upload it to the server:After that, unzip it with the command:tar -xvf yourfile.tar
Now your bot is stored in a folder with the name of the archive.The second way is through Git. I think it’s not worth explaining how to do this to people who know how to use it.After installing it with the command:sudo apt install git
You can clone it on your VM.After that, let's move on to setting up systemd. To do this, go to his directory:cd /etc/systemd/system
And create the bot.service file:sudo nano bot.service
Enter the following in the window that opens:
[Unit]
Description=Telegram bot ' '
After=syslog.target
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/home/ /
ExecStart=/usr/bin/python3 /home/ / /bot.py
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
Close and save the file. Closes with the command Ctrl + X.After that, enter the commands in turn:sudo systemctl daemon-reload
sudo systemctl enable bot
sudo systemctl start bot
sudo systemctl status bot
If all goes well, you will see something like the following:That's it, now your bot works independently. I hope my article will help you host your bot.PS
1.Check your bot for errors
Launch your bot and test its operation before launching it through systemd. To do this, go to the folder with the bot and run through python.cd < >
python3 bot.py
2. Add encoding to the files with py scripts
Insert at the beginning of the file.3.Errors in systemd
If you checked the bot for errors and it worked fine, but it does not want to start in systemd, then you can look at the logs and understand what the error is by looking at the file:sudo nano /var/log/syslog
You can download this file and view it on your computer through Notepad ++.4.Bot update
If you want to add or upload a new version of the bot, enter the command:sudo systemctl stop bot
Provide all necessary manipulations. And then enter the following commands so that it works again:sudo systemctl daemon-reload
sudo systemctl start bot
sudo systemctl status bot