From c31e81de06ad9b362775bf011ef56a0ba4fd0056 Mon Sep 17 00:00:00 2001 From: Tal Borenstein Date: Tue, 3 Dec 2024 17:27:19 +0200 Subject: [PATCH 1/4] fix(slack): message is not required with attachments --- .../slack_provider/slack_provider.py | 57 +++++++------------ 1 file changed, 19 insertions(+), 38 deletions(-) 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.", From 1759a649e0d053e4630707a0a32fd902b0d70a8d Mon Sep 17 00:00:00 2001 From: Tal Borenstein Date: Tue, 3 Dec 2024 17:28:03 +0200 Subject: [PATCH 2/4] chore: bump version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"}] From de979fdba7d51542b221fd2ae7515934b3f6dea8 Mon Sep 17 00:00:00 2001 From: Tal Borenstein Date: Tue, 3 Dec 2024 17:30:34 +0200 Subject: [PATCH 3/4] fix: fix --- keep/providers/teams_provider/teams_provider.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/keep/providers/teams_provider/teams_provider.py b/keep/providers/teams_provider/teams_provider.py index 876fb5184..e32553980 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,16 +75,16 @@ 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": From db52be816de030fc081e1d56eedbf8e955b8bd99 Mon Sep 17 00:00:00 2001 From: Tal Borenstein Date: Tue, 3 Dec 2024 17:58:21 +0200 Subject: [PATCH 4/4] fix: fix --- .../teams_provider/teams_provider.py | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/keep/providers/teams_provider/teams_provider.py b/keep/providers/teams_provider/teams_provider.py index e32553980..33397f797 100644 --- a/keep/providers/teams_provider/teams_provider.py +++ b/keep/providers/teams_provider/teams_provider.py @@ -89,26 +89,28 @@ def _notify( 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 = {