- Notifications
You must be signed in to change notification settings - Fork5.7k
Hosting your bot
You will need a VPS (or dedicated server) first. Check out the list atWhere to host Telegram Bots if you don't have one already.
Your login details should contain
- The IP address of your server
- Your username (usually
root
) - Your password
You should also receive some information on the type of server you got, including Operating System, RAM, storage and monthly traffic.
Once you received that information, you can connect to the server via SSH.
Note that although the explanations below mention entering your password, it is strongly recommended to authenticate using aSSH public key instead of the password. You can readthis explanation on how to generate keys and send the public key to the server, or check your VPS provider's documentation.
Run the following command in the terminal and replace<user>
with your username and<ip>
with your servers IP address:
ssh <user>@<ip>
Confirm that you want to trust the host and enter the password if you are asked to do so.
InstallpuTTY and start it.
In the fieldHost Name (or IP address) enter the IP address of your server. As the connection type, selectSSH and setPort to22. You can save these settings my entering a name in the field belowSaved Sessions and clickingSave. Then, clickOpen and enter your username and password when asked to do so.
First, install the Python package managerpip
.
apt-get update && apt-get install python3-pip
Now, install thepython-telegram-bot
library:
pip3 install python-telegram-bot
Finally, confirm the installation:
python3 -c "import telegram;print(telegram.__version__)"
Of course, this is the easy way; the correct way (especially if you have multiple Python projects on your server) is to install avirtual environment (likepipenv
orpoetry
) first.
Now you can upload your bot to the server. There are multiple ways to do that, one way is to useFileZilla. Install it (if you're on Linux, chances are you already have it) and start it. Open theServer Manager and create a new server with the button on the left. Give it a nice name, then go to the right and fill in the fields:
- Server: Your servers IP address
- Port: 22
- Protocol: SFTP
- Connection type: Normal
- Username: Your username
- Password: Your password
Now, clickConnect. You will probably see the/root
directory (your user's home directory) on the right and your local files on the left. Create a directory for your bot and upload all the files needed by your bot into that directory.
To run your bot, connect to your server again via SSH (or go back to the connection) andcd
into the directory you created. You could now immediately start the bot, but then it would stop working once you disconnect from the server. There are again several ways to make sure that doesn't happen:
- Use a terminal multiplexer like
screen
ortmux
. - Run your bot as a systemd service.
screen
is called a "terminal multiplexer". It createsvirtual terminals that you can attach to and detach from and that can run processes without you being logged in.
Create a newscreen and attach to it:
screen -S mybot
Start the bot:
python3 bot.py
Detach from thescreen by holdingCTRL and pressingA, thenD. You can now disconnect from the server by typingexit
if you want.
To re-attach to thescreen after you logged back in:
screen -r mybot
or
screen -d -r mybot
A known alternative toscreen
istmux
.
Choosing this option will mean that your Python program will be running in the background. Another upside to this option is that you can set it up to start at system boot, which means that if you reboot your server (e.g. to apply system upgrades), your bot will start automatically afterwards.
- Install
python-dotenv
. - Create the
.env
file and put your bot-related environment variables in there (you didn't put your bot token right into your code, hopefully!). If you're using Git, make sure.env
is in.gitignore
so it doesn't get checked in. - Follow e.g.this tutorial to run your Python script as a service. If you're using a virtual environment (seethis external article for details), you have to locate the Python executable first. To do that, activate the virtual environment on the remote server and enter
which python
in your bash console. You will get a path to the Python instance you will need to use when configuring systemd.
Steps 1 and 2 are optional if you're not using any virtual environment, but it's better to go through them anyway because if you set the environment variables manually, they will be lost after system reboot.
If you plan on hosting multiple bots on your server, it's recommended to usevirtualenv
. It allows you to install and upgrade Python modules viapip
for one project, without worrying how it affects other projects on the server. Readthis external article for more information.
Learn about how to use a webhook for your bot inthis article.
You might also read the article onPerformance Optimizations if you didn't read it yet.
Wiki ofpython-telegram-bot
© Copyright 2015-2025 – Licensed byCreative Commons
- Architecture Overview
- Builder Pattern for
Application
- Types of Handlers
- Working with Files and Media
- Exceptions, Warnings and Logging
- Concurrency in PTB
- Advanced Filters
- Storing data
- Making your bot persistent
- Adding Defaults
- Job Queue
- Arbitrary
callback_data
- Avoiding flood limits
- Webhooks
- Bot API Forward Compatiblity
- Frequently requested design patterns
- Code snippets
- Performance Optimizations
- Telegram Passport
- Bots built with PTB
- Automated Bot Tests