multi-notifications-channel
v1.0.0
Published
Universal multi-channel notification system for Node.js with Email, SMS, and Slack support - works with any framework
Downloads
103
Maintainers
Readme
multi-notifications-channel
Universal multi-channel notification system for Node.js with Email, SMS, and Slack support. Works with any JavaScript/TypeScript framework or environment.
✨ Features
- 📧 Email - Brevo provider with HTML & template support
- 📱 SMS - Twilio provider with OTP verification
- 💬 Slack - Full integration (DMs, channels, Block Kit)
- 🔄 Multi-Channel - Send to multiple channels with one call
- 🌍 Universal - Works with Express, Next.js, Nuxt.js, plain Node.js, and more
- 🎯 Type-Safe - Full TypeScript support
- ⚡ Zero Dependencies - No framework lock-in
- 🔌 Extensible - Easy to add custom providers
📦 Installation
npm install multi-notifications-channelProvider SDKs
Install only the providers you need:
# For email support
npm install @getbrevo/brevo
# For SMS support
npm install twilio
# For Slack support
npm install @slack/web-api🚀 Quick Start
import { NotificationService } from 'multi-notifications-channel';
const notifier = new NotificationService({
brevo: {
apiKey: process.env.BREVO_API_KEY!,
senderEmail: '[email protected]',
},
twilio: {
accountSid: process.env.TWILIO_ACCOUNT_SID!,
authToken: process.env.TWILIO_AUTH_TOKEN!,
phoneNumber: process.env.TWILIO_PHONE_NUMBER!,
},
slack: {
botToken: process.env.SLACK_BOT_TOKEN!,
},
});
// Send email
await notifier.sendEmail({
to: '[email protected]',
subject: 'Welcome!',
content: '<h1>Hello!</h1>',
});
// Send SMS
await notifier.sendSMS({
phoneNumber: '+1234567890',
message: 'Your code is 123456',
});
// Send Slack DM
await notifier.sendSlackDMByEmail('[email protected]', 'Hello from Slack!');📚 Framework Examples
Express.js
import express from 'express';
import { NotificationService } from 'multi-notifications-channel';
const notifier = new NotificationService({ /* config */ });
const app = express();
app.post('/api/notify', async (req, res) => {
const result = await notifier.sendEmail({
to: req.body.email,
subject: 'Notification',
content: req.body.message,
});
res.json(result);
});Next.js (App Router)
// app/api/notify/route.ts
import { NotificationService } from 'multi-notifications-channel';
const notifier = new NotificationService({ /* config */ });
export async function POST(request: Request) {
const { to, subject, content } = await request.json();
const result = await notifier.sendEmail({ to, subject, content });
return Response.json(result);
}Nuxt.js (Server API)
// server/api/notify.post.ts
import { NotificationService } from 'multi-notifications-channel';
const notifier = new NotificationService({ /* config */ });
export default defineEventHandler(async (event) => {
const { to, subject, content } = await readBody(event);
return await notifier.sendEmail({ to, subject, content });
});Plain Node.js
import { NotificationService } from 'multi-notifications-channel';
const notifier = new NotificationService({ /* config */ });
await notifier.sendEmail({
to: '[email protected]',
subject: 'Hello',
content: 'World!',
});📖 Usage Guide
Email Notifications
Send HTML Email
await notifier.sendEmail({
to: '[email protected]',
subject: 'Order Confirmation',
content: '<h1>Thank you!</h1><p>Order #12345</p>',
});Send Template Email
await notifier.sendEmailWithTemplate({
to: '[email protected]',
subject: 'Monthly Report',
templateId: 1,
params: {
name: 'John',
month: 'January',
},
});SMS Notifications
Send SMS
await notifier.sendSMS({
phoneNumber: '+1234567890',
message: 'Your order has shipped!',
});Send & Verify OTP
// Send OTP
await notifier.sendVerificationCode('+1234567890');
// Verify OTP
const isValid = await notifier.verifyCode('+1234567890', '123456');Slack Notifications
Send DM by User ID
await notifier.sendSlackDM('U1234567890', 'Hello!');Send DM by Email
await notifier.sendSlackDMByEmail('[email protected]', 'Hello!');Send to Channel
await notifier.sendSlackChannel('C1234567890', 'Deployment complete!');Send with Block Kit
await notifier.sendSlackMessage({
userId: 'U1234567890',
message: 'Fallback text',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: '*Alert* :warning:\nAction required!',
},
},
],
});Multi-Channel Notifications
const result = await notifier.send({
channels: ['EMAIL', 'SMS', 'SLACK'],
recipient: {
email: '[email protected]',
phone: '+1234567890',
slackUserId: 'U1234567890',
},
subject: 'Critical Alert',
content: 'Immediate action required!',
});
console.log(`Success: ${result.successCount}/${result.results.length}`);⚙️ Configuration
Full Configuration
const notifier = new NotificationService({
// Email provider
brevo: {
apiKey: 'your-api-key',
senderEmail: '[email protected]',
senderName: 'My App', // optional
},
// SMS provider
twilio: {
accountSid: 'your-account-sid',
authToken: 'your-auth-token',
phoneNumber: '+1234567890',
verifyServiceSid: 'your-verify-sid', // optional, for OTP
},
// Slack provider
slack: {
botToken: 'xoxb-your-bot-token',
},
// Custom logger (optional)
logger: customLogger,
// Environment (optional)
env: 'production',
});Custom Logger
import { ILogger } from 'multi-notifications-channel';
const customLogger: ILogger = {
log: (msg, ...args) => console.log(msg, ...args),
error: (msg, ...args) => console.error(msg, ...args),
warn: (msg, ...args) => console.warn(msg, ...args),
};
const notifier = new NotificationService({
logger: customLogger,
// ... other config
});🔧 API Reference
NotificationService
Email Methods
sendEmail(options)- Send HTML or plain emailsendEmailWithTemplate(options)- Send template-based email
SMS Methods
sendSMS(options)- Send SMS messagesendVerificationCode(phoneNumber)- Send OTP codeverifyCode(phoneNumber, code)- Verify OTP code
Slack Methods
sendSlackMessage(options)- Flexible Slack messagingsendSlackDM(userId, message, blocks?)- DM by user IDsendSlackDMByEmail(email, message, blocks?)- DM by emailsendSlackChannel(channelId, message, blocks?)- Channel message
Multi-Channel
send(options)- Send to multiple channels
Utility Methods
hasChannel(channel)- Check if channel is availablegetAvailableChannels()- Get all configured channelsgetRegistry()- Access provider registry
🔐 Environment Variables
# Brevo
BREVO_API_KEY=your-api-key
# Twilio
TWILIO_ACCOUNT_SID=your-account-sid
TWILIO_AUTH_TOKEN=your-auth-token
TWILIO_PHONE_NUMBER=+1234567890
TWILIO_VERIFY_SERVICE_SID=your-verify-sid
# Slack
SLACK_BOT_TOKEN=xoxb-your-bot-token🎯 TypeScript Support
Full TypeScript support with comprehensive type definitions:
import {
NotificationService,
NotificationChannel,
NotificationResult,
SendEmailOptions,
SendSmsOptions,
SendSlackOptions,
MultiChannelOptions,
ILogger,
} from 'multi-notifications-channel';📝 Examples
See the examples/ directory for complete examples:
express-example.ts- Express.js integrationnextjs-example.ts- Next.js API routesnuxtjs-example.ts- Nuxt.js server APIplain-nodejs-example.ts- Plain Node.js script
🔄 Migration from NestJS Version
If you're migrating from nestjs-multi-notifications:
Before (NestJS):
@Module({
imports: [NotificationsModule.forRoot()],
})
export class AppModule {}
// In service
constructor(private notificationsService: NotificationsService) {}After (Framework-agnostic):
const notifier = new NotificationService({
brevo: { apiKey: '...', senderEmail: '...' },
});
await notifier.sendEmail({ ... });📄 License
MIT
🤝 Contributing
Contributions welcome! Please open an issue or PR.
💬 Support
For issues and questions, use GitHub Issues.
