|
1 | 1 | import json |
2 | 2 | from abc import ABC, abstractmethod |
3 | | -from typing import Dict, List, Optional, Tuple |
| 3 | +from typing import Dict, List, Optional, Tuple, Union |
4 | 4 |
|
| 5 | +import requests |
5 | 6 | from ratelimit import limits, sleep_and_retry |
6 | 7 | from slack_sdk import WebClient, WebhookClient |
7 | 8 | from slack_sdk.errors import SlackApiError |
@@ -41,16 +42,21 @@ def create_client( |
41 | 42 | return SlackWebClient(token=config.slack_token, tracking=tracking) |
42 | 43 | elif config.slack_webhook: |
43 | 44 | logger.debug("Creating Slack client with webhook.") |
44 | | - return SlackWebhookClient(webhook=config.slack_webhook, tracking=tracking) |
| 45 | + return SlackWebhookClient( |
| 46 | + webhook=config.slack_webhook, |
| 47 | + is_workflow=config.is_slack_workflow, |
| 48 | + tracking=tracking, |
| 49 | + ) |
45 | 50 | return None |
46 | 51 |
|
47 | 52 | @abstractmethod |
48 | 53 | def _initial_client(self): |
49 | 54 | raise NotImplementedError |
50 | 55 |
|
51 | 56 | def _initial_retry_handlers(self): |
52 | | - rate_limit_handler = RateLimitErrorRetryHandler(max_retry_count=5) |
53 | | - self.client.retry_handlers.append(rate_limit_handler) |
| 57 | + if isinstance(self.client, WebClient): |
| 58 | + rate_limit_handler = RateLimitErrorRetryHandler(max_retry_count=5) |
| 59 | + self.client.retry_handlers.append(rate_limit_handler) |
54 | 60 |
|
55 | 61 | @abstractmethod |
56 | 62 | def send_message(self, **kwargs): |
@@ -223,28 +229,43 @@ class SlackWebhookClient(SlackClient): |
223 | 229 | def __init__( |
224 | 230 | self, |
225 | 231 | webhook: str, |
| 232 | + is_workflow: bool, |
226 | 233 | tracking: Optional[Tracking] = None, |
227 | 234 | ): |
228 | 235 | self.webhook = webhook |
| 236 | + self.is_workflow = is_workflow |
229 | 237 | super().__init__(tracking) |
230 | 238 |
|
231 | 239 | def _initial_client(self): |
| 240 | + if self.is_workflow: |
| 241 | + return requests.Session() |
232 | 242 | return WebhookClient( |
233 | 243 | url=self.webhook, default_headers={"Content-type": "application/json"} |
234 | 244 | ) |
235 | 245 |
|
236 | 246 | @sleep_and_retry |
237 | 247 | @limits(calls=1, period=ONE_SECOND) |
238 | 248 | def send_message(self, message: SlackMessageSchema, **kwargs) -> bool: |
239 | | - response: WebhookResponse = self.client.send( |
240 | | - text=message.text, blocks=message.blocks, attachments=message.attachments |
241 | | - ) |
| 249 | + response: Union[requests.Response, WebhookResponse] |
| 250 | + if self.is_workflow: |
| 251 | + # For slack workflows, we need to send the message raw to the webhook |
| 252 | + response = self.client.post(self.webhook, data=message.text) |
| 253 | + else: |
| 254 | + response = self.client.send( |
| 255 | + text=message.text, |
| 256 | + blocks=message.blocks, |
| 257 | + attachments=message.attachments, |
| 258 | + ) |
242 | 259 | if response.status_code == OK_STATUS_CODE: |
243 | 260 | return True |
244 | | - |
245 | 261 | else: |
| 262 | + response_body = ( |
| 263 | + response.text |
| 264 | + if isinstance(response, requests.Response) |
| 265 | + else response.body |
| 266 | + ) |
246 | 267 | logger.error( |
247 | | - f"Could not post message to slack via webhook - {self.webhook}. Status code: {response.status_code}, Error: {response.body}" |
| 268 | + f"Could not post message to slack via webhook - {self.webhook}. Status code: {response.status_code}, Error: {response_body}" |
248 | 269 | ) |
249 | 270 | return False |
250 | 271 |
|
|
0 commit comments