This repository contains a Python implementation of a Trivia Telegram bot. The bot plays Trivia game with a user. During the game the bot gives a sequence of questions which the user must answer. The main goal is to get the maximum score for correct answers by the end of the game.
NOTE: The instruction below is shown for MacOS.
- Install Homebrew Official page - Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)".
- Install
pyenv. PyEnv - Documentation on GitHub.brew install pyenv.- Add to your profile (e.g.
~/.zshrc)eval "$(pyenv init -)"to turn onpyenv.
- Install
Python 3.10.0.pyenv install 3.10.0.
- Install
pyenv-virtualenv.brew install pyenv-virtualenv.
- Create a new virtual environment.
pyenv virtualenv 3.10.0 bot.
- Restart your Shell.
- To make this interpreter default for the project
cd <repo root>- run
pyenv local bot
- Install the requirements:
pip install -r requirements.txt. - Install Docker from the Docker official page.
- Optinally install PgAdmin here for inspecting the database.
- Install PyCharm from Official page.
- Open the repo root as a new project.
- Mark directories:
src- sources root,tests- tests root. Right click to do so. - Set
pyenv botas the project interpreter.
The bot relies on environment to get DB credentials. Launching the database and the bot assumes the below variables are set.
- Set
POSTGRES_DB_USERandPOSTGRES_DB_PASSWDenvironment variables. Put them in your profile (e.g..zshrc)export POSTGRES_DB_USER='postgres'export POSTGRES_DB_PASSWD=<password>export POSTGRES_DB_HOST='localhost'
The bot relies on being able to connect to database for storing it's state and accessing questions.
- Run postgres docker container:
docker run -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=$POSTGRES_DB_USER -e POSTGRES_USER=$POSTGRES_DB_PASSWD -d postgres.- You can stop it by
docker stop postgresand rerun bydocker start postgrescommand.
- You can stop it by
cd src/scripts- Scrap the questions from OpenTriviaDB
python scrap_opentrivia.py. - Move the scrapped questions to the DB
python populate_db.py populate
- Ensure postgres docker container is running.
cd <repo_root>/srcpython main.py
- Build the docker image with
docker build --platform=linux/amd64 --tag triviabot .to create an image based x64 architecture.
- To run the container locally you should create a bridge network for
botcontainer to be able to talk topostgresdocker network create --driver bridge bot-net. It should be done once. After the network creation you can see it in the list bydocker network lscommand.
docker run --name postgres -e POSTGRES_PASSWORD=$POSTGRES_DB_PASSWD --network bot-net -p 5432:5432 -d postgresto connectpostgrescontainer to the network.docker run -dit -e POSTGRES_DB_USER=$POSTGRES_DB_USER -e POSTGRES_DB_PASSWD=$POSTGRES_DB_PASSWD -e POSTGRES_DB_HOST=$POSTGRES_DB_HOST -e POSTGRES_DB_NAME=$POSTGRES_DB_NAME -e TELEGRAM_BOT_TOKEN=$TELEGRAM_BOT_TOKEN --name bot --network bot-net triviabotto connect your docker image to the network and run bot locally.docker network inspect bot-netto check if the containers are connected properly. Below this command the information about connection will be represented. You have to seeContainerskey where there are two lists dedicated topostgresandbotcontainers with their IP addresses. If both are shown there, everything is ok with the connection.- Do the additional checks from this tutorial to be make sure the connection is good by pinging it.
- While your containers are running in the background, use
docker attachcommand to connect each container alternately. i.e. for thepostgrescontainer:docker attach postgresping -c 2 google.comping -c 2 botping -c 2 <IP address of bot>- Detach from
postgreswithout stopping it by using the detach sequence, <CTRL+p> <CTRL+q> (hold down CTRL and type p followed by q). - Repeat the same steps for
botcontainer.
- While your containers are running in the background, use
The bot relies on environment to get DB credentials. Launching the database and the bot assumes the below variables are set.
export POSTGRES_DB_USER="postgres"export POSTGRES_DB_PASSWD=<password>export POSTGRES_DB_HOST="triviabotdb.cuiy2vsvegcs.eu-central-1.rds.amazonaws.com"export POSTGRES_DB_NAME="initial_db"export TELEGRAM_BOT_TOKEN=<bot token>
- Before you launch
boton AWS, see the sectionCreating a docker imageabove. - Use the existing resources below by following the links:
- If the database is empty, populate your json file with questions to AWS database by
python populate_db.py populatefrom <repo_root>/src/scripts. - If you build the docker
botimage with the latest updates, push it to AWS repository following this tutorial.aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin 158048943261.dkr.ecr.eu-central-1.amazonaws.com.docker imagesdocker tag <image_id> 158048943261.dkr.ecr.eu-central-1.amazonaws.com/trivia_bot:latestdocker push 158048943261.dkr.ecr.eu-central-1.amazonaws.com/trivia_bot:latest
- Run your VM. Connect to your VM using SSH and:
- If any of the resources below do not exist, follow the links and install it once:
- Authenticate with AWS ECR by
aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin 158048943261.dkr.ecr.eu-central-1.amazonaws.com. - Pull your docker image using
docker pull 158048943261.dkr.ecr.eu-central-1.amazonaws.com/trivia_bot:latest.
- Configure environment variables in your VM.
- To run
botinserver mode:docker run -p 443:443 -e TELEGRAM_BOT_TOKEN=$TELEGRAM_BOT_TOKEN -e POSTGRES_DB_USER=$POSTGRES_DB_USER -e POSTGRES_DB_PASSWD=$POSTGRES_DB_PASSWD -e POSTGRES_DB_HOST=$POSTGRES_DB_HOST -e POSTGRES_DB_NAME=$POSTGRES_DB_NAME -v /home/ubuntu/keys/:/home/ubuntu/keys --name triviabot 158048943261.dkr.ecr.eu-central-1.amazonaws.com/trivia_bot:latest "python" "main.py" "server" "https://ec2-3-19-61-96.us-east-2.compute.amazonaws.com/handleUpdate" "0.0.0.0" "443" "--cert-path" "/home/ubuntu/keys/cert.pem" "--key-path" "/home/ubuntu/keys/key.key" - To run
botinclient mode:docker run -p 5432:5432 -e TELEGRAM_BOT_TOKEN=$TELEGRAM_BOT_TOKEN -e POSTGRES_DB_USER=$POSTGRES_DB_USER -e POSTGRES_DB_PASSWD=$POSTGRES_DB_PASSWD -e POSTGRES_DB_HOST=$POSTGRES_DB_HOST -e POSTGRES_DB_NAME=$POSTGRES_DB_NAME -v /home/ubuntu/keys/:/home/ubuntu/keys --name triviabot 158048943261.dkr.ecr.eu-central-1.amazonaws.com/trivia_bot:latest "python" "main.py" "client"