-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnotifier.py
More file actions
67 lines (57 loc) · 2.46 KB
/
notifier.py
File metadata and controls
67 lines (57 loc) · 2.46 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
import json
from typing import Optional
from datetime import datetime # This import was missing
class Notifier:
def __init__(self, slack_webhook: Optional[str] = None, discord_webhook: Optional[str] = None):
self.slack_webhook = slack_webhook
self.discord_webhook = discord_webhook
def send(self, message: str, level: str = "info"):
"""Send notification to configured channels"""
# Discord
if self.discord_webhook:
try:
# Color mapping
color = {
"info": 5814783, # Blue
"success": 3066993, # Green
"warning": 16776960, # Yellow
"error": 15548997 # Red
}.get(level, 5814783)
# Simple message format (more reliable)
payload = {
"content": f"```{message}```" # Wrap in code block for better formatting
}
# Or use embed format (more fancy)
# payload = {
# "embeds": [{
# "description": message,
# "color": color,
# "timestamp": datetime.now().isoformat(),
# "title": f"GitHub Automation - {level.upper()}"
# }]
# }
response = requests.post(
self.discord_webhook,
json=payload,
timeout=5
)
if response.status_code == 204: # Discord returns 204 on success
print(f"✅ Discord notification sent")
else:
print(f"⚠ Discord returned status: {response.status_code}")
except Exception as e:
print(f"⚠ Discord notification failed: {e}")
# Slack
if self.slack_webhook:
try:
payload = {"text": message}
response = requests.post(
self.slack_webhook,
json=payload,
timeout=5
)
if response.status_code == 200:
print(f"✅ Slack notification sent")
except Exception as e:
print(f"⚠ Slack notification failed: {e}")