Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
eoinkelly opened this issue Feb 15, 2021 · 0 comments
Open

Comments

@eoinkelly
Copy link
Contributor

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
        }
      }
    ]
  };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant