Simple, but extensible template for telegram bot, using pyTelegramBotAPI and adaptix
git clone https://github.com/Cub11k/tgBotTemplate.git # via HTTPS
# or
git clone [email protected]:Cub11k/tgBotTemplate.git # via SSH
cd tgBotTemplate- Change package name, description, version, author, homepage, etc. in
pyproject.toml - Create virtual environment or use the existing one
- Activate virtual environment
- Installation options
- Install the package in editable mode
- Install the dependencies from a lock-file without installing the package itself
# Option #1 - installing the package in editable mode
pip install -e .
# Option #2 - installing only the dependencies
pip install -r requirements.txtIn this template, the requirements.txt file is in fact a lock-file, generated using pip-compile
It contains a fixed set dependencies with strictly fixed versions (==) and is crucial
for having stable reproducible builds in production. Not to be edited manually
as it can lead to conflicts ❗ ❗
If you want to change this file, e.g. your set of dependencies in pyproject.toml has changed,
or you want to update the versions of existing packages, simply run
# this will ensure that requirements.txt satisfies the constraints set in your project
# it will not change the versions of packages if they already satisfy the constraints set
pip-compile pyproject.toml -o requirements.txt
# to force the update use --upgrade or --upgrade-package for specific packages (can be used multiple times)
pip-compile pyproject.toml --upgrade -o requirements.txt
# or
pip-compile pyproject.toml --upgrade-package pytelegrambotapi --upgrade-package adaptix==3.0.0b7 -o requirements.txtBefore running the bot you'll have to configure the environment using the environment variables
| Environment variable | Description | Allowed values |
|---|---|---|
| CONFIG_PATH | Path to the config file to use | Default config.toml |
| CONFIG_USE_ENV_VARS | Override config file with environment variables | True, 1Default False |
| CONFIG_ENV_MAPPING_PATH | Path to the file with mapping of config values and env vars | Default config_env_mapping.toml |
The simplest way to run the bot using long polling is to use the launch-polling script
launch-polling <path-to-the-config-file>To get more details about the script, run it with the --help flag
launch-polling --helpTo run the bot using webhook, you'll have to adjust the module mybot:webhook
according to the web-framework used
After that, you can launch the app using the web-server of your choice, e.g. gunicorn
gunicorn 'mybot:webhook_app()' --bind=$HOST:$PORT --workers-class=$WORKERS_CLASSpip uninstall <your-package-name>Beware that mybot is not the package name, but the name of the module,
the package name is defined in pyproject.toml
In project root execute:
docker build -t tgbot .In any case subsititute token with your Telegram Bot API Token
Run in interactive mode (TTY + stdin):
docker run -it --rm -e MYAPP_BOT_TOKEN=token tgbotRun detached (in background) with restart policy:
docker run -d --restart unless-stopped -e MYAPP_BOT_TOKEN=token tgbotRun detached with bind mounts:
docker run -d --restart unless-stopped -v ./config:/config -v ./logs:/logs -e MYAPP_BOT_TOKEN=token tgbotBy default provided docker-compose.yml will build project in new image and run it with redis instance as a state storage.
Provide .env file with MYAPP_BOT_TOKEN set to your Telegram Bot API Token.
Also in .env you can pass any configuration in env vars form according to your config.toml and config_env_mapping.toml.
docker compose up -d --builddocker compose downBy default there are 4 volumes created by docker-compose.yml:
- config - directory with
config.tomlandconfig_env_mapping.tomlwhich are used by running instance - logs - directory with logs (by default loggers are write in stdout and in files in this directory)
- redis-config - redis configuration
- redis-data - redis data storage (for persistence)
Feel free to contribute to the project by creating issues and pull requests