I made this repository as a basic template for my future bots written with python-telegram-bot, which should persist some data in database (I'm using Postgres).
It has simple and clear structure which easy for understand and can be extended with your functionality.
- .gitignore - for ignoring environment libs, python .pyc files.
- Procfile - for deploying to Heroku.
- requirements.txt - contains list of all python libs dependencies.
- run.py - for running bot.
- app/settings.py - you can paste your settings here.
- app/database.py - database connection settings, most likely you don't need to touch them except you really know what you do.
- app/models.py - add your own database models here.
- app/views.py - add your own views (functions that are using to respond to user).
- app/bot.py - here you can add needed handlers and assign them with views from app/views.py.
- Python 3 with pip installed.
- Postgres installed.
- Telegram bot created with BotFather and valid token for it.
- Heroku CLI - only for deployment to Heroku.
- Clone repository firstly:
git clone https://github.com/aperekhozhuk/TelegramBotTemplate && cd TelegramBotTemplate
- Create virtual environment && switch into it:
python3 -m venv env && source env/bin/activate
- Install requirements:
pip install -r requirements.txt
Warning: In some cases "psycopg2" package can demand some additional software (check in google how to install it for your OS).
- Create new database in Postgres and construct url for it.
- Add environment variables for your project (see app/settings.py file).
Warning: You can set settings as environment variables or set it directly in file, but anyway you should keep in secrete your sensitive settings (APP_URL, DATABASE_URL, API_TOKEN), don't commit them - you can use "git stash" instead for example.
- Create database tables:
python app/database.py
- run bot:
python run.py
- Sign up / sign in to Heroku.
- Create new Heroku app.
- Add "Heroku Postgres" add-on to your app.
- Config environment variables in your Heroku app: (on your app page click "Settings" -> "Reveal Config vars")
# Your api token which you got from BotFather.
TELEGRAM_BOT_API_TOKEN
# Heroku creates it automatically by default, just ensure that it presents.
DATABASE_URL
# set it to "PRODUCTION" (default value is "DEVELOPMENT" - only for local running).
TELEGRAM_BOT_MODE
# also created automatically by Heroku but not listed in config vars.
# don't set it because it's Heroku system variable.
# If you overwrite it - you'll have problems, I promise)
PORT
# set it to link to your heroku app (don't forget back slash in the end)
TELEGRAM_BOT_APP_URL
- Move to repository root folder (if you not here).
- Login to Heroku CLI:
heroku login
- Add heroku app repository to your remotes (replace "YourAppName" with your real heroku app name):
heroku git:remote -a YourAppName
- Push project to heroku repository:
git push heroku
- Create database's tables:
heroku run python app/database.py
- Restart your dyno (because first running fails because of database schema missing)
heroku dyno:restart