Skip to content

Generic lambda should truncate long messages to fit slack limit #3

Description

@eoinkelly

Slack has a limit of 3000 chars on a section type. We should truncate long messages to fit because a truncated message is much better than no message and it will signal that the limit exists to the dev.

Rough example:

const buildSlackMsg = (event: unknown): IncomingWebhookSendArguments => {
  let warning = JSON.stringify(event, undefined, 2);

  // The maximum length of a `section.text` field in Slack API is 3000
  // characters (https://api.slack.com/reference/block-kit/blocks#section) Slack
  // Webhooks will HTTP 400 if the message we give it is too long.

  const TRUNCATED_TEXT_REPLACEMENT = '\n\n[MESSAGE TRUNCATED FOR SLACK]';
  const WRAPPER_TEXT_LENGTH = 12; // 6 quoted backticks
  const MAX_WARNING_LENGTH =
    3000 - WRAPPER_TEXT_LENGTH - TRUNCATED_TEXT_REPLACEMENT.length;

  if (warning.length > MAX_WARNING_LENGTH) {
    warning = warning.substring(0, MAX_WARNING_LENGTH);
    warning = warning.concat(TRUNCATED_TEXT_REPLACEMENT);
  }

  const alarmMessage = `\`\`\`${warning}\`\`\``;

  return {
    channel: process.env.SLACK_CHANNEL,
    blocks: [
      {
        type: 'header',
        text: {
          type: 'plain_text',
          text: ':warning: An event requires investigation',
          emoji: true
        }
      },
      {
        type: 'section',
        text: {
          type: 'mrkdwn',
          text: alarmMessage
        }
      }
    ]
  };
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions