Skip to content

Commit 514e3b8

Browse files
committed
Salck Notification Working
1 parent 00c29ea commit 514e3b8

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

dpytools/slack/slack.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1+
import os
2+
import logging
3+
from dpytools.http_clients.base import BaseHttpClient
14

25
class SlackNotifier:
36

47
def __init__(self):
5-
# Set a webhok via an env var, ask Mike for a
6-
#web hook url.
7-
...
8-
9-
def notify(self, msg: str):
10-
# Check formatting options for messages to slack.
11-
# From memory you style it via sending a dictionary.
12-
# It's a post request so do use the http client
13-
# we've developing elsewhere in this library.
14-
...
8+
self.webhook_url = os.getenv("SLACK_WEBHOOK_URL")
9+
if not self.webhook_url:
10+
raise ValueError('SLACK_WEBHOOK_URL is not set')
11+
self.http_client = BaseHttpClient()
12+
self.validate_webhook_url()
13+
14+
def validate_webhook_url(self):
15+
response = self.http_client.get(self.webhook_url)
16+
if response.status_code != 200:
17+
logging.error(f'Invalid SLACK_WEBHOOK_URL: {response.status_code}')
18+
raise ValueError('Invalid SLACK_WEBHOOK_URL')
19+
20+
def notify(self, msg: dict):
21+
try:
22+
response = self.http_client.post(self.webhook_url, json=msg)
23+
response.raise_for_status()
24+
except Exception as e:
25+
logging.error(f'Failed to send notification: {e}')

0 commit comments

Comments
 (0)