-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification.py
More file actions
35 lines (30 loc) · 1.15 KB
/
notification.py
File metadata and controls
35 lines (30 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from config import get_config
import json
import urllib
# Sends a notification to a webhook URL
# Fails silently
def send_webhook_notification(message: str, status:str="") -> str:
config = get_config()
err_msg = ""
webhook_url = config.get("WEBHOOK_URL")
if not webhook_url:
err_msg = "Webhook URL not provided."
payload = {
"hostname": config["HOST_NAME"],
"message": message
}
if status:
payload["status"] = status
data = json.dumps(payload).encode('utf-8')
req = urllib.request.Request(webhook_url, data=data, headers={'content-type': 'application/json'})
try:
with urllib.request.urlopen(req) as response:
if response.status != 200:
err_msg = f"Failed to send webhook notification. Status code: {response.status}"
except urllib.error.HTTPError as e:
err_msg = f'HTTP error occurred while sending webhook: {e.code} {e.reason}'
except urllib.error.URLError as e:
err_msg = f'URL error occurred while sending webhook: {e.reason}'
except Exception as e:
err_msg = f'Error occurred while sending webhook: {e}'
return err_msg