Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 1x 1x 10x 11x 10x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 1x 1x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 5x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 5x 5x 5x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 85x 85x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 7x 7x 7x 7x 7x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 6x 6x 6x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x | import {
SlackConfig,
SlackMessage,
SlackResponse,
SlackNotificationOptions,
SlackWebhookConfig,
SlackWebhookMessage,
SlackBlock,
SlackElement,
SlackField,
SlackAttachment,
SlackAttachmentField,
} from './types'
const DEFAULT_RETRY_CONFIG = {
maxAttempts: 3,
delayMs: 1000,
}
/**
* Provides user-friendly error messages for common Slack API errors.
*
* @param {string | undefined} error - The error code from Slack API.
* @returns {string} A user-friendly error message.
*/
function getSlackErrorMessage(error: string | undefined): string {
if (!error) return 'Unknown error from Slack API'
const errorMessages: Record<string, string> = {
not_in_channel:
'Bot is not in the channel. Add the bot to the channel or use a public channel.',
channel_not_found:
'Channel not found. Verify that the channel name is correct.',
invalid_auth:
'Invalid authentication token. Verify that the token is correct.',
account_inactive: 'Bot account is inactive. Check the bot settings.',
token_revoked: 'Token has been revoked. Generate a new token.',
no_permission:
'Bot does not have permission to send messages to this channel.',
is_archived: 'Channel is archived and cannot receive messages.',
msg_too_long: 'Message too long. Reduce the message size.',
rate_limited: 'Rate limit exceeded. Try again in a few seconds.',
fatal_error: 'Fatal Slack error. Try again later.',
}
return errorMessages[error] || `Slack error: ${error}`
}
/**
* Checks if the bot is a member of the specified channel.
*
* @param {string} channel - The channel name or ID.
* @param {SlackConfig} config - The Slack configuration.
* @returns {Promise<boolean>} True if the bot is in the channel, false otherwise.
*/
async function isBotInChannel(
channel: string,
config: SlackConfig
): Promise<boolean> {
try {
const baseUrl = config.baseUrl || 'https://slack.com/api'
const response = await fetch(`${baseUrl}/conversations.info`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${config.token}`,
},
body: JSON.stringify({ channel }),
})
if (!response.ok) {
console.warn(
`Failed to check channel membership for ${channel}: HTTP ${response.status}`
)
return false
}
const data = (await response.json()) as {
ok: boolean
channel?: { is_member?: boolean }
error?: string
}
if (!data.ok) {
console.warn(`Slack API error checking channel ${channel}: ${data.error}`)
return false
}
const isMember = data.channel?.is_member === true
console.log(`Bot membership check for ${channel}: ${isMember}`)
return isMember
} catch (error) {
console.warn(`Error checking channel membership for ${channel}:`, error)
return false
}
}
/**
* Validates the Slack configuration.
*
* @param {SlackConfig} config - The Slack configuration.
* @returns {void}
* @throws {Error} If the configuration is invalid.
*/
function validateSlackConfig(config: SlackConfig): void {
if (!config.token) {
throw new Error('Slack token is required')
}
if (typeof config.token !== 'string') {
throw new Error('Slack token must be a string')
}
if (!config.token.startsWith('xoxb-')) {
console.warn(
'Slack token does not appear to be a valid Bot User OAuth Token'
)
}
}
/**
* Validates the Slack webhook configuration.
*
* @param {SlackWebhookConfig} config - The webhook configuration.
* @returns {void}
* @throws {Error} If the configuration is invalid.
*/
function validateWebhookConfig(config: SlackWebhookConfig): void {
if (!config.webhookUrl) {
throw new Error('Slack webhook URL is required')
}
if (typeof config.webhookUrl !== 'string') {
throw new Error('Slack webhook URL must be a string')
}
if (!config.webhookUrl.startsWith('https://hooks.slack.com/')) {
throw new Error(
'Slack webhook URL must start with https://hooks.slack.com/'
)
}
}
/**
* Validates a Slack message.
*
* @param {SlackMessage} message - The message to validate.
* @returns {void}
* @throws {Error} If the message is invalid.
*/
function validateSlackMessage(message: SlackMessage): void {
if (!message.channel) {
throw new Error('Channel is required')
}
if (!message.text && !message.blocks && !message.attachments) {
throw new Error('Message must contain text, blocks, or attachments')
}
}
/**
* Waits for a given number of milliseconds.
*
* @param {number} ms - Number of milliseconds to wait.
* @returns {Promise<void>} Promise that resolves after the delay.
*/
async function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
/**
* Executes an operation with retry and exponential backoff.
*
* @param {() => Promise<T>} operation - The operation to execute.
* @param {object} retryConfig - Retry configuration.
* @returns {Promise<T>} Result of the operation.
*/
async function retryWithBackoff<T>(
operation: () => Promise<T>,
retryConfig = DEFAULT_RETRY_CONFIG
): Promise<T> {
let lastError: Error
for (let attempt = 1; attempt <= retryConfig.maxAttempts; attempt++) {
try {
return await operation()
} catch (error) {
lastError = error instanceof Error ? error : new Error(String(error))
if (attempt === retryConfig.maxAttempts) {
throw lastError
}
const delayTime = retryConfig.delayMs * Math.pow(2, attempt - 1)
console.warn(
`Attempt ${attempt} failed, retrying in ${delayTime}ms:`,
lastError.message
)
await delay(delayTime)
}
}
throw lastError!
}
/**
* Sends a message to Slack using the official API.
*
* @param {SlackMessage} message - The message to send.
* @param {SlackNotificationOptions} options - Notification options.
* @returns {Promise<SlackResponse>} Slack API response.
*
* @example
* ```typescript
* const response = await sendSlackMessage({
* channel: '#general',
* text: 'Hello, this is a notification!',
* username: 'My Bot'
* }, {
* config: {
* token: 'xoxb-your-bot-token',
* defaultChannel: '#general'
* }
* })
* ```
*/
export async function sendSlackMessage(
message: SlackMessage,
options: SlackNotificationOptions
): Promise<SlackResponse> {
validateSlackConfig(options.config)
validateSlackMessage(message)
if (options.checkChannelMembership === true) {
const isInChannel = await isBotInChannel(message.channel, options.config)
if (!isInChannel) {
const errorMessage = `Bot is not in the channel ${message.channel}. Add the bot to the channel or use a public channel.`
console.warn(errorMessage)
if (!options.failSilently) {
throw new Error(errorMessage)
}
return {
ok: false,
error: errorMessage,
}
}
}
const retryConfig = options.retryConfig || DEFAULT_RETRY_CONFIG
const baseUrl = options.config.baseUrl || 'https://slack.com/api'
const payload = {
channel: message.channel,
text: message.text,
...(message.blocks && { blocks: message.blocks }),
...(message.attachments && { attachments: message.attachments }),
...(message.username && { username: message.username }),
...(message.icon_emoji && { icon_emoji: message.icon_emoji }),
...(message.icon_url && { icon_url: message.icon_url }),
...(message.thread_ts && { thread_ts: message.thread_ts }),
...(message.replace_original && {
replace_original: message.replace_original,
}),
...(message.delete_original && {
delete_original: message.delete_original,
}),
}
try {
const response = await retryWithBackoff(async () => {
const res = await fetch(`${baseUrl}/chat.postMessage`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${options.config.token}`,
},
body: JSON.stringify(payload),
})
if (!res.ok) {
throw new Error(`HTTP ${res.status}: ${res.statusText}`)
}
const data = (await res.json()) as SlackResponse
if (!data.ok) {
const errorMessage = getSlackErrorMessage(data.error)
throw new Error(errorMessage)
}
return data
}, retryConfig)
console.log(`Message sent successfully to channel ${message.channel}`)
return response
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : 'Unknown error'
console.error(`Error sending message to Slack: ${errorMessage}`)
if (!options.failSilently) {
throw error
}
return {
ok: false,
error: errorMessage,
}
}
}
/**
* Sends a message to Slack using a webhook.
*
* @param {SlackWebhookMessage} message - The message to send.
* @param {SlackWebhookConfig} config - Webhook configuration.
* @param {object} options - Additional options.
* @returns {Promise<SlackResponse>} Webhook response.
*
* @example
* ```typescript
* const response = await sendSlackWebhook({
* text: 'Hello, this is a notification via webhook!',
* channel: '#general'
* }, {
* webhookUrl: 'https://hooks.slack.com/services/...',
* botName: 'My Bot'
* })
* ```
*/
export async function sendSlackWebhook(
message: SlackWebhookMessage,
config: SlackWebhookConfig,
options: {
failSilently?: boolean
retryConfig?: { maxAttempts: number; delayMs: number }
} = {}
): Promise<SlackResponse> {
validateWebhookConfig(config)
if (!message.text && !message.blocks && !message.attachments) {
throw new Error('Message must contain text, blocks, or attachments')
}
const retryConfig = options.retryConfig || DEFAULT_RETRY_CONFIG
const payload = {
text: message.text,
...(message.channel && { channel: message.channel }),
...(message.username && { username: message.username }),
...(message.icon_emoji && { icon_emoji: message.icon_emoji }),
...(message.icon_url && { icon_url: message.icon_url }),
...(message.attachments && { attachments: message.attachments }),
...(message.blocks && { blocks: message.blocks }),
}
try {
const response = await retryWithBackoff(async () => {
const res = await fetch(config.webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
if (!res.ok) {
throw new Error(`HTTP ${res.status}: ${res.statusText}`)
}
const responseText = await res.text()
if (responseText !== 'ok') {
throw new Error(`Webhook returned: ${responseText}`)
}
return {
ok: true,
channel: message.channel || config.defaultChannel || '#general',
}
}, retryConfig)
console.log(
`Message sent successfully via webhook to channel ${message.channel || config.defaultChannel}`
)
return response
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : 'Unknown error'
console.error(`Error sending message via Slack webhook: ${errorMessage}`)
if (!options.failSilently) {
throw error
}
return {
ok: false,
error: errorMessage,
}
}
}
/**
* Removes control characters from text.
*
* @param {string} text - Text to clean.
* @returns {string} Cleaned text.
*/
function removeControlCharacters(text: string): string {
return text
.split('')
.filter((char) => {
const code = char.charCodeAt(0)
return code >= 32 && code !== 127
})
.join('')
}
/**
* Validates and sanitizes text for Slack blocks.
*
* @param {string} text - Text to validate.
* @param {string} type - Text type ('plain_text' or 'mrkdwn').
* @returns {string} Sanitized text.
*/
function validateAndSanitizeText(
text: string,
type: 'plain_text' | 'mrkdwn'
): string {
if (!text || text.trim() === '') {
return type === 'mrkdwn' ? ' ' : ' '
}
// Remove or replace problematic characters
let sanitized = removeControlCharacters(text)
.replace(/\uFEFF/g, '')
.trim()
// For mrkdwn, ensure proper escaping
if (type === 'mrkdwn') {
// Escape special characters that could break mrkdwn
sanitized = sanitized
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
}
// Truncate if too long (Slack limit is 3000 characters for section text)
const maxLength = 3000
if (sanitized.length > maxLength) {
sanitized = sanitized.substring(0, maxLength - 3) + '...'
}
return sanitized || ' '
}
/**
* Validates a Slack block structure.
*
* @param {SlackBlock} block - Block to validate.
* @returns {boolean} True if valid, false otherwise.
*/
function validateSlackBlock(block: SlackBlock): boolean {
if (!block || typeof block !== 'object') {
return false
}
if (!block.type || typeof block.type !== 'string') {
return false
}
// For section blocks, validate text
if (block.type === 'section' && block.text) {
if (!block.text.type || !block.text.text) {
return false
}
const textLength = block.text.text.length
if (textLength === 0 || textLength > 3000) {
return false
}
}
return true
}
/**
* Creates a section block for formatted Slack messages.
*
* @param {string} text - Block text.
* @param {object} options - Additional options.
* @returns {SlackBlock} Section block.
*
* @example
* ```typescript
* const block = createSectionBlock('*Bold text*', { type: 'mrkdwn' })
* ```
*/
export function createSectionBlock(
text: string,
options: { type?: 'plain_text' | 'mrkdwn'; fields?: SlackField[] } = {}
): SlackBlock {
const textType = options.type || 'mrkdwn'
const sanitizedText = validateAndSanitizeText(text, textType)
return {
type: 'section',
text: {
type: textType,
text: sanitizedText,
},
...(options.fields && { fields: options.fields }),
}
}
/**
* Creates a divider block for visual separation.
*
* @returns {SlackBlock} Divider block.
*
* @example
* ```typescript
* const divider = createDividerBlock()
* ```
*/
export function createDividerBlock(): SlackBlock {
return {
type: 'divider',
}
}
/**
* Creates a button block for interactive Slack messages.
*
* @param {string} text - Button text.
* @param {string} value - Button value.
* @param {object} options - Additional options.
* @returns {SlackElement} Button element.
*
* @example
* ```typescript
* const button = createButtonElement('Click here', 'button_clicked', { style: 'primary' })
* ```
*/
export function createButtonElement(
text: string,
value: string,
options: {
style?: 'primary' | 'danger'
url?: string
action_id?: string
} = {}
): SlackElement {
return {
type: 'button',
text: {
type: 'plain_text',
text,
},
value,
...(options.style && { style: options.style }),
...(options.url && { url: options.url }),
...(options.action_id && { action_id: options.action_id }),
}
}
/**
* Creates an actions block with buttons.
*
* @param {SlackElement[]} elements - Block elements.
* @param {string} blockId - Block ID (optional).
* @returns {SlackBlock} Actions block.
*
* @example
* ```typescript
* const actionBlock = createActionBlock([
* createButtonElement('Approve', 'approve', { style: 'primary' }),
* createButtonElement('Reject', 'reject', { style: 'danger' })
* ])
* ```
*/
export function createActionBlock(
elements: SlackElement[],
blockId?: string
): SlackBlock {
return {
type: 'actions',
elements,
...(blockId && { block_id: blockId }),
}
}
/**
* Creates an attachment for Slack messages.
*
* @param {object} options - Attachment options.
* @returns {SlackAttachment} Slack attachment.
*
* @example
* ```typescript
* const attachment = createSlackAttachment({
* color: 'good',
* title: 'Attachment title',
* text: 'Attachment text',
* fields: [
* { title: 'Field 1', value: 'Value 1', short: true },
* { title: 'Field 2', value: 'Value 2', short: true }
* ]
* })
* ```
*/
export function createSlackAttachment(options: {
color?: string
title?: string
title_link?: string
text?: string
fields?: SlackAttachmentField[]
image_url?: string
thumb_url?: string
ts?: number
}): SlackAttachment {
return {
...options,
}
}
/**
* Class to conveniently manage Slack notifications.
*/
export class SlackNotifier {
private config: SlackConfig
private retryConfig: { maxAttempts: number; delayMs: number }
private failSilently: boolean
constructor(
config: SlackConfig,
options: {
retryConfig?: { maxAttempts: number; delayMs: number }
failSilently?: boolean
} = {}
) {
validateSlackConfig(config)
this.config = config
this.retryConfig = options.retryConfig || DEFAULT_RETRY_CONFIG
this.failSilently = options.failSilently || false
}
/**
* Sends a simple message.
*
* @param {string} text - Message text.
* @param {string} channel - Channel (optional, uses default if not specified).
* @returns {Promise<SlackResponse>} API response.
*/
async sendMessage(text: string, channel?: string): Promise<SlackResponse> {
return sendSlackMessage(
{
channel: channel || this.config.defaultChannel || '#general',
text,
...(this.config.botName && { username: this.config.botName }),
},
{
config: this.config,
retryConfig: this.retryConfig,
failSilently: this.failSilently,
}
)
}
/**
* Sends a formatted message with blocks.
*
* @param {SlackBlock[]} blocks - Message blocks.
* @param {string} channel - Channel (optional, uses default if not specified).
* @param {string} fallbackText - Fallback text (optional).
* @returns {Promise<SlackResponse>} API response.
*/
async sendFormattedMessage(
blocks: SlackBlock[],
channel?: string,
fallbackText?: string
): Promise<SlackResponse> {
// Validate and filter blocks
const validBlocks = blocks.filter((block) => {
const isValid = validateSlackBlock(block)
if (!isValid) {
console.warn(
'Invalid block filtered out:',
JSON.stringify(block, null, 2)
)
}
return isValid
})
// If no valid blocks remain, send a simple text message instead
if (validBlocks.length === 0) {
console.warn('No valid blocks found, sending fallback text message')
return this.sendMessage(
fallbackText || 'Formatted message (blocks were invalid)',
channel
)
}
return sendSlackMessage(
{
channel: channel || this.config.defaultChannel || '#general',
text: fallbackText || 'Formatted message',
blocks: validBlocks,
...(this.config.botName && { username: this.config.botName }),
},
{
config: this.config,
retryConfig: this.retryConfig,
failSilently: this.failSilently,
}
)
}
/**
* Sends an error message.
*
* @param {string} error - Error message.
* @param {string} channel - Channel (optional, uses default if not specified).
* @returns {Promise<SlackResponse>} API response.
*/
async sendError(error: string, channel?: string): Promise<SlackResponse> {
return this.sendFormattedMessage(
[
createSectionBlock('🚨 *Error*', { type: 'mrkdwn' }),
createSectionBlock(error),
],
channel,
`Error: ${error}`
)
}
/**
* Sends a success message.
*
* @param {string} message - Success message.
* @param {string} channel - Channel (optional, uses default if not specified).
* @returns {Promise<SlackResponse>} API response.
*/
async sendSuccess(message: string, channel?: string): Promise<SlackResponse> {
return this.sendFormattedMessage(
[
createSectionBlock('✅ *Success*', { type: 'mrkdwn' }),
createSectionBlock(message),
],
channel,
`Success: ${message}`
)
}
/**
* Sends a warning message.
*
* @param {string} warning - Warning message.
* @param {string} channel - Channel (optional, uses default if not specified).
* @returns {Promise<SlackResponse>} API response.
*/
async sendWarning(warning: string, channel?: string): Promise<SlackResponse> {
return this.sendFormattedMessage(
[
createSectionBlock('⚠️ *Warning*', { type: 'mrkdwn' }),
createSectionBlock(warning),
],
channel,
`Warning: ${warning}`
)
}
}
|