Python Telegram bot template for run it locally, run in Docker directly or deploy it on Heroku by Heroku Container Registry
This is a Python telegram bot template for run it locally, run in Docker directly or deploy it on Heroku by Heroku Container Registry
- Telegram account
- (Optional) Installed Docker
- (Optional) Heroku account
- (Optional) Heroku CLI Tool
- Add
TOKEN
andMODE
environment variables to you IDE config e.g. PyCharm. You can get token from BotFather at Telegram. For local running you can setMODE=dev
E.g. ENV_VAR:PYTHONUNBUFFERED=1;TOKEN=1234567890:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;MODE=dev
- Run
pip install -r requirements.txt
to install dependencies - Run
bot.py
to start
- (Optional) Specify Python version at Dockerfile
- Add TOKEN to .env file. You can get token from BotFather at Telegram
- (Optional) Run
pip freeze > requirements.txt
to add your own dependencies installed bypip
- Run
docker build --tag app_name:1.0 .
for build image - (Optional) Run
docker run --env-file .env app_name:1.0 env
for view all environment variables for this image - Run
docker run --env-file .env test:1.0
to start
After running bot.py using your preferred option, you will get:
2018–12–14 15:12:21,500 — root — INFO — Starting bot
If you go to your bot in Telegram and press Start button, you will get:
2018–12–14 15:12:36,617 — root — INFO — User <user_id> started bot
And if you type /random command, you will get:
2018–12–14 15:12:38,238 — root — INFO — User <user_id> randomed number 2
- Run
docker images
to view list of all images - Run
docker rmi -f <image_id>
- Run
docker stats
to view all running containers - Run
docker kill <container_id>
Detailed guidance in this article
To launch your bot in Heroku you should go through the next steps:
- Create an account or use an existing one
- Create an application (remember about
HEROKU_APP_NAME
) - Specify environmental variables in Settings tab of your application (
MODE=prod
,TOKEN
HEROKU_APP_NAME
) - Setting env variables.
You can read full instructions about deploying images to Heroku here.
Go to the directory where you place bot.py, Dockerfile and execute next steps:
-
Download Heroku CLI here
-
Login
heroku container:login
-
Build and push an image
heroku container:push --app <HEROKU_APP_NAME> web
-
Create a new release
heroku container:release --app <HEROKU_APP_NAME> web
-
Watch logs
heroku logs --tail --app <HEROKU_APP_NAME>
After some time, you should be able to see logs of your bot.
This repository based on this repo and article with some improvements and instructions, big thanks to Artem Rys!