Skip to content

Instantly share code, notes, and snippets.

@vlad-ds
Last active July 21, 2024 01:46
Show Gist options
  • Save vlad-ds/b098af8260d57a4490efed68bae50a78 to your computer and use it in GitHub Desktop.
Save vlad-ds/b098af8260d57a4490efed68bae50a78 to your computer and use it in GitHub Desktop.
How to deploy a Telegram bot on AWS EC2

Remote setup (AWS EC2)

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.

Use screen to run the bot

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment