Skip to content

This is a Python telegram bot template for run it locally, run in Docker directly or deploy it on Heroku by Heroku Container Registry

License

Notifications You must be signed in to change notification settings

sigseg5/python-telegram-bot-docker-heroku-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Requirements

  • Telegram account
  • (Optional) Installed Docker
  • (Optional) Heroku account
  • (Optional) Heroku CLI Tool

Run locally

  • Add TOKEN and MODE environment variables to you IDE config e.g. PyCharm. You can get token from BotFather at Telegram. For local running you can set MODE=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

Run in Docker

  • (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 by pip
  • 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

Results

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

Remove image

  • Run docker images to view list of all images
  • Run docker rmi -f <image_id>

Stop container

  • Run docker stats to view all running containers
  • Run docker kill <container_id>

Deploy on Heroku

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.

Note

This repository based on this repo and article with some improvements and instructions, big thanks to Artem Rys!

About

This is a Python telegram bot template for run it locally, run in Docker directly or deploy it on Heroku by Heroku Container Registry

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project