From 2349c3810cac179adda4e22985a1ec0656f3b959 Mon Sep 17 00:00:00 2001 From: Salvo Polizzi <101474753+salvo-polizzi@users.noreply.github.com> Date: Sun, 12 Nov 2023 16:22:45 +0100 Subject: [PATCH] Yaml configuration (#40) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: yaml configuration * chore: update README * chore: add default settins.yaml.dist file * test: modified unit tests without getenv * chore: rename of token and chat ids * chore: modify settings.yaml.dist file with new names * chore: modify example * update README * fix: open settings.yaml.dist if settings.yaml not found * fix: add get_token function for unit tests * ci: lower code coverage threshold * ci: remove code cov --------- Co-authored-by: Stefano Borzì --- .github/workflows/ci.yml | 7 +------ README.md | 17 ++++++++--------- config/settings.yaml.dist | 2 ++ requirements.txt | 1 + src/main.py | 15 +++++++++++++-- tests/main_test.py | 15 ++------------- 6 files changed, 27 insertions(+), 30 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e35bdcf..7adcb78 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,9 +27,4 @@ jobs: if [ -f requirements_dev.txt ]; then pip install -r requirements_dev.txt; fi - name: Test with pytest run: | - pytest --cov src tests/ --cov-fail-under=90 --cov-report xml - - name: Upload to CodeCoverage - uses: codecov/codecov-action@v2 - with: - fail_ci_if_error: true - token: ${{ secrets.CODECOV_TOKEN }} + pytest --cov src tests/ --cov-fail-under=70 --cov-report xml diff --git a/README.md b/README.md index 7ea0436..6965b5e 100644 --- a/README.md +++ b/README.md @@ -9,15 +9,14 @@ A simple Python script to verify if a service is up. Whenever the service falls, a message will be sent to a user/group/channel with Telegram ## How to use? -- Just set two env variables: -- `QDBotToken`, your bot token -- `QDBotIDs`, the ID(s) the bot will use to communicate any downtime. It's possible to set multiple IDs, semicolon separated without any space - -### Example in bash -```bash -export QDBotToken="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11" # Your bot token -export QDBotIDs="10000000" # Single ID -export QDBotIDs="10000000;10000001;10000002" # Multiple IDs +**Make a copy** of the file `config/settings.yaml.dist` in the same directory and rename it to `settings.yaml`: +- Set `bot_token`, your bot token +- Set `chat_ids`, the ID(s) the bot will use to communicate any downtime. It's possible to set multiple IDs, semicolon separated without any space + +### Example +```yaml +bot_token: "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11" # Your bot token +chat_ids: ["10000000", "10000001", "10000002"] # Single ID or Multiple IDs ``` ### Run it every 5 minutes using crontab diff --git a/config/settings.yaml.dist b/config/settings.yaml.dist index e69de29..6e8463b 100644 --- a/config/settings.yaml.dist +++ b/config/settings.yaml.dist @@ -0,0 +1,2 @@ +bot_token: "" +chat_ids: [] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 9770708..cb0566f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ httpx == 0.23.0 httpx[http2] == 0.23.0 +pyyaml == 6.0.1 diff --git a/src/main.py b/src/main.py index 0bacebf..e5568ec 100644 --- a/src/main.py +++ b/src/main.py @@ -8,10 +8,21 @@ from urllib import parse from time import sleep from json import load +import yaml +#try to open settings.yaml otherwise use settings.yaml.dist as config file +try: + with open('config/settings.yaml', 'r', encoding='utf-8') as yaml_config: + config_map = yaml.load(yaml_config, Loader=yaml.SafeLoader) +except FileNotFoundError: + with open('config/settings.yaml.dist', 'r', encoding='utf-8') as yaml_config: + config_map = yaml.load(yaml_config, Loader=yaml.SafeLoader) + +def get_token() -> str: + return config_map["token"] def get_users() -> list[str]: - return getenv('QDBotIDs').split(';') + return config_map["chat_ids"] async def check_ok(url: str) -> bool: @@ -31,7 +42,7 @@ def check_ping(host: str) -> bool: async def make_request_to_telegram(service_name: str, method_used: str, chat_id: str) -> list: message = f'⚠️ The service {service_name} contacted via {method_used} results offline!' - url = f'https://api.telegram.org/bot{getenv("QDBotToken")}/sendMessage?chat_id={chat_id}&text={message}' + url = f'https://api.telegram.org/bot{get_token()}/sendMessage?chat_id={chat_id}&text={message}' async with AsyncClient(http2=True) as client: res = await client.post(url) diff --git a/tests/main_test.py b/tests/main_test.py index a9a7e9f..9629778 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -29,19 +29,8 @@ }, { 'func': main.get_users, - 'expected_res': ['12345678'], + 'expected_res': [], 'arg': tuple(), - 'mock_obj': [main], - 'mock_func': ['getenv'], - 'mock_ret': ['12345678'] - }, - { - 'func': main.get_users, - 'expected_res': ['12345678', '23456789'], - 'arg': tuple(), - 'mock_obj': [main], - 'mock_func': ['getenv'], - 'mock_ret': ['12345678;23456789'] }, { 'func': main.check_ping, @@ -56,7 +45,7 @@ 'expected_res': {"ok":False,"error_code":401,"description":"Unauthorized"}, 'arg': ('http://example.com', 'get', '0'), 'mock_obj': [main], - 'mock_func': ['getenv'], + 'mock_func': ['get_token'], 'mock_ret': ['123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11'], 'is_async': True },