Simple Telegram Bot
Find here the source code of the Advanced Telegram Chatbots: features that make a difference article.
- Setup
- Run on Local
- Deploy to Heroku with Git
- Deploy to Heroku with Docker (on x86-64 architecture)
- Deploy to Heroku with Docker (on ARM architecture)
Table of contents generated with markdown-toc
Clone the repository
git clone https://github.com/gcatanese/TelegramBotDemo.git
Create and .env file in the same folder as app.py. The .env file defines the environment variables.
- TELEGRAM_TOKEN={your Telegram token} [here]
Run the application
cd telegram_bot
python telegram_bot.py
Access the bot via the deeplink https://t.me/{bot_username}
and start chatting
Note: the chatbot runs in Polling mode
- Heroku CLI
requirements.txt
must be in the root folder, it defines the required dependenciesruntime.txt
must be in the root folder, it sets the Python versionProcfile
must be in the root folder, it declares the startup command
Create a new app
heroku create advancedtelegrambot
Check the remote is correct
git remote -v
Note: the remote is called heroku
by default but it can be renamed
Define the Python buildpack
heroku buildpacks:set heroku/python
Configure the environment variables (Heroku Config Vars)
heroku config:set TELEGRAM_TOKEN="{your Telegram token}"
heroku config:set MODE="webhook"
heroku config:set WEBHOOK_URL="https://advancedtelegrambot.herokuapp.com/"
git push heroku main
When working on Intel64 architecture the Docker image can be built and pushed using the Heroku CLI.
- Heroku CLI
Dockerfile
must be in the root folder- Login into the Heroku Docker Registry:
heroku container:login
Create a new app
heroku create advancedtelegrambot
Configure the environment variables (Heroku Config Vars)
heroku config:set TELEGRAM_TOKEN="{your Telegram token}"
heroku config:set MODE="webhook"
heroku config:set WEBHOOK_URL="https://advancedtelegrambot.herokuapp.com/"
Build and push the image
heroku container:push web
Release the image
heroku container:release web
When working on ARM architecture the Docker image needs to be made compatible with Heroku x64, using the Docker command line together with the Buildx plugin.
- Heroku CLI
Dockerfile
must be in the root folder- Login into the Heroku Docker Registry:
heroku container:login
Create a new app
heroku create advancedtelegrambot
Configure the environment variables (Heroku Config Vars)
heroku config:set TELEGRAM_TOKEN="{your Telegram token}"
heroku config:set MODE="webhook"
heroku config:set WEBHOOK_URL="https://advancedtelegrambot.herokuapp.com/"
Build the image setting the platform
used by Heroku
docker buildx build --platform linux/amd64 -t advancedtelegrambot .
Tag the image following Heroku naming conventions
docker tag advancedtelegrambot registry.heroku.com/advancedtelegrambot/web
Push the image into Docker Registry
docker push registry.heroku.com/advancedtelegrambot/web
Release the image
heroku container:release web -a advancedtelegrambot