diff --git a/keep/providers/slack_provider/slack_provider.py b/keep/providers/slack_provider/slack_provider.py index 6368c5834..b571b3539 100644 --- a/keep/providers/slack_provider/slack_provider.py +++ b/keep/providers/slack_provider/slack_provider.py @@ -25,7 +25,7 @@ class SlackProviderAuthConfig: "required": True, "description": "Slack Webhook Url", "validation": "https_url", - "sensitive": True + "sensitive": True, }, ) access_token: str = dataclasses.field( @@ -134,20 +134,21 @@ def _notify( }, ) if not message: - if not blocks: + if not blocks and not attachments: raise ProviderException( "Message is required - see for example https://github.com/keephq/keep/blob/main/examples/workflows/slack_basic.yml#L16" ) - message = blocks[0].get("text") payload = { "channel": channel, - "text": message, - "blocks": ( + } + if message: + payload["text"] = message + if blocks: + payload["blocks"] = ( json.dumps(blocks) if isinstance(blocks, dict) or isinstance(blocks, list) else blocks - ), - } + ) if attachments: payload["attachments"] = ( json.dumps(attachments) @@ -194,9 +195,17 @@ def _notify( method = "chat.postMessage" payload["thread_ts"] = thread_timestamp - response = requests.post( - f"{SlackProvider.SLACK_API}/{method}", data=payload - ) + if payload["attachments"]: + payload["attachments"] = attachments + response = requests.post( + f"{SlackProvider.SLACK_API}/{method}", + data={"payload": json.dumps(payload)}, + headers={"Content-Type": "application/x-www-form-urlencoded"}, + ) + else: + response = requests.post( + f"{SlackProvider.SLACK_API}/{method}", data=payload + ) response_json = response.json() if not response.ok or not response_json.get("ok"): @@ -251,35 +260,7 @@ def _notify( provider_config=config, ) provider.notify( - message="Simple alert showing context with name: John Doe", channel="C04P7QSG692", - blocks=[ - { - "type": "section", - "text": { - "type": "mrkdwn", - "text": "Danny Torrence left the following review for your property:", - }, - }, - { - "type": "section", - "block_id": "section567", - "text": { - "type": "mrkdwn", - "text": " \n :star: \n Doors had too many axe holes, guest in room 237 was far too rowdy, whole place felt stuck in the 1920s.", - }, - "accessory": { - "type": "image", - "image_url": "https://is5-ssl.mzstatic.com/image/thumb/Purple3/v4/d3/72/5c/d3725c8f-c642-5d69-1904-aa36e4297885/source/256x256bb.jpg", - "alt_text": "Haunted hotel image", - }, - }, - { - "type": "section", - "block_id": "section789", - "fields": [{"type": "mrkdwn", "text": "*Average Rating*\n1.0"}], - }, - ], attachments=[ { "fallback": "Plain-text summary of the attachment.", diff --git a/keep/providers/teams_provider/teams_provider.py b/keep/providers/teams_provider/teams_provider.py index 876fb5184..33397f797 100644 --- a/keep/providers/teams_provider/teams_provider.py +++ b/keep/providers/teams_provider/teams_provider.py @@ -24,7 +24,7 @@ class TeamsProviderAuthConfig: "required": True, "description": "Teams Webhook Url", "sensitive": True, - "validation": "https_url" + "validation": "https_url", } ) @@ -75,40 +75,42 @@ def _notify( self.logger.debug("Notifying alert message to Teams") webhook_url = self.authentication_config.webhook_url - if isinstance(sections, str): + if sections and isinstance(sections, str): try: sections = json.loads(sections) - except json.JSONDecodeError as e: + except Exception as e: self.logger.error(f"Failed to decode sections string to JSON: {e}") if attachments and isinstance(attachments, str): try: attachments = json.loads(attachments) - except json.JSONDecodeError as e: + except Exception as e: self.logger.error(f"Failed to decode attachments string to JSON: {e}") if typeCard == "message": # Adaptive Card format - payload = { - "type": "message", - "attachments": attachments - or [ - { - "contentType": "application/vnd.microsoft.card.adaptive", - "contentUrl": None, - "content": { - "$schema": schema, - "type": "AdaptiveCard", - "version": "1.2", - "body": ( - sections - if sections - else [{"type": "TextBlock", "text": message}] - ), - }, - } - ], - } + payload = {"type": "message"} + if attachments: + payload["attachments"] = attachments + else: + payload["attachments"] = ( + [ + { + "contentType": "application/vnd.microsoft.card.adaptive", + "contentUrl": None, + "content": { + "$schema": schema, + "type": "AdaptiveCard", + "version": "1.2", + "body": ( + sections + if sections + else [{"type": "TextBlock", "text": message}] + ), + }, + } + ], + ) else: # Standard MessageCard format payload = { diff --git a/pyproject.toml b/pyproject.toml index 3d6a966d9..24629efdf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "keep" -version = "0.31.0" +version = "0.31.1" description = "Alerting. for developers, by developers." authors = ["Keep Alerting LTD"] packages = [{include = "keep"}]