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!
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:
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:
Select the cheapest plan again and click “Create a virtual server”.
Congratulations! Your server is running and ready for work (your server name will be likely different, but that’s also name:)):
Go to the file manager on the control panel:
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):
Double-click on our folder and go to it:
Upload our bot file here:
If you have more than one file, there are folders, zip them, upload the archive to the server, and then unzip it:
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:
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()
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:
Now copy the IP-address of your server from the control panel, from here:
Paste the copied IP into PuTTy and click “Open”:
In the black window opened, in the “login as” line, enter “root” and press “Enter”:
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.
Once you have done everything accurately, you should see roughly the same window:
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:
pip install pyTelegramBotAPI
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:
As we can see, the terminal goes to the folder with bot:
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):
nohup python3 main.py &
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!
Now that our bot is working, let’s find out an another couple of points. Firstly:
To stop a bot launched using nohup, the easiest way in our case is to use the 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.
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.