From 33a0c15b758539ca6db0f4ea867f043a503cbbd4 Mon Sep 17 00:00:00 2001 From: Michael Derynck Date: Thu, 3 Oct 2024 12:45:23 -0600 Subject: [PATCH] Truncate slack block text so it is not rejected by slack API (#5121) # What this PR does Truncates text for slack message to avoid this error: ``` File "/usr/local/lib/python3.12/site-packages/slack_sdk/web/slack_response.py", line 199, in validate raise e.SlackApiError(message=msg, response=self) slack_sdk.errors.SlackApiError: The request to the Slack API failed. (url: https://www.slack.com/api/chat.postMessage) The server responded with: {'ok': False, 'error': 'invalid_blocks', 'errors': ['failed to match all allowed schemas [json-pointer:/blocks/0/text]', 'must be less than 3001 characters [json-pointer:/blocks/0/text/text]'], 'response_metadata': {'messages': ['[ERROR] failed to match all allowed schemas [json-pointer:/blocks/0/text]', '[ERROR] must be less than 3001 characters [json-pointer:/blocks/0/text/text]']}} ``` ## Which issue(s) this PR closes Related to [issue link here] ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes. --- engine/apps/slack/models/slack_message.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/engine/apps/slack/models/slack_message.py b/engine/apps/slack/models/slack_message.py index 1e1c8f7f..50a4c821 100644 --- a/engine/apps/slack/models/slack_message.py +++ b/engine/apps/slack/models/slack_message.py @@ -6,6 +6,7 @@ import uuid from django.db import models from apps.slack.client import SlackClient +from apps.slack.constants import BLOCK_SECTION_TEXT_MAX_SIZE from apps.slack.errors import ( SlackAPIChannelArchivedError, SlackAPIError, @@ -127,6 +128,8 @@ class SlackMessage(models.Model): else: text = "{}\nInviting {} to look at the alert group.".format(alert_group.long_verbose_name, user_verbal) + text = text[:BLOCK_SECTION_TEXT_MAX_SIZE] + blocks = [ { "type": "section",