File tree Expand file tree Collapse file tree 1 file changed +21
-10
lines changed Expand file tree Collapse file tree 1 file changed +21
-10
lines changed Original file line number Diff line number Diff line change
1
+ import os
2
+ import logging
3
+ from dpytools .http_clients .base import BaseHttpClient
1
4
2
5
class SlackNotifier :
3
6
4
7
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 } ' )
You can’t perform that action at this time.
0 commit comments