feat: add token in config.ini

Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
Macintxsh 2025-03-02 14:09:49 +03:00
parent 6ec797a387
commit 1178c80831
Signed by: mctaylors
GPG key ID: 4EEF4F949A266EE2
4 changed files with 24 additions and 12 deletions

19
main.py
View file

@ -17,12 +17,12 @@ logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
config = configparser.ConfigParser()
config.read('config.ini')
config.read("config.ini")
def main() -> None:
validate_config()
application = Application.builder().token(os.getenv("BOT_TOKEN")).build()
application = Application.builder().token(get_token()).build()
application.add_handler(CommandHandler("start", start_command))
application.add_handler(CommandHandler("help", help_command))
@ -36,13 +36,24 @@ def main() -> None:
def validate_config():
# noinspection PyBroadException
try:
response = requests.get(f"https://{config.get('General', 'Domain')}/profile.json")
response = requests.get(f"https://{config.get('Service', 'Domain')}/profile.json")
if response.status_code != 200:
raise
except:
print('Unable validate Domain in config.ini.')
print("Unable validate Domain in config.ini.")
exit(1)
def get_token():
if os.getenv("BOT_TOKEN") is not None:
return os.getenv("BOT_TOKEN")
if config.get('General', 'Token') != "":
return config.get('General', 'Token')
print("Set BOT_TOKEN environment variable or use config.ini to set bot token.")
exit(1)
if __name__ == "__main__":
main()