If you run your Telegram bot locally, it will stop working when you close the terminal. The alternative is to host the bot somewhere.
There are many ways to do this. I chose AWS EC2 as it's a cheap and easy option.
Create an AWS account and start an EC2 instance. Install pip, git and github cli. If you use an Amazon Linux image (which is what the free tier offers) you can run the commands in this gist.
Connect to your GitHub account with gh auth login
and clone this repo. Create the .env
in the bin/
folder and add your secrets.
You should now be able to run the bot with python3 <repo>/bin/bot.py
.
You can use screen
to run the bot even after you close the connection. See the section below.
I recommend using the s3
storage option if you are going to run the bot in EC2.
screen
is a tool that allows you to run a process in the background.
You can use it to run the bot on a remote server.
Say you decided to run a process named chatlingo
. Use these commands to run it with screen:
# create dedicated session
screen -S chatlingo
# start the bot
python3 chatlingo/bin/bot.py
# press ctrl+a then d to detach
# list sessions
screen -ls
# return to the session
screen -r chatlingo
# kill the session
screen -X -S chatlingo quit