A simple survey in telegram without seeking out external survey providers. Since many organizations use telegram as an information resource to know about deadlines, new events, and many more things; I wanted to create something that is contained within the telegram platform itself.
This repo contains a demo of the main idea with simple backend while focusing more on the DevOps side of the project (for educational purposes).
- The server was created using
python-telegram-bot==13.15to communicate with Telegram,Flask==2.2.3andFlask-SQLAlchemy==3.0.3and manage the admin page and the database. - One Docker file was implemented to build two images: test image and product image. The test image uses an sqlite3 database (for simplicity) that has some basic data to test (test.sqlite3 file in the repo), while the product image will connect to a Postgres database.
- The production database is hosted on the server in separate docker container
postgres:13-alpinethat is managed by the docker-compose file. - The docker-compose will manage and run both of the docker containers: the product (web) and the database (db).
- The workflow
docker-image.ymlhandles building the docker images and running the tests before building and deploying the project to the server. The workflow runs on push and pull_request events on master branch. - Sensitive data (secret keys, passwords, etc.) are saved as secrets in the repository and passed to the docker files.
dockerdocker-compose
You need to set the SQLALCHEMY_TEST_DATABASE_URI, SECRET_KEY, and BOT_TOKEN environment variables.
Then run:
docker build . \
-t test_image \
--target test \
--build-arg SQLALCHEMY_TEST_DATABASE_URI \
--build-arg SECRET_KEY \
--build-arg BOT_TOKENYou can set
SQLALCHEMY_TEST_DATABASE_URIassqlite:///test.sqlite3
You need to set the SQLALCHEMY_DATABASE_URI, SECRET_KEY, and BOT_TOKEN environment variables.
Then run:
docker build . \
-t telegram_survey_bot \
--target product \
--build-arg SQLALCHEMY_DATABASE_URI \
--build-arg SECRET_KEY \
--build-arg BOT_TOKEN
SQLALCHEMY_DATABASE_URIhas the following form:postgresql://$POSTGRES_USER:$POSTGRES_PASS@db:5432/$POSTGRES_DB
After building the product image you need to set the POSTGRES_USER, POSTGRES_PASS, and POSTGRES_DB environment variables.
Then run:
docker-compose build \
--build-arg POSTGRES_USER \
--build-arg POSTGRES_PASS \
--build-arg POSTGRES_DBdocker-compose up Note that if you are running the product for the first you need to create the database by running this command in other terminal:
docker-compose exec web python manage.py create_db