Skip to content

Commit

Permalink
Yaml configuration (#40)
Browse files Browse the repository at this point in the history
* 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ì <stefanoborzi32@gmail.com>
  • Loading branch information
salvo-polizzi and Helias authored Nov 12, 2023
1 parent 6391763 commit 2349c38
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 30 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/settings.yaml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bot_token: ""
chat_ids: []
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
httpx == 0.23.0
httpx[http2] == 0.23.0
pyyaml == 6.0.1
15 changes: 13 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
15 changes: 2 additions & 13 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
},
Expand Down

0 comments on commit 2349c38

Please sign in to comment.