Skip to content

Commit

Permalink
[Notifications] Fix Slack Notifications With Long Messages (#5489)
Browse files Browse the repository at this point in the history
* fix slack notifications with long messages

* better

* like this

---------

Co-authored-by: quaark <a.melnick@icloud.com>
  • Loading branch information
quaark and quaark committed May 2, 2024
1 parent 370acaf commit f3c0222
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions mlrun/utils/notifications/notification/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ def _generate_slack_data(
event_data: mlrun.common.schemas.Event = None,
) -> dict:
data = {
"blocks": [
{
"type": "header",
"text": {"type": "plain_text", "text": f"[{severity}] {message}"},
},
]
"blocks": self._generate_slack_header_blocks(severity, message),
}
if self.name:
data["blocks"].append(
Expand Down Expand Up @@ -106,6 +101,32 @@ def _generate_slack_data(

return data

def _generate_slack_header_blocks(self, severity: str, message: str):
header_text = block_text = f"[{severity}] {message}"
section_text = None

# Slack doesn't allow headers to be longer than 150 characters
# If there's a comma in the message, split the message at the comma
# Otherwise, split the message at 150 characters
if len(block_text) > 150:
if ", " in block_text and block_text.index(", ") < 149:
header_text = block_text.split(",")[0]
section_text = block_text[len(header_text) + 2 :]
else:
header_text = block_text[:150]
section_text = block_text[150:]
blocks = [
{"type": "header", "text": {"type": "plain_text", "text": header_text}}
]
if section_text:
blocks.append(
{
"type": "section",
"text": self._get_slack_row(section_text),
}
)
return blocks

def _get_alert_fields(
self,
alert: mlrun.common.schemas.AlertConfig,
Expand Down

0 comments on commit f3c0222

Please sign in to comment.