Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yaml configuration #40

Merged
merged 12 commits into from
Nov 12, 2023
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 `QDBotToken`, your bot token
- Set `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
```yaml
bot-token: "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11" # Your bot token
chat_ids: ["10000000", "10000001", "10000002"] # Single ID or Multiple IDs
salvo-polizzi marked this conversation as resolved.
Show resolved Hide resolved
```

### Run it every 5 minutes using crontab
Expand Down
2 changes: 2 additions & 0 deletions config/settings.yaml.dist
Helias marked this conversation as resolved.
Show resolved Hide resolved
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
8 changes: 6 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
from urllib import parse
from time import sleep
from json import load
import yaml

with open('config/settings.yaml', 'r', encoding='utf-8') as yaml_config:
config_map = yaml.load(yaml_config, Loader=yaml.SafeLoader)


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 +35,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{config_map["bot_token"]}/sendMessage?chat_id={chat_id}&text={message}'

async with AsyncClient(http2=True) as client:
res = await client.post(url)
Expand Down
18 changes: 2 additions & 16 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 @@ -53,11 +42,8 @@
},
{
'func': main.make_request_to_telegram,
'expected_res': {"ok":False,"error_code":401,"description":"Unauthorized"},
'expected_res': {"ok":False,"error_code":400,"description":"Bad Request: chat not found"},
'arg': ('http://example.com', 'get', '0'),
'mock_obj': [main],
'mock_func': ['getenv'],
'mock_ret': ['123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11'],
'is_async': True
},
{
Expand Down
Loading