Skip to content

Commit 3d57b8b

Browse files
authored
Merge pull request #10 from GSS-Cogs/5-Slack-Notification
5 slack notification
2 parents 5f79568 + 5c8fe55 commit 3d57b8b

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

dpytools/slack/slack.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1+
import logging
2+
from dpytools.http_clients.base import BaseHttpClient
13

24
class SlackNotifier:
35

4-
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-
...
6+
def __init__(self, webhook_url):
7+
if not webhook_url:
8+
raise ValueError('webhook_url is not set')
9+
self.webhook_url = webhook_url
10+
self.http_client = BaseHttpClient()
11+
12+
def notify(self, msg_dict: dict):
13+
"""
14+
Send a message to the Slack webhook.
15+
16+
The msg_dict parameter should be a dictionary that matches the
17+
structure documented at https://api.slack.com/messaging/webhooks
18+
"""
19+
try:
20+
response = self.http_client.post(self.webhook_url, json=msg_dict)
21+
response.raise_for_status()
22+
except Exception as e:
23+
logging.error(f'Failed to send notification: {e}')
24+
25+
def msg_str(self, msg: str):
26+
"""
27+
Send a string message to the Slack webhook.
28+
29+
The msg parameter is wrapped into a dictionary before being sent.
30+
"""
31+
self.notify({'text': msg})

tests/test_slack.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
from unittest.mock import patch, MagicMock
3+
from requests import HTTPError, Response
4+
from dpytools.http_clients.base import BaseHttpClient
5+
from dpytools.slack.slack import SlackNotifier
6+
7+
@patch.object(BaseHttpClient, 'post')
8+
def test_notify(mock_post):
9+
"""
10+
Test that the notify method sends a POST request
11+
"""
12+
webhook_url = 'http://example.com'
13+
mock_response = MagicMock(Response)
14+
mock_response.status_code = 200
15+
mock_post.return_value = mock_response
16+
17+
notifier = SlackNotifier(webhook_url)
18+
notifier.notify({'text': 'Test message'})
19+
20+
mock_post.assert_called_once_with(webhook_url, json={'text': 'Test message'})
21+
22+
@patch.object(BaseHttpClient, 'post')
23+
def test_msg_str(mock_post):
24+
"""
25+
Test that the msg_str method sends a POST request with a string message
26+
"""
27+
webhook_url = 'http://example.com'
28+
mock_response = MagicMock(Response)
29+
mock_response.status_code = 200
30+
mock_post.return_value = mock_response
31+
32+
notifier = SlackNotifier(webhook_url)
33+
notifier.msg_str('Test message')
34+
35+
mock_post.assert_called_once_with(webhook_url, json={'text': 'Test message'})

0 commit comments

Comments
 (0)