Introduction
Hey there! Have you ever thought about having your own Telegram bot? Well, you’re in for a treat. In this brief step-by-step guide, we’ll walk through hosting your very own Telegram bot on Ubuntu. Whether you’re a seasoned developer or just dipping your toes into the tech waters, this is your chance to bring exciting ideas to life on a popular messaging platform! So get comfy, and let’s embark on the journey to unleash the full potential of your digital creations.
Prerequisites
Before we begin our learning journey, do note that this guide assumes knowledge of the following:
- Provisioning a VPS
- Familiarity with SSH
- Familiarity with linux command line
- A Ready Telegram Bot Project (if you don’t, feel free to use mine as an example or check out this guide!)
We will not be covering the above as they are not the focus of this guide — there are plenty of guides out there for them as well! All that said, if you need help with any of the above, you are more than welcome to reach out for assistance.
Server Setup
To kickstart your learning journey, you will need to have a VPS (Virtual Private Server) provisioned from cloud providers such as DigitalOcean or OVH. Since we’re hosting a Telegram Bot, you will need to have a project ready to be hosted as well. If you don’t, fret not! You can clone
and make use of the project that I have published here on Github to follow through with the rest of this guide.
Once you have your server provisioned, it’s time to get your hands dirty with the actual setup! First, SSH into your server with the following command (replacing 172.1.1.1 with your actual IP address):
ssh root@172.1.1.1
You will be prompted to enter the root password that would have been provided by your cloud provider. Upon logging in, run the following commands to update packages:
apt-get update && apt-get upgrade
Next, to ensure that we can keep our Telegram Bot running 24/7 later on, we will be using screen which can be installed with the following command:
apt-get install screen
User Setup
While the server setup is completed, it is not a good idea for us to run our python program using the root account. Let us quickly create a new telegrambot user that will be used to handle the hosting of our Telegram Bot. Run the following command to create this new user:
adduser telegrambot
For the remaining part of the guide, exit from your root SSH session and login as telegrambot instead (again replacing 172.1.1.1 with the IP address of your VPS):
ssh telegrambot@172.1.1.1
Project Setup & Launch
Depending on your choice of project, you will need to ensure your project is setup and ready to run. If you are using the project I shared as an example, then the setup for it has been clearly written in the README.
Once the project setup is complete, initialize a new screen session with the following command (this is important if you want your bot to be running 24/7):
screen
Hit enter to continue and in this new screen, start your telegram bot with with the following command (assuming you are using a Python project):
python3 main.py
Detach from the screen by hitting CTRL + A + D on your keyboard. Make sure to hit all 3 at once because just hitting CTRL + D alone will terminate your screen instead. To view the list of screens, you may use the following command:
screen -ls
To reattach to the screen, simply use the command:
screen -r
On a side note, screen (or tmux if you prefer so) has a wide variety of uses beyond just the one shown in this guide. They may be used to run time consuming processes such as training machine learning models or even hosting Minecraft servers!
Conclusion
With that said, your Telegram Bot should now be running 24/7! The guide has made several assumptions about the choice of your projects so depending on your setup (e.g. using JavaScript instead), some commands may be slightly different.
As an additional challenge, you might consider implementing liveness monitoring for your Telegram bot! It’s a step beyond the basics but not significantly more complex — and if you need someone to bounce ideas with, I’m more than happy to do so here or in the comments. Thank you for reading and hope you enjoyed this short walkthrough!