JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 20
  • Score
    100M100P100Q49490F
  • License MIT

A NestJS messaging module for Slack and Discord notifications, built with a focus on simplicity and ease of use.

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nest-pingbot) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Logo

    Nest Pingbot ๐Ÿ””

    Node.js Version TypeScript NestJS License NPM Version Unit Tests

    Nest Pingbot is a flexible NestJS module for integrating Slack and Discord messaging capabilities into your applications. Send notifications, alerts, and messages to your team's communication channels directly from your NestJS application.


    Features โšก

    • Multi-Platform Support: Send messages to both Slack and Discord from a single interface
    • Platform-Specific Features:
      • Slack: Support for Block Kit UI elements, thread replies, and rich formatting
      • Discord: Support for embeds and advanced message formatting
    • Flexible Configuration:
      • Use only Slack, only Discord, or both simultaneously
      • Clear error handling when platforms aren't configured
    • Type Safety:
      • Full TypeScript support with proper interfaces for all message components
      • Dedicated types for Slack Block Kit components
    • Developer Experience:
      • Simple integration with any NestJS application
      • Comprehensive test coverage
      • Well-documented API

    Installation โš™๏ธ

    npm install nest-pingbot

    Usage ๐Ÿš€

    Module Registration

    You can register the module in multiple ways:

    Basic Registration

    import { Module } from '@nestjs/common';
    import { MessagingModule, MessagePlatform } from 'nest-pingbot';
    
    @Module({
      imports: [
        MessagingModule.register({
          platforms: {
            slack: {
              token: 'your-slack-token',
              // or
              webhookUrl: 'your-slack-webhook-url',
            },
            discord: {
              token: 'your-discord-token',
            },
          },
          defaultPlatform: MessagePlatform.SLACK,
          debug: true, // Optional debugging
        }),
      ],
    })
    export class AppModule {}

    Global Registration

    import { Module } from '@nestjs/common';
    import { MessagingModule } from 'nest-pingbot';
    
    @Module({
      imports: [
        MessagingModule.forRoot({
          platforms: {
            slack: {
              token: process.env.SLACK_TOKEN,
            },
          },
          debug: true,
        }),
      ],
    })
    export class AppModule {}

    Async Registration

    import { Module } from '@nestjs/common';
    import { MessagingModule } from 'nest-pingbot';
    import { ConfigModule, ConfigService } from '@nestjs/config';
    
    @Module({
      imports: [
        ConfigModule.forRoot(),
        MessagingModule.registerAsync({
          imports: [ConfigModule],
          inject: [ConfigService],
          useFactory: (configService: ConfigService) => ({
            platforms: {
              slack: {
                token: configService.get('SLACK_TOKEN'),
              },
              discord: {
                token: configService.get('DISCORD_TOKEN'),
              },
            },
            debug: configService.get('NODE_ENV') !== 'production',
          }),
        }),
      ],
    })
    export class AppModule {}

    Sending Messages

    Inject the MessagingService into your controllers or services:

    import { Injectable } from '@nestjs/common';
    import { MessagingService } from 'nest-pingbot';
    
    @Injectable()
    export class NotificationService {
      constructor(private readonly messagingService: MessagingService) {}
    
      async sendAlert(message: string) {
        return this.messagingService.sendMessage({
          slack: {
            channel: 'alerts',
            text: message,
            blocks: [
              {
                type: 'header',
                text: {
                  type: 'plain_text',
                  text: 'New Alert',
                },
              },
              {
                type: 'section',
                text: {
                  type: 'mrkdwn',
                  text: message,
                },
              },
            ],
          },
          discord: {
            channelId: '1234567890123456789',
            content: 'New Alert',
            embeds: [
              {
                title: 'Alert',
                description: message,
                color: 0xff0000,
                timestamp: new Date(),
              },
            ],
          },
        });
      }
    
      async scheduleReminder(message: string, scheduledTime: Date) {
        return this.messagingService.sendMessage({
          slack: {
            channel: 'reminders',
            text: `Reminder: ${message}`,
          },
          schedule: {
            time: scheduledTime,
          },
        });
      }
    }

    Working with Threads

    Slack Threads

    async continueSlackThread(threadTs: string, message: string) {
      return this.messagingService.sendMessage({
        slack: {
          channel: 'general',
          text: message,
          threadTs: threadTs,
        },
      });
    }

    Discord Threads

    async continueDiscordThread(channelId: string, threadId: string, message: string) {
      return this.messagingService.sendMessage({
        discord: {
          channelId: channelId,
          threadId: threadId,
          content: message,
        },
      });
    }

    API Reference ๐Ÿ“’

    MessagingService

    The main service for sending messages:

    • sendMessage(options: MessageOptions): Promise<any> - Send messages to one or both platforms

    MessageOptions

    interface MessageOptions {
      slack?: SlackMessageOptions;
      discord?: DiscordMessageOptions;
      schedule?: ScheduleOptions;
    }

    SlackMessageOptions

    interface SlackMessageOptions {
      channel: string;
      text?: string;
      blocks?: SlackBlock[];
      threadTs?: string;
      unfurlLinks?: boolean;
      unfurlMedia?: boolean;
      mrkdwn?: boolean;
    }

    DiscordMessageOptions

    interface DiscordMessageOptions {
      channelId: string;
      content?: string;
      embeds?: DiscordEmbed[];
      threadId?: string;
      components?: any[];
    }

    ScheduleOptions

    interface ScheduleOptions {
      time: Date;
      timezone?: string;
    }

    Testing ๐Ÿงช

    The module includes comprehensive tests:

    # Run unit tests
    npm test

    Contributing ๐Ÿค

    Contributions are welcome! Please feel free to submit a Pull Request.

    License ๐Ÿ“œ

    This project is licensed under the MIT License. See the LICENSE file for details.


    Acknowledgments ๐Ÿ™Œ

    • Built with NestJS for a structured and scalable messaging solution.
    • Inspired by the need for simple, unified messaging across different platforms.
    • Made with โค๏ธ for developers who need reliable communication integrations.