forked from thnx4playing/streamlink-with-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification_manager.py
More file actions
99 lines (82 loc) · 3.23 KB
/
Copy pathnotification_manager.py
File metadata and controls
99 lines (82 loc) · 3.23 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import json
import requests
from abc import ABC, abstractmethod
import logging
logger = logging.getLogger(__name__)
from enum import Enum, auto
class NotifierType(Enum):
SLACK = auto()
TELEGRAM = auto()
class Notifier(ABC):
@abstractmethod
def notify(self, message):
raise NotImplementedError("Subclasses must implement the notify method")
class NotificationManager:
def __init__(self, config):
self.config = config
self.notifiers = []
self.initialize_notifiers()
def initialize_notifiers(self):
slack_id = self.config.slack_id
if slack_id:
self.notifiers.append(NotifierFactory.create_notifier(NotifierType.SLACK, slack_id=slack_id))
telegram_bot_token = self.config.telegram_bot_token
telegram_chat_id = self.config.telegram_chat_id
if telegram_bot_token and telegram_chat_id:
self.notifiers.append(NotifierFactory.create_notifier(NotifierType.TELEGRAM, bot_token=telegram_bot_token, chat_id=telegram_chat_id))
def add_notifier(self, notifier):
self.notifiers.append(notifier)
def notify_all(self, message):
for notifier in self.notifiers:
notifier.notify(message)
class SlackNotifier(Notifier):
def __init__(self, slack_id):
self.slack_id = slack_id
def notify(self, message):
slack_url = f"https://hooks.slack.com/services/{self.slack_id}"
slack_data = {"text": message}
try:
response = requests.post(
slack_url,
data=json.dumps(slack_data),
headers={"Content-Type": "application/json"},
timeout=30,
)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
logger.error(f"HTTP error occurred: {e}")
except Exception as e:
logger.error(f"Unexpected error occurred while sending message to Slack: {e}")
class TelegramNotifier(Notifier):
def __init__(self, bot_token, chat_id):
self.bot_token = bot_token
self.chat_id = chat_id
def notify(self, message):
telegram_url = f"https://api.telegram.org/bot{self.bot_token}/sendMessage"
telegram_data = {
"chat_id": self.chat_id,
"text": message,
}
try:
response = requests.post(
telegram_url,
data=json.dumps(telegram_data),
headers={"Content-Type": "application/json"},
timeout=30,
)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
logger.error(f"HTTP error occurred: {e}")
except Exception as exc:
logger.error(f"Unexpected error occurred while sending message to Telegram: {exc}")
class NotifierFactory:
_notifiers = {
NotifierType.SLACK: SlackNotifier,
NotifierType.TELEGRAM: TelegramNotifier,
}
@staticmethod
def create_notifier(notifier_type: NotifierType, *args, **kwargs):
notifier_class = NotifierFactory._notifiers.get(notifier_type, None)
if notifier_class:
return notifier_class(*args, **kwargs)
raise ValueError(f"Notifier type '{notifier_type}' is not supported.")