From ae147d4dd0b08d097aec3cbeb28a464d69e4102e Mon Sep 17 00:00:00 2001 From: dev-fatal Date: Sat, 11 Feb 2023 17:00:15 +0000 Subject: [PATCH] Comments, cleaner config, exit if no token in main.py --- main.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index 6df9ea8..00136dd 100644 --- a/main.py +++ b/main.py @@ -1,26 +1,30 @@ from message import get_chat_id from monitor import monitor import toml +import sys def load_config(): - config_path = "config.toml" - with open(config_path) as file: - config = toml.load(file) - if config["chat_id"] == "": - chat_id = None - while not chat_id: - input("Please send a message to the Telegram bot you created. Once done, wait 1 minute then press Enter.") - chat_id = get_chat_id(config["token"]) - config["chat_id"] = chat_id - with open(config_path, "w") as file: - toml.dump(config, file) + config_location = "config.toml" + with open(config_location) as file: + config = toml.load(file) # Make a dict of the config values + + if not config["token"]: # No token supplied + sys.exit("Please enter a token in the config file") + while not config["chat_id"]: # Keep trying to find chat id until we get a valid one + input("Please send a message to the Telegram bot you created. Once done, wait 1 \ + minute then press Enter. If it doesn't work, send another message then try \ + again.") + config["chat_id"] = get_chat_id(config["token"]) + + with open(config_location, "w") as file: + toml.dump(config, file) # Output new config (to file) with updated chat id return config def main(): config = load_config() - monitor(config["path"], config["token"], config["chat_id"]) + monitor(config) if __name__ == "__main__":