Why GCP?
When writing telegrams for bots, I ran into the question of how to quickly and free of charge make the bot work all the time. The Heroku and Pythonanywhere options are way too low if you have more than one bot. So I decided to use GCP. The platform provides $300 for a year for free + huge discounts when using these funds (up to 94%).

How to host your bot?
Step 1: Register for GCP
Go to website and push Get started for free.

Enter your details and card. Money from the card will not be debited if you do not activate the paid subscription yourself.

Step 2. Create a virtual machine
After registration, you will find yourself on the main page of the service. You need to select the Compute Engine tab in the Resources section.

You need to create a new instance.

If you are not going to deploy the DB on virtual machine, then you can take g1-small, otherwise I recommend n1-standart.

You will also need to select an OS. I chose Debian GNU/Linux 9 (stretch).

That's it, the VM is created. Its usual deployment takes from 1 to 5 minutes.
Step 3: Set up the virtual machine
You can connect via SSH from your PC or interact through the platform.
To do this, click on SSH.
![]()
This will open a Linux terminal in a new window.

Now let's move on to the setup. First enter the command:
sudo apt-get updateto update information about the latest package versions.
After we enter:
sudo apt-get install python3-setuptools
sudo apt-get install python3-pipPython itself does not need to be installed, it is already there.
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, and
sudo pip3 install ‘name_of_package’ for systemd. This utility will help you start the bot and restart it if it crashes.
The easiest way to run the bot is through python3, but it will shut down if you disconnect. You can use screen, but the bot will not restart itself. You can also use crontab with a port check, but I think this option is more complicated than systemd.
Step 4. Upload the bot to the server
There are two ways to upload your bot. If you are not Git friendly, you can simply zip the bot to .tar and upload it to the server:

After that, unzip it with the command:
tar -xvf yourfile.tarNow 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 it to people who know how to use it.
After installing it with the command:
sudo apt install gitYou can clone it to your VM.
After that, we move on to setting up systemd. To do this, go to its directory:
cd /etc/systemd/systemAnd create a bot.service file:
sudo nano bot.serviceEnter 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. Closed with 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 everything goes well, you will see something like this:

Everything, now your bot works independently. I hope my article will help you to host your bot.
P.S.
1.Check your bot for errors
Start your bot and test it out before running it through systemd. To do this, go to the folder with the bot and run through python.
cd <папка вашего бота>
python3 bot.py2. Add encoding to files with py scripts
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Insert at the beginning of the file.
3. Bugs in systemd
If you checked the bot for errors and it worked fine, but it does not want to run in systemd, then you can look at the logs and understand what the error is by viewing the file:
sudo nano /var/log/syslogYou can download this file and view it on your computer with Notepad++.
4.Bot update
If you want to add or upload a new version of the bot, then enter the command:
sudo systemctl stop botSee all the necessary manipulations. And then enter the following commands to make it work again:
sudo systemctl daemon-reload
sudo systemctl start bot
sudo systemctl status botSource: habr.com
