|
25 | 25 | from web3.middleware import ExtraDataToPOAMiddleware |
26 | 26 |
|
27 | 27 | from configuration.config import ChainId |
28 | | -from configuration.types import Configuration |
| 28 | +from configuration.types import ( |
| 29 | + Configuration, |
| 30 | + NotificationDiscord, |
| 31 | + NotificationGeneric, |
| 32 | + NotificationSlack, |
| 33 | + NotificationTelegram, |
| 34 | +) |
29 | 35 | from observer.reward_epoch_manager import ( |
30 | 36 | Entity, |
31 | 37 | SigningPolicy, |
@@ -62,17 +68,38 @@ def from_vrs(cls, s: SSignature) -> Self: |
62 | 68 | ) |
63 | 69 |
|
64 | 70 |
|
65 | | -def notify_discord(config: Configuration, message: str) -> None: |
66 | | - if config.discord_webhook is None: |
67 | | - return |
68 | | - |
| 71 | +def notify_discord(config: NotificationDiscord, message: str) -> None: |
69 | 72 | requests.post( |
70 | | - config.discord_webhook, |
| 73 | + config.webhook_url, |
71 | 74 | headers={"Content-Type": "application/json"}, |
72 | 75 | json={"content": message}, |
73 | 76 | ) |
74 | 77 |
|
75 | 78 |
|
| 79 | +def notify_slack(config: NotificationSlack, message: str) -> None: |
| 80 | + requests.post( |
| 81 | + config.webhook_url, |
| 82 | + headers={"Content-Type": "application/json"}, |
| 83 | + json={"text": message}, |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +def notify_telegram(config: NotificationTelegram, message: str) -> None: |
| 88 | + requests.post( |
| 89 | + f"https://api.telegram.org/bot{config.bot_token}/sendMessage", |
| 90 | + headers={"Content-Type": "application/json"}, |
| 91 | + json={"chat_id": config.chat_id, "text": message}, |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def notify_generic(config: NotificationGeneric, issue: "Issue") -> None: |
| 96 | + requests.post( |
| 97 | + config.webhook_url, |
| 98 | + headers={"Content-Type": "application/json"}, |
| 99 | + json={"level": issue.level.value, "message": issue.message}, |
| 100 | + ) |
| 101 | + |
| 102 | + |
76 | 103 | async def find_voter_registration_blocks( |
77 | 104 | w: AsyncWeb3, |
78 | 105 | current_block_id: int, |
@@ -259,9 +286,22 @@ def add_message(self, m: str) -> Self: |
259 | 286 | return self |
260 | 287 |
|
261 | 288 |
|
262 | | -def log_issue(config, issue: Issue): |
| 289 | +def log_issue(config: Configuration, issue: Issue): |
263 | 290 | LOGGER.log(issue.level.value, issue.message) |
264 | | - notify_discord(config, issue.level.name + " " + issue.message) |
| 291 | + |
| 292 | + n = config.notification |
| 293 | + |
| 294 | + if n.discord is not None: |
| 295 | + notify_discord(n.discord, issue.level.name + " " + issue.message) |
| 296 | + |
| 297 | + if n.slack is not None: |
| 298 | + notify_slack(n.slack, issue.level.name + " " + issue.message) |
| 299 | + |
| 300 | + if n.telegram is not None: |
| 301 | + notify_telegram(n.telegram, issue.level.name + " " + issue.message) |
| 302 | + |
| 303 | + if n.generic is not None: |
| 304 | + notify_generic(n.generic, issue) |
265 | 305 |
|
266 | 306 |
|
267 | 307 | def extract[T]( |
@@ -503,10 +543,10 @@ async def observer_loop(config: Configuration) -> None: |
503 | 543 | # Issue( |
504 | 544 | # IssueLevel.INFO, |
505 | 545 | # MessageBuilder() |
506 | | - # .add_network(config.chain) |
| 546 | + # .add_network(config.chain_id) |
507 | 547 | # .add_protocol(100) |
508 | 548 | # .add_round(VotingEpoch(12, None)) |
509 | | - # .build_with_message("testing message"), |
| 549 | + # .build_with_message("testing message" + str(config.notification)), |
510 | 550 | # ), |
511 | 551 | # ) |
512 | 552 | # return |
|
0 commit comments