From ad6c69d827c106ff375cb475c522fff50ca1f6ad Mon Sep 17 00:00:00 2001 From: Mohd Quamar Tyagi <104281681+Tyagiquamar@users.noreply.github.com> Date: Sun, 25 Oct 2026 19:47:20 +0530 Subject: [PATCH 1/3] fix(api): prevent Slack notification failures when alert message exceeds 3000 chars Fixes #3452 Slack section blocks enforce a strict 3000-character limit on the text field. Inline alert links encode large chartConfig objects in query params, causing the message block text to exceed 3000 chars and fail delivery with WEBHOOK_ERROR. Format Slack section text using formatSlackSectionText, which falls back to the shorter /alerts link if the link causes an overflow, and truncates the body safely if necessary. Signed-off-by: Mohd Quamar Tyagi <347892697+Tyagiquamar@users.noreply.github.com> --- .../transports/__tests__/slack.test.ts | 103 ++++++++++++++++++ .../src/tasks/checkAlerts/transports/slack.ts | 49 ++++++++- 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts diff --git a/packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts b/packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts new file mode 100644 index 0000000000..63e3423fd0 --- /dev/null +++ b/packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts @@ -0,0 +1,103 @@ +import { + formatSlackSectionText, + getFallbackHdxLink, + handleSendSlackWebhook, +} from '@/tasks/checkAlerts/transports/slack'; +import type { Message, WebhookChannel } from '@/tasks/checkAlerts/transports/types'; +import * as slack from '@/utils/slack'; + +jest.mock('@/utils/slack'); +jest.mock('@/utils/validators'); + +describe('Slack transport', () => { + describe('getFallbackHdxLink', () => { + it('returns origin + /alerts for valid URLs', () => { + expect( + getFallbackHdxLink('https://app.hyperdx.io/chart?config=%7B%22very%22%3A%22long%22%7D'), + ).toBe('https://app.hyperdx.io/alerts'); + }); + + it('returns original input if not a valid URL', () => { + expect(getFallbackHdxLink('not-a-url')).toBe('not-a-url'); + }); + }); + + describe('formatSlackSectionText', () => { + it('keeps standard link and body when under character limit', () => { + const link = 'https://app.hyperdx.io/alerts/123'; + const title = 'High Error Rate'; + const body = 'Error rate exceeded 5%'; + expect(formatSlackSectionText(link, title, body)).toBe( + `*<${link} | ${title}>*\n${body}`, + ); + }); + + it('falls back to /alerts when large query string exceeds limit', () => { + const longQuery = 'x'.repeat(3100); + const longLink = `https://app.hyperdx.io/chart?config=${longQuery}`; + const title = 'High Latency'; + const body = 'p99 latency > 500ms'; + const formatted = formatSlackSectionText(longLink, title, body, 3000); + + expect(formatted).toBe(`**\n${body}`); + expect(formatted.length).toBeLessThanOrEqual(3000); + }); + + it('truncates body when text still exceeds limit after fallback link', () => { + const longQuery = 'x'.repeat(2900); + const longLink = `https://app.hyperdx.io/chart?config=${longQuery}`; + const title = 'High Latency'; + const longBody = 'b'.repeat(3500); + const formatted = formatSlackSectionText(longLink, title, longBody, 3000); + + expect(formatted.startsWith(`**\n`)).toBe(true); + expect(formatted.endsWith('...')).toBe(true); + expect(formatted.length).toBeLessThanOrEqual(3000); + }); + }); + + describe('handleSendSlackWebhook', () => { + it('passes formatted section block text to postMessageToWebhook', async () => { + const mockPostMessage = jest.spyOn(slack, 'postMessageToWebhook').mockResolvedValue({} as any); + + const channel: WebhookChannel = { + type: 'webhook', + channel: { + _id: 'wh-1' as any, + name: 'Slack Alerts', + service: 'slack' as any, + url: 'https://hooks.slack.com/services/T00/B00/X00', + } as any, + }; + + const message: Message = { + hdxLink: 'https://app.hyperdx.io/chart?config=test', + title: 'CPU Alert', + body: 'CPU > 90%', + state: 'ALERT' as any, + startTime: 0, + endTime: 1000, + eventId: 'evt-1', + }; + + await handleSendSlackWebhook(channel, message); + + expect(mockPostMessage).toHaveBeenCalledTimes(1); + expect(mockPostMessage).toHaveBeenCalledWith( + 'https://hooks.slack.com/services/T00/B00/X00', + { + text: 'CPU Alert', + blocks: [ + { + type: 'section', + text: { + type: 'mrkdwn', + text: '**\nCPU > 90%', + }, + }, + ], + }, + ); + }); + }); +}); diff --git a/packages/api/src/tasks/checkAlerts/transports/slack.ts b/packages/api/src/tasks/checkAlerts/transports/slack.ts index d1f069ade6..cacef85d69 100644 --- a/packages/api/src/tasks/checkAlerts/transports/slack.ts +++ b/packages/api/src/tasks/checkAlerts/transports/slack.ts @@ -13,6 +13,49 @@ import type { import * as slack from '@/utils/slack'; import { validateWebhookUrl } from '@/utils/validators'; +const MAX_SLACK_SECTION_LENGTH = 3000; + +export const getFallbackHdxLink = (hdxLink: string): string => { + try { + const url = new URL(hdxLink); + return `${url.origin}/alerts`; + } catch { + return hdxLink; + } +}; + +export const formatSlackSectionText = ( + hdxLink: string, + title: string, + body: string, + maxLength = MAX_SLACK_SECTION_LENGTH, +): string => { + let link = hdxLink; + let text = `*<${link} | ${title}>*\n${body}`; + if (text.length <= maxLength) { + return text; + } + + // If text exceeds limit, try falling back to the shorter /alerts link + const fallbackLink = getFallbackHdxLink(hdxLink); + if (fallbackLink !== link) { + text = `*<${fallbackLink} | ${title}>*\n${body}`; + if (text.length <= maxLength) { + return text; + } + link = fallbackLink; + } + + // If still exceeding limit, truncate body with an ellipsis + const header = `*<${link} | ${title}>*\n`; + const availableBodyLen = maxLength - header.length - 3; + if (availableBodyLen > 0) { + return `${header}${body.slice(0, availableBodyLen)}...`; + } + + return `*<${link} | ${title}>*`.slice(0, maxLength); +}; + export const handleSendSlackWebhook = async ( channel: WebhookChannel, message: Message, @@ -29,7 +72,11 @@ export const handleSendSlackWebhook = async ( type: 'section', text: { type: 'mrkdwn', - text: `*<${message.hdxLink} | ${message.title}>*\n${message.body}`, + text: formatSlackSectionText( + message.hdxLink, + message.title, + message.body, + ), }, }, ], From f40bf101877dacf18aa45539c2ba7d298b7d7f51 Mon Sep 17 00:00:00 2001 From: Mohd Quamar Tyagi <104281681+Tyagiquamar@users.noreply.github.com> Date: Thu, 5 Nov 2026 14:56:58 +0530 Subject: [PATCH 2/3] chore: add changeset for slack block limit fix Signed-off-by: Mohd Quamar Tyagi <104281681+Tyagiquamar@users.noreply.github.com> --- .changeset/slack-block-limit.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/slack-block-limit.md diff --git a/.changeset/slack-block-limit.md b/.changeset/slack-block-limit.md new file mode 100644 index 0000000000..18de699cc1 --- /dev/null +++ b/.changeset/slack-block-limit.md @@ -0,0 +1,5 @@ +--- +"@hyperdx/api": patch +--- + +fix(api): prevent Slack notification failures when alert message exceeds 3000 chars From ea3846b5768139621ec10a2690c29a5d67b9defb Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Tue, 1 Dec 2026 14:09:26 -0500 Subject: [PATCH 3/3] fix: Link inline alerts to alert details; fix code and emoji splitting --- .../__tests__/checkAlerts.int.test.ts | 22 ++-- .../__tests__/checkAlertsTask.test.ts | 2 +- .../__tests__/renderAlertTemplate.int.test.ts | 1 + .../tasks/checkAlerts/providers/default.ts | 24 ++--- .../src/tasks/checkAlerts/providers/index.ts | 7 +- .../api/src/tasks/checkAlerts/template.ts | 10 +- .../transports/__tests__/slack.test.ts | 102 +++++++++++------- .../src/tasks/checkAlerts/transports/slack.ts | 54 +++++----- 8 files changed, 107 insertions(+), 115 deletions(-) diff --git a/packages/api/src/tasks/checkAlerts/__tests__/checkAlerts.int.test.ts b/packages/api/src/tasks/checkAlerts/__tests__/checkAlerts.int.test.ts index 59f69cb107..692585343f 100644 --- a/packages/api/src/tasks/checkAlerts/__tests__/checkAlerts.int.test.ts +++ b/packages/api/src/tasks/checkAlerts/__tests__/checkAlerts.int.test.ts @@ -1336,6 +1336,7 @@ describe('checkAlerts', () => { }); const defaultInlineAlertView: AlertMessageTemplateDefaultView = { alert: { + id: 'fake-inline-alert-id', thresholdType: AlertThresholdType.ABOVE, threshold: 1, source: AlertSource.INLINE, @@ -1387,18 +1388,11 @@ describe('checkAlerts', () => { `"http://app:8080/dashboards/id-123?from=1679089083103&granularity=5+minute&to=1679093339103&highlightedTileId=test-tile-id"`, ); - // Inline alerts link to the chart explorer seeded with the persisted - // config. Built programmatically — the URL-encoded config JSON makes an - // inline snapshot unreadable. - const expectedChartAlertUrl = new URL('http://app:8080/chart'); - expectedChartAlertUrl.search = new URLSearchParams({ - config: JSON.stringify(inlineAlertConfig), - from: '1679089083103', - to: '1679093339103', - }).toString(); expect( buildAlertMessageTemplateHdxLink(alertProvider, defaultInlineAlertView), - ).toBe(expectedChartAlertUrl.toString()); + ).toMatchInlineSnapshot( + `"http://app:8080/alerts/fake-inline-alert-id?from=1679089083103&to=1679093339103"`, + ); }); it('formatValueToMatchThreshold', () => { @@ -3031,13 +3025,9 @@ describe('checkAlerts', () => { expect(history2.state).toBe('OK'); expect(history2.createdAt).toEqual(new Date('2023-11-16T22:15:00.000Z')); - // Notification links to the chart explorer seeded with the persisted - // config, padded by 7x granularity on both sides of the window. - const expectedUrl = new URL('http://app:8080/chart'); + // Padded by 7x granularity on both sides of the window. + const expectedUrl = new URL(`http://app:8080/alerts/${details.alert.id}`); expectedUrl.search = new URLSearchParams({ - config: JSON.stringify( - (await Alert.findById(details.alert.id))!.chartConfig, - ), from: String( new Date('2023-11-16T22:05:00.000Z').getTime() - ms('5m') * 7, ), diff --git a/packages/api/src/tasks/checkAlerts/__tests__/checkAlertsTask.test.ts b/packages/api/src/tasks/checkAlerts/__tests__/checkAlertsTask.test.ts index 4bee904862..5d4389a23b 100644 --- a/packages/api/src/tasks/checkAlerts/__tests__/checkAlertsTask.test.ts +++ b/packages/api/src/tasks/checkAlerts/__tests__/checkAlertsTask.test.ts @@ -173,7 +173,7 @@ describe('CheckAlertTask', () => { recordAlertErrors: jest.fn(), asyncDispose: jest.fn(), buildChartLink: jest.fn(), - buildChartExplorerLink: jest.fn(), + buildAlertDetailsLink: jest.fn(), buildLogSearchLink: jest.fn(), getClickHouseClient: jest .fn() diff --git a/packages/api/src/tasks/checkAlerts/__tests__/renderAlertTemplate.int.test.ts b/packages/api/src/tasks/checkAlerts/__tests__/renderAlertTemplate.int.test.ts index 266af03eee..2697b4dc5f 100644 --- a/packages/api/src/tasks/checkAlerts/__tests__/renderAlertTemplate.int.test.ts +++ b/packages/api/src/tasks/checkAlerts/__tests__/renderAlertTemplate.int.test.ts @@ -198,6 +198,7 @@ const makeInlineView = ( overrides: ViewOverrides = {}, ): AlertMessageTemplateDefaultView => ({ alert: { + id: 'fake-inline-alert-id', thresholdType: overrides.thresholdType ?? AlertThresholdType.ABOVE, threshold: overrides.threshold ?? 5, thresholdMax: overrides.thresholdMax, diff --git a/packages/api/src/tasks/checkAlerts/providers/default.ts b/packages/api/src/tasks/checkAlerts/providers/default.ts index 892a6f1a96..5399d5f516 100644 --- a/packages/api/src/tasks/checkAlerts/providers/default.ts +++ b/packages/api/src/tasks/checkAlerts/providers/default.ts @@ -1,11 +1,7 @@ import PQueue from '@esm2cjs/p-queue'; import { displayTypeSupportsRawSqlAlerts } from '@hyperdx/common-utils/dist/core/utils'; import { isRawSqlSavedChartConfig } from '@hyperdx/common-utils/dist/guards'; -import { - AlertChartConfig, - RawSqlSavedChartConfig, - Tile, -} from '@hyperdx/common-utils/dist/types'; +import { RawSqlSavedChartConfig, Tile } from '@hyperdx/common-utils/dist/types'; import mongoose from 'mongoose'; import ms from 'ms'; import { serializeError } from 'serialize-error'; @@ -643,29 +639,23 @@ export default class DefaultAlertProvider implements AlertProvider { return url.toString(); } - buildChartExplorerLink({ - chartConfig, + buildAlertDetailsLink({ + alertId, endTime, granularity, startTime, }: { - chartConfig: AlertChartConfig; + alertId: string; endTime: Date; granularity: string; startTime: Date; }): string { - const url = new URL(`${config.FRONTEND_URL}/chart`); + const url = new URL(`${config.FRONTEND_URL}/alerts/${alertId}`); // Extend both start and end time by 7x granularity, matching the - // dashboard link so the alerting window has surrounding context. The - // chart explorer reads `config` (JSON) and `from`/`to` (epoch ms). + // dashboard link so the alerting window has surrounding context. const from = (startTime.getTime() - ms(granularity) * 7).toString(); const to = (endTime.getTime() + ms(granularity) * 7).toString(); - const queryParams = new URLSearchParams({ - config: JSON.stringify(chartConfig), - from, - to, - }); - url.search = queryParams.toString(); + url.search = new URLSearchParams({ from, to }).toString(); return url.toString(); } diff --git a/packages/api/src/tasks/checkAlerts/providers/index.ts b/packages/api/src/tasks/checkAlerts/providers/index.ts index 96071696ed..67a4ffe231 100644 --- a/packages/api/src/tasks/checkAlerts/providers/index.ts +++ b/packages/api/src/tasks/checkAlerts/providers/index.ts @@ -90,11 +90,10 @@ export interface AlertProvider { /** * Link for inline alerts (which have no saved search or dashboard to open): - * the chart explorer with the alert's persisted config over the alerting - * window. Works regardless of whether the alert-details page is enabled. + * the alert's details page over the alerting window. */ - buildChartExplorerLink(params: { - chartConfig: AlertChartConfig; + buildAlertDetailsLink(params: { + alertId: string; endTime: Date; granularity: string; startTime: Date; diff --git a/packages/api/src/tasks/checkAlerts/template.ts b/packages/api/src/tasks/checkAlerts/template.ts index fde25921b9..dd7af20c9a 100644 --- a/packages/api/src/tasks/checkAlerts/template.ts +++ b/packages/api/src/tasks/checkAlerts/template.ts @@ -327,13 +327,11 @@ export const buildAlertMessageTemplateHdxLink = ( tileId: alert.tileId ?? undefined, }); } else if (alert.source === AlertSource.INLINE) { - if (alert.chartConfig == null) { - throw new Error(`Source is ${alert.source} but chartConfig is null`); + if (alert.id == null) { + throw new Error(`Source is ${alert.source} but alert id is null`); } - // Inline alerts have no saved search or dashboard to open — link to the - // chart explorer seeded with the alert's persisted config. - return alertProvider.buildChartExplorerLink({ - chartConfig: alert.chartConfig, + return alertProvider.buildAlertDetailsLink({ + alertId: alert.id, endTime, granularity, startTime, diff --git a/packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts b/packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts index 63e3423fd0..dbd18450ae 100644 --- a/packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts +++ b/packages/api/src/tasks/checkAlerts/transports/__tests__/slack.test.ts @@ -1,80 +1,100 @@ +import { WebhookService } from '@hyperdx/common-utils/dist/types'; +import { fromPartial } from '@total-typescript/shoehorn'; +import { ObjectId } from 'mongodb'; + +import { AlertState } from '@/models/alert'; import { formatSlackSectionText, - getFallbackHdxLink, handleSendSlackWebhook, } from '@/tasks/checkAlerts/transports/slack'; -import type { Message, WebhookChannel } from '@/tasks/checkAlerts/transports/types'; +import type { + Message, + WebhookChannel, +} from '@/tasks/checkAlerts/transports/types'; import * as slack from '@/utils/slack'; jest.mock('@/utils/slack'); jest.mock('@/utils/validators'); describe('Slack transport', () => { - describe('getFallbackHdxLink', () => { - it('returns origin + /alerts for valid URLs', () => { - expect( - getFallbackHdxLink('https://app.hyperdx.io/chart?config=%7B%22very%22%3A%22long%22%7D'), - ).toBe('https://app.hyperdx.io/alerts'); - }); + describe('formatSlackSectionText', () => { + const link = 'https://app.hyperdx.io/alerts/123'; + const title = 'High Error Rate'; + const header = `*<${link} | ${title}>*\n`; - it('returns original input if not a valid URL', () => { - expect(getFallbackHdxLink('not-a-url')).toBe('not-a-url'); + it('keeps the body when the text fits', () => { + const body = 'Error rate exceeded 5%'; + expect(formatSlackSectionText(link, title, body)).toBe( + `${header}${body}`, + ); }); - }); - describe('formatSlackSectionText', () => { - it('keeps standard link and body when under character limit', () => { - const link = 'https://app.hyperdx.io/alerts/123'; - const title = 'High Error Rate'; - const body = 'Error rate exceeded 5%'; + it('keeps a body that exactly fills the limit', () => { + const body = 'b'.repeat(3000 - header.length); expect(formatSlackSectionText(link, title, body)).toBe( - `*<${link} | ${title}>*\n${body}`, + `${header}${body}`, ); }); - it('falls back to /alerts when large query string exceeds limit', () => { - const longQuery = 'x'.repeat(3100); - const longLink = `https://app.hyperdx.io/chart?config=${longQuery}`; - const title = 'High Latency'; - const body = 'p99 latency > 500ms'; - const formatted = formatSlackSectionText(longLink, title, body, 3000); + it('truncates the body to fit the limit, keeping the link', () => { + const formatted = formatSlackSectionText(link, title, 'b'.repeat(3500)); - expect(formatted).toBe(`**\n${body}`); + expect(formatted).toBe( + `${header}${'b'.repeat(3000 - header.length - 7)}...`, + ); expect(formatted.length).toBeLessThanOrEqual(3000); }); - it('truncates body when text still exceeds limit after fallback link', () => { - const longQuery = 'x'.repeat(2900); - const longLink = `https://app.hyperdx.io/chart?config=${longQuery}`; - const title = 'High Latency'; - const longBody = 'b'.repeat(3500); - const formatted = formatSlackSectionText(longLink, title, longBody, 3000); + it('re-closes a code block the cut lands inside', () => { + const body = `5 lines found\n\`\`\`\n${'log line\n'.repeat(400)}\`\`\``; + const formatted = formatSlackSectionText(link, title, body); - expect(formatted.startsWith(`**\n`)).toBe(true); - expect(formatted.endsWith('...')).toBe(true); + expect(formatted.endsWith('...\n```')).toBe(true); + expect(formatted.split('```')).toHaveLength(3); expect(formatted.length).toBeLessThanOrEqual(3000); }); + + it('does not add a fence when the cut lands after a closed code block', () => { + const body = `\`\`\`\nsample\n\`\`\`\n${'m'.repeat(3500)}`; + const formatted = formatSlackSectionText(link, title, body); + + expect(formatted.endsWith('m...')).toBe(true); + expect(formatted.split('```')).toHaveLength(3); + }); + + it('does not split an emoji at the cut', () => { + const prefix = 'b'.repeat(3000 - header.length - 7 - 1); + const formatted = formatSlackSectionText( + link, + title, + `${prefix}🚨${'b'.repeat(100)}`, + ); + + expect(formatted).toBe(`${header}${prefix}...`); + }); }); describe('handleSendSlackWebhook', () => { it('passes formatted section block text to postMessageToWebhook', async () => { - const mockPostMessage = jest.spyOn(slack, 'postMessageToWebhook').mockResolvedValue({} as any); + const mockPostMessage = jest + .spyOn(slack, 'postMessageToWebhook') + .mockResolvedValue({ text: 'ok' }); - const channel: WebhookChannel = { + const channel = fromPartial({ type: 'webhook', channel: { - _id: 'wh-1' as any, + _id: new ObjectId(), name: 'Slack Alerts', - service: 'slack' as any, + service: WebhookService.Slack, url: 'https://hooks.slack.com/services/T00/B00/X00', - } as any, - }; + }, + }); const message: Message = { - hdxLink: 'https://app.hyperdx.io/chart?config=test', + hdxLink: 'https://app.hyperdx.io/alerts/123', title: 'CPU Alert', body: 'CPU > 90%', - state: 'ALERT' as any, + state: AlertState.ALERT, startTime: 0, endTime: 1000, eventId: 'evt-1', @@ -92,7 +112,7 @@ describe('Slack transport', () => { type: 'section', text: { type: 'mrkdwn', - text: '**\nCPU > 90%', + text: '**\nCPU > 90%', }, }, ], diff --git a/packages/api/src/tasks/checkAlerts/transports/slack.ts b/packages/api/src/tasks/checkAlerts/transports/slack.ts index cacef85d69..17c2de0d86 100644 --- a/packages/api/src/tasks/checkAlerts/transports/slack.ts +++ b/packages/api/src/tasks/checkAlerts/transports/slack.ts @@ -13,15 +13,31 @@ import type { import * as slack from '@/utils/slack'; import { validateWebhookUrl } from '@/utils/validators'; +// Slack rejects a section block whose text is over 3000 characters. const MAX_SLACK_SECTION_LENGTH = 3000; +const ELLIPSIS = '...'; +const CODE_FENCE = '```'; +const CLOSING_FENCE = `\n${CODE_FENCE}`; -export const getFallbackHdxLink = (hdxLink: string): string => { - try { - const url = new URL(hdxLink); - return `${url.origin}/alerts`; - } catch { - return hdxLink; +const isHighSurrogate = (code: number) => code >= 0xd800 && code <= 0xdbff; + +const truncateBody = (body: string, maxLength: number): string => { + if (body.length <= maxLength) { + return body; + } + // Always reserve room to re-close a code fence, so the result fits either way. + let kept = body.slice( + 0, + Math.max(0, maxLength - ELLIPSIS.length - CLOSING_FENCE.length), + ); + // Don't leave half of a surrogate pair (e.g. an emoji) at the cut. + if (isHighSurrogate(kept.charCodeAt(kept.length - 1))) { + kept = kept.slice(0, -1); } + // Saved-search bodies end with sample lines in a code block; the cut + // usually lands inside it. + const isFenceOpen = kept.split(CODE_FENCE).length % 2 === 0; + return `${kept}${ELLIPSIS}${isFenceOpen ? CLOSING_FENCE : ''}`; }; export const formatSlackSectionText = ( @@ -30,30 +46,8 @@ export const formatSlackSectionText = ( body: string, maxLength = MAX_SLACK_SECTION_LENGTH, ): string => { - let link = hdxLink; - let text = `*<${link} | ${title}>*\n${body}`; - if (text.length <= maxLength) { - return text; - } - - // If text exceeds limit, try falling back to the shorter /alerts link - const fallbackLink = getFallbackHdxLink(hdxLink); - if (fallbackLink !== link) { - text = `*<${fallbackLink} | ${title}>*\n${body}`; - if (text.length <= maxLength) { - return text; - } - link = fallbackLink; - } - - // If still exceeding limit, truncate body with an ellipsis - const header = `*<${link} | ${title}>*\n`; - const availableBodyLen = maxLength - header.length - 3; - if (availableBodyLen > 0) { - return `${header}${body.slice(0, availableBodyLen)}...`; - } - - return `*<${link} | ${title}>*`.slice(0, maxLength); + const header = `*<${hdxLink} | ${title}>*\n`; + return `${header}${truncateBody(body, Math.max(0, maxLength - header.length))}`; }; export const handleSendSlackWebhook = async (