@wasserstoff/notification-system
v0.0.3
Published
A flexible and extensible TypeScript-based notification system supporting multiple channels including Gmail, Twilio SMS, and desktop notifications.
Downloads
5
Readme
@wasserstoff/notification-system
A flexible and extensible TypeScript-based notification system that supports multiple notification channels including Gmail, Twilio SMS, and desktop notifications.
Features
- Multiple Notification Channels:
- 📧 Gmail Email Notifications
- 📱 Twilio SMS Notifications
- 💻 Desktop Notifications
- TypeScript Support: Built with TypeScript for type safety and better development experience
- Easy to Extend: Implement the
ISenderinterface to add new notification channels - Simple API: Consistent interface across all notification methods
Quick Start
Install the package and its peer dependencies:
npm install @wstf/notification-system dotenv node-notifier nodemailer twilioCreate a
.envfile in your project root with your credentials (see Configuration below).Start sending notifications:
import { GmailSender, TwilioSMSSender, DesktopNotifier } from '@wstf/notification-system'; import 'dotenv/config'; // Initialize senders const gmail = new GmailSender(process.env.GMAIL_USER!, process.env.GMAIL_PASS!); const sms = new TwilioSMSSender(process.env.TWILIO_SID!, process.env.TWILIO_TOKEN!, process.env.TWILIO_FROM!); const desktop = new DesktopNotifier('MyApp'); // Send notifications async function sendNotifications() { const message = 'Hello from Notification System!'; await gmail.send(message); await sms.send(message); await desktop.send(message); } sendNotifications().catch(console.error);
Installation
npm install @wstf/notification-systemPeer Dependencies
This package requires the following peer dependencies which you'll need to install separately:
npm install dotenv node-notifier nodemailer twilioConfiguration
Create a .env file in your project root with the following variables:
# Gmail Configuration
[email protected]
GMAIL_PASS=your-app-specific-password # Use App Password for 2FA accounts
# Twilio Configuration
TWILIO_SID=your-account-sid
TWILIO_TOKEN=your-auth-token
TWILIO_FROM=your-twilio-phone-number # Format: +1234567890Note: For Gmail, you may need to generate an App Password if you have 2FA enabled on your Google account.
Usage
Basic Usage
import { GmailSender, TwilioSMSSender, DesktopNotifier } from '@wstf/notification-system';
import 'dotenv/config';
// Initialize senders
const gmail = new GmailSender(process.env.GMAIL_USER!, process.env.GMAIL_PASS!);
const sms = new TwilioSMSSender(process.env.TWILIO_SID!, process.env.TWILIO_TOKEN!, process.env.TWILIO_FROM!);
const desktop = new DesktopNotifier('MyApp');
// Send notifications
async function sendNotifications() {
const message = 'Hello from Notification System!';
await gmail.send(message);
await sms.send(message);
await desktop.send(message);
}
sendNotifications().catch(console.error);Available Senders
1. Gmail Sender
const gmail = new GmailSender(email: string, password: string);
await gmail.send('Your email message here');2. Twilio SMS Sender
const sms = new TwilioSMSSender(accountSid: string, authToken: string, fromNumber: string);
await sms.send('Your SMS message here');3. Desktop Notifier
const desktop = new DesktopNotifier(title?: string);
await desktop.send('Your desktop notification here');Extending the System
You can easily add new notification channels by implementing the ISender interface:
import { ISender } from '@wstf/notification-system';
export class MyCustomSender implements ISender {
async send(content: string): Promise<void> {
// Implement your custom notification logic here
console.log('Sending notification:', content);
}
}Error Handling
Each sender implements error handling specific to its notification channel. Errors are propagated up to the caller, so make sure to handle them appropriately:
try {
await sms.send('Important message!');
} catch (error) {
console.error('Failed to send SMS:', error);
// Handle error or fallback to another notification method
}Security Considerations
- Gmail: Use an App Password instead of your main Google account password
- Twilio: Keep your Auth Token and Account SID secure
- Environment Variables: Never commit your
.envfile to version control
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
ISC © 2023
