Skip to content

Commit fcde81f

Browse files
committed
feat: add support for multiple notification setups
1 parent 2f279ce commit fcde81f

File tree

4 files changed

+65
-67
lines changed

4 files changed

+65
-67
lines changed

configuration/config.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
NotificationGeneric,
1414
NotificationSlack,
1515
NotificationTelegram,
16+
TelegramBot,
1617
)
1718

1819

@@ -67,35 +68,34 @@ def get_epoch(chain_id: int) -> Epoch:
6768

6869

6970
def get_notification_config() -> Notification:
70-
discord = None
7171
discord_webhook = os.environ.get("NOTIFICATION_DISCORD_WEBHOOK")
72+
discord = []
7273
if discord_webhook is not None:
73-
discord = NotificationDiscord(discord_webhook)
74+
discord.extend(discord_webhook.split(","))
7475

75-
slack = None
7676
slack_webhook = os.environ.get("NOTIFICATION_SLACK_WEBHOOK")
77+
slack = []
7778
if slack_webhook is not None:
78-
slack = NotificationSlack(slack_webhook)
79+
slack.extend(slack_webhook.split(","))
7980

80-
telegram = None
8181
telegram_bot_token = os.environ.get("NOTIFICATION_TELEGRAM_BOT_TOKEN")
8282
telegram_chat_id = os.environ.get("NOTIFICATION_TELEGRAM_CHAT_ID")
83+
telegram = []
8384
if telegram_bot_token is not None and telegram_chat_id is not None:
84-
telegram = NotificationTelegram(
85-
bot_token=telegram_bot_token,
86-
chat_id=telegram_chat_id,
87-
)
85+
bot_tokens = telegram_bot_token.split(",")
86+
chat_ids = telegram_chat_id.split(",")
87+
telegram = [TelegramBot(t, c) for t, c in zip(bot_tokens, chat_ids)]
8888

89-
generic = None
9089
generic_webhook = os.environ.get("NOTIFICATION_GENERIC_WEBHOOK")
90+
generic = []
9191
if generic_webhook is not None:
92-
generic = NotificationGeneric(generic_webhook)
92+
generic.extend(generic_webhook.split(","))
9393

9494
return Notification(
95-
discord=discord,
96-
slack=slack,
97-
telegram=telegram,
98-
generic=generic,
95+
discord=NotificationDiscord(discord),
96+
slack=NotificationSlack(slack),
97+
telegram=NotificationTelegram(telegram),
98+
generic=NotificationGeneric(generic),
9999
)
100100

101101

configuration/types.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,31 +184,36 @@ class Epoch:
184184

185185
@frozen
186186
class NotificationDiscord:
187-
webhook_url: str
187+
webhook_url: list[str]
188188

189189

190190
@frozen
191191
class NotificationSlack:
192-
webhook_url: str
192+
webhook_url: list[str]
193193

194194

195195
@frozen
196-
class NotificationTelegram:
196+
class TelegramBot:
197197
bot_token: str
198198
chat_id: str
199199

200200

201+
@frozen
202+
class NotificationTelegram:
203+
bot: list[TelegramBot]
204+
205+
201206
@frozen
202207
class NotificationGeneric:
203-
webhook_url: str
208+
webhook_url: list[str]
204209

205210

206211
@frozen
207212
class Notification:
208-
discord: NotificationDiscord | None
209-
slack: NotificationSlack | None
210-
telegram: NotificationTelegram | None
211-
generic: NotificationGeneric | None
213+
discord: NotificationDiscord
214+
slack: NotificationSlack
215+
telegram: NotificationTelegram
216+
generic: NotificationGeneric
212217

213218

214219
@frozen

observer/notification.py

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,41 @@ def notify(
2626
pass
2727

2828

29-
def notify_discord(
30-
config: NotificationDiscord, message: str
31-
) -> requests.Response | None:
32-
return notify(
33-
config.webhook_url,
34-
"POST",
35-
headers={"Content-Type": "application/json"},
36-
json={"content": message},
37-
)
29+
def notify_discord(config: NotificationDiscord, message: str) -> None:
30+
for u in config.webhook_url:
31+
notify(
32+
u,
33+
"POST",
34+
headers={"Content-Type": "application/json"},
35+
json={"content": message},
36+
)
3837

3938

40-
def notify_slack(config: NotificationSlack, message: str) -> requests.Response | None:
41-
return notify(
42-
config.webhook_url,
43-
"POST",
44-
headers={"Content-Type": "application/json"},
45-
json={"text": message},
46-
)
39+
def notify_slack(config: NotificationSlack, message: str) -> None:
40+
for u in config.webhook_url:
41+
notify(
42+
u,
43+
"POST",
44+
headers={"Content-Type": "application/json"},
45+
json={"text": message},
46+
)
4747

4848

49-
def notify_telegram(
50-
config: NotificationTelegram, message: str
51-
) -> requests.Response | None:
52-
return notify(
53-
f"https://api.telegram.org/bot{config.bot_token}/sendMessage",
54-
"POST",
55-
headers={"Content-Type": "application/json"},
56-
json={"chat_id": config.chat_id, "text": message},
57-
)
49+
def notify_telegram(config: NotificationTelegram, message: str) -> None:
50+
for t in config.bot:
51+
notify(
52+
f"https://api.telegram.org/bot{t.bot_token}/sendMessage",
53+
"POST",
54+
headers={"Content-Type": "application/json"},
55+
json={"chat_id": t.chat_id, "text": message},
56+
)
5857

5958

60-
def notify_generic(
61-
config: NotificationGeneric, issue: "Message"
62-
) -> requests.Response | None:
63-
return notify(
64-
config.webhook_url,
65-
"POST",
66-
headers={"Content-Type": "application/json"},
67-
json={"level": issue.level.value, "message": issue.message},
68-
)
59+
def notify_generic(config: NotificationGeneric, issue: "Message") -> None:
60+
for u in config.webhook_url:
61+
notify(
62+
u,
63+
"POST",
64+
headers={"Content-Type": "application/json"},
65+
json={"level": issue.level.value, "message": issue.message},
66+
)

observer/observer.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,12 @@ def log_message(config: Configuration, message: Message):
182182

183183
n = config.notification
184184

185-
if n.discord is not None:
186-
notify_discord(n.discord, message.level.name + " " + message.message)
185+
lvl_msg = f"{message.level.name} {message.message}"
187186

188-
if n.slack is not None:
189-
notify_slack(n.slack, message.level.name + " " + message.message)
190-
191-
if n.telegram is not None:
192-
notify_telegram(n.telegram, message.level.name + " " + message.message)
193-
194-
if n.generic is not None:
195-
notify_generic(n.generic, message)
187+
notify_discord(n.discord, lvl_msg)
188+
notify_slack(n.slack, lvl_msg)
189+
notify_telegram(n.telegram, lvl_msg)
190+
notify_generic(n.generic, message)
196191

197192

198193
async def observer_loop(config: Configuration) -> None:

0 commit comments

Comments
 (0)