J-Rios / TLG_JoinCaptchaBot

Telegram Bot to verify if users joining a group are human. The Bot sends an image captcha for each new user and kicks any of them who can't solve the captcha in a specified time.
GNU General Public License v3.0
516 stars 217 forks source link

read configuration from environment variables #69

Closed categulario closed 3 years ago

categulario commented 3 years ago

While I was trying to setup an instance of this bot I realized that I had to modify a file in the git tree (constants.py). That means that there's no easy way to update the bot by just pulling new code, due to the working directory not being clean.

Therefore I propose this change, that allows configuration to be read from environment variables so that there's no need to modify a file in the working tree.

I would even go further by doing this:

instead of having a dictionary containing the possible setttings I would put them as top-level variables that can be imported individually:

# Set if you wan't the Bot to be Public or Private
# Public: can be used by any group
# Private: just can be used in allowed groups (Bot owner allow them with /allow_group command)
BOT_PRIVATE = bool(int(os.getenv("BOT_PRIVATE", "0")))

# Bot Token (get it from @BotFather)
TOKEN = os.getenv("TOKEN", "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

# Bot Owner (i.e. "@JoseTLG" or "123456789")
BOT_OWNER = os.getenv("BOT_OWNER", "XXXXXXXXX")

that makes the constants.py a little bit cleaner with the only cost of having to import all the config variables in join_captcha_bot.py, which is not that bad because it allows for linters do detect unused variables or misspellings.

patschi commented 3 years ago

IMHO: I don't necessarily think that it "complicates bot setup", because that's how Docker/Container work. You have your application in a container, and you persist data using volumes and environment variables (while env labels/secrets are more used for defining settings/options, like database connections or so). If someone isn't able able to spin up the bot the intended way using docker, reading the docker documentation or the principle about containers is recommended anyway.

Thanks @categulario for the PR!

J-Rios commented 3 years ago

Then, adding a short text description and pointing with links into Docker and Virtual Environment documentations in Readme file should be enough...

However, @categulario and I have some private conversation and we both agree that maybe the first step should be make a separate configuration file (settings.py) passed to Bot launch, it is a better approach to isolate Bot config from the Bot code and also maintain simplicity in setup while avoid requesting the users to know/learn about other technologies like venv, Docker, etc.

With this approach, Bot launch will be something like:

python3 join_captcha_bot.py -c settings.py

After that, then we can also accept environment variables to bring flexibility of use to both, simple users and advanced-users.

J-Rios commented 3 years ago

Settings file and Environment Variables support added in this commit.

Closing this PR.