Free Telegram Bot Hosting on Google Cloud Platform

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%).

Free Telegram Bot Hosting on Google Cloud Platform

How to host your bot?

Step 1: Register for GCP

Go to website GCP and push Get started for free.
Free Telegram Bot Hosting on Google Cloud Platform
Enter your details and card. Money from the card will not be debited if you do not activate the paid subscription yourself.

Free Telegram Bot Hosting on Google Cloud Platform

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.
Free Telegram Bot Hosting on Google Cloud Platform
You need to create a new instance.

Free Telegram Bot Hosting on Google Cloud Platform
If you are not going to deploy the DB on virtual machine, then you can take g1-small, otherwise I recommend n1-standart.

Free Telegram Bot Hosting on Google Cloud Platform
You will also need to select an OS. I chose Debian GNU/Linux 9 (stretch).

Free Telegram Bot Hosting on Google Cloud Platform
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.

Free Telegram Bot Hosting on Google Cloud Platform
This will open a Linux terminal in a new window.

Free Telegram Bot Hosting on Google Cloud Platform
Now let's move on to the setup. First enter the command:

sudo apt-get update

to update information about 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 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:

Free Telegram Bot Hosting on Google Cloud Platform
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 it to people who know how to use it.

After installing it with the command:

sudo apt install git

You 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/system

And create a 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. 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:

Free Telegram Bot Hosting on Google Cloud Platform
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.py

2. 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/syslog

You 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 bot

See 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 bot

Source: habr.com

Add a comment