Encyclopedia of Distance
work and education
rus

Telegram-bot deploy with pictures – in 2 minutes and for free

This simple instruction will allow any of you to launch (deploy) your own Telegram-bot on server – quickly, without specific programs and skills, using your browser only!

No need for any tricky settings, no need for huge sets of instructions. Your bot will be working on server in 2 minutes! Let’s go!

Server for Telegram-bot

We will upload the Telegram-bot to the server of Beget hosting service provider (one of the largest hosting service provider).

Go to this page, here you will find the server you need: https://beget.com/en/vps

Fret not, you don’t need to pay anything right away – at the first the server for Telegram-bot will work without payment.

Select the cheapest plan here. Believe me, if you need this instruction, then you don’t have a reason to buy an expensive server yet. Take the cheapest one of all:

Server for Telegram-bot. Select the cheapest plan here.

Fill in your login information – full name, phone number, email. For the time being, almost all countries require you to confirm your phone number when registering:

Server for Telegram-bot - Fill in your login information

Select the cheapest plan again and click “Create a virtual server”.

Server for Telegram-bot - Select the cheapest plan

Server for Telegram-bot - press Create

Congratulations! Your server is running and ready for work (your server name will be likely different, but that’s also name:)):

Server for Telegram-bot - started

Go to the file manager on the control panel:

Server for Telegram-bot - file manager

Here are the server folders. Do not be scared – these are just folders of the Ubuntu operating system. This is about the same as the folders on your C drive on your computer. 

Create a folder with the name of your bot right here. Let it be “giganika”. (When you are creating bots in a professional way, read up which directory is the best to place them. There are different opinions, but we do this for training purposes, and we will create a bot folder right in the root of operating system – it’s safe, but just not very correct):

Server for Telegram-bot - create a folder

Server for Telegram-bot - foder name

Double-click on our folder and go to it:

Server for Telegram-bot - open

Upload our bot file here:

Server for Telegram-bot - upload files

If you have more than one file, there are folders, zip them, upload the archive to the server, and then unzip it:

Server for Telegram-bot - extract archive

We will host a simple echo-bot that sends a message back. If you don’t have your own yet, you can take our code (create a main.py file and place the code into it), if you have it, host yours:

[python] import telebot bot = telebot.TeleBot(`YOUR_BOT_TOKEN`) @bot.message_handler(content_types=["text"]) def repeat_all_messages(message): bot.send_message(message.chat.id, message.text) if __name__ == ‘__main__’: bot.infinity_polling()[/python]

After the bot file is uploaded, we need to start launching it. Our bot works on the basis of pyTelegramBotAPI library (“telebot” in the bot code). To make bot work, we need to install this library on the server. You should install the one on which your bot works. 

To work with the server, we need to install a simple program for working via SSH. This is PuTTy, and you can download it from the official website: https://www.putty.org/ 

Download and install the program, then open it. You will see the following window:

Server for Telegram-bot - PuTTy

Now copy the IP-address of your server from the control panel, from here:

Server for Telegram-bot - your ip

Paste the copied IP into PuTTy and click “Open”:

Server for Telegram-bot - open your server

In the black window opened, in the “login as” line, enter “root” and press “Enter”:

Server for Telegram-bot - as root

Insert your server’s password into the “password” line – it should have been sent to your email (If the password has not been received, click “Settings”-“Reset password”-“Reset password and reboot server” on the control panel, the new password will be sent to your email).

Important! Insertion in PuTTy program is carried out either with a single-click of the RIGHT mouse button, or with the SHIFT+Insert key combination.

Server for Telegram-bot - your pass

Once you have done everything accurately, you should see roughly the same window:

Server for Telegram-bot - window foir download

Our bot uses the “pyTelegramBotAPI” library. If your bot uses the different one, install yours. We install our library “pyTelegramBotAPI” using pip. We write in the terminal:

[code]pip install pyTelegramBotAPI[/code]

Server for Telegram-bot - pip install

After the library is installed, we will be left to go to the folder with our bot via terminal and launch it. We write “cd /name_of_folder_with_bot” in the terminal. In our case this is cd /giganika:

Server for Telegram-bot - change folder

As we can see, the terminal goes to the folder with bot:

Server for Telegram-bot - yes

Now we launch the bot here using the nohup command and & symbol at the end of the line (this command allows the bot to work in the background):

[code]nohup python3 main.py &[/code]

main.py is the name of the code file of our bot. If your file has another name, replace this name with yours.

Finished! Our bot is working!

Server for Telegram-bot - bot is working

Now that our bot is working, let’s find out an another couple of points. Firstly:

How to stop your Telegram-bot

To stop a bot launched using nohup, the easiest way in our case is to use the killall command:

[code]killall python3[/code]

Server for Telegram-bot - killall command

Note that this will stop all processes running by python3. You don’t have any other processes since the server was created just now, and the bot is the first thing we launched on it. But bear it in mind in the future.

If the Telegram-bot stops

Sometimes it happens that the bot stops. In 99,9% of cases, the problem is that Telegram doesn’t respond to bot’s requests at times, and the bot errors out. That the bot not to stop, you need to handle errors in your bot script properly. This is a topic for another article. It more concerns the development process rather than deployment one. Perhaps one day we will write it. And while you can google information on the topic of proper error handling in a Telegram-bot. The essence of proper handling is that the script doesn’t stop when most errors occur, but tries to repeat the operation again and records the error information into the logs. This is not difficult to do, but it is necessary.

Encyclopedia of distance work and education.
Absolutely everything you need to know
to learn and work remotely.