|
| 1 | +/** |
| 2 | + * Example: Telegram Notification System |
| 3 | + * Shows how to integrate the notification system with Telegram |
| 4 | + */ |
| 5 | + |
| 6 | +import { Bot } from 'grammy'; |
| 7 | +import { NotificationService } from '../src/core/services/notification-service'; |
| 8 | +import { NotificationConnector } from '../src/connectors/notification-connector'; |
| 9 | +import { TelegramNotificationAdapter } from '../src/adapters/telegram/notification-adapter'; |
| 10 | +import { EventBus } from '../src/core/events/event-bus'; |
| 11 | +import { NotificationCategory } from '../src/core/interfaces/notification'; |
| 12 | + |
| 13 | +// Example logger implementation |
| 14 | +class ConsoleLogger { |
| 15 | + debug(message: string, meta?: unknown) { |
| 16 | + console.debug(`[DEBUG] ${message}`, meta); |
| 17 | + } |
| 18 | + |
| 19 | + info(message: string, meta?: unknown) { |
| 20 | + console.info(`[INFO] ${message}`, meta); |
| 21 | + } |
| 22 | + |
| 23 | + warn(message: string, meta?: unknown) { |
| 24 | + console.warn(`[WARN] ${message}`, meta); |
| 25 | + } |
| 26 | + |
| 27 | + error(message: string, meta?: unknown) { |
| 28 | + console.error(`[ERROR] ${message}`, meta); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Example KV store implementation |
| 33 | +class MemoryKVStore { |
| 34 | + private store = new Map<string, string>(); |
| 35 | + |
| 36 | + async get<T = string>(key: string): Promise<T | null> { |
| 37 | + const value = this.store.get(key); |
| 38 | + return value ? (JSON.parse(value) as T) : null; |
| 39 | + } |
| 40 | + |
| 41 | + async put(key: string, value: string): Promise<void> { |
| 42 | + this.store.set(key, value); |
| 43 | + } |
| 44 | + |
| 45 | + async delete(key: string): Promise<void> { |
| 46 | + this.store.delete(key); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// Initialize services |
| 51 | +const bot = new Bot(process.env.BOT_TOKEN || ''); |
| 52 | +const logger = new ConsoleLogger(); |
| 53 | +const eventBus = new EventBus(); |
| 54 | +const kvStore = new MemoryKVStore(); |
| 55 | + |
| 56 | +// Create notification adapter |
| 57 | +const adapter = new TelegramNotificationAdapter({ |
| 58 | + bot, |
| 59 | + defaultLocale: 'en', |
| 60 | +}); |
| 61 | + |
| 62 | +// Create notification connector |
| 63 | +const connector = new NotificationConnector({ |
| 64 | + adapter, |
| 65 | + storage: kvStore, |
| 66 | + logger, |
| 67 | + eventBus, |
| 68 | + retryConfig: { |
| 69 | + maxAttempts: 3, |
| 70 | + initialDelay: 1000, |
| 71 | + maxDelay: 60000, |
| 72 | + backoffMultiplier: 2, |
| 73 | + }, |
| 74 | +}); |
| 75 | + |
| 76 | +// Create notification service |
| 77 | +const notificationService = new NotificationService({ |
| 78 | + connector, |
| 79 | + logger, |
| 80 | + eventBus, |
| 81 | + defaultLocale: 'en', |
| 82 | +}); |
| 83 | + |
| 84 | +// Example usage |
| 85 | +async function sendWelcomeNotification(userId: string) { |
| 86 | + await notificationService.send( |
| 87 | + userId, |
| 88 | + 'welcome', |
| 89 | + { |
| 90 | + type: 'user_onboarding', |
| 91 | + data: { |
| 92 | + username: 'John Doe', |
| 93 | + registrationDate: new Date().toISOString(), |
| 94 | + }, |
| 95 | + }, |
| 96 | + NotificationCategory.SYSTEM, |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +// Example batch notifications |
| 101 | +async function sendMaintenanceNotifications(userIds: string[]) { |
| 102 | + await notificationService.sendBatch( |
| 103 | + userIds, |
| 104 | + 'maintenance', |
| 105 | + { |
| 106 | + type: 'system_maintenance', |
| 107 | + data: { |
| 108 | + startTime: '2025-01-27 10:00 UTC', |
| 109 | + duration: '2 hours', |
| 110 | + affectedServices: ['API', 'Dashboard'], |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + batchSize: 50, |
| 115 | + delayBetweenBatches: 1000, |
| 116 | + }, |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +// Listen to notification events |
| 121 | +eventBus.on('notification:sent', (event) => { |
| 122 | + console.log('Notification sent:', event); |
| 123 | +}); |
| 124 | + |
| 125 | +eventBus.on('notification:failed', (event) => { |
| 126 | + console.error('Notification failed:', event); |
| 127 | +}); |
| 128 | + |
| 129 | +eventBus.on('notification:batch:completed', (event) => { |
| 130 | + console.log('Batch completed:', event); |
| 131 | +}); |
| 132 | + |
| 133 | +// Example with custom templates |
| 134 | +const templates = { |
| 135 | + welcome: { |
| 136 | + en: { |
| 137 | + body: `Welcome {{username}}! |
| 138 | + |
| 139 | +Thank you for joining our platform. |
| 140 | +Registration date: {{registrationDate}}`, |
| 141 | + parseMode: 'HTML' as const, |
| 142 | + buttons: [ |
| 143 | + [ |
| 144 | + { |
| 145 | + text: 'Get Started', |
| 146 | + callbackData: 'start_tutorial', |
| 147 | + }, |
| 148 | + { |
| 149 | + text: 'View Help', |
| 150 | + url: 'https://example.com/help', |
| 151 | + }, |
| 152 | + ], |
| 153 | + ], |
| 154 | + }, |
| 155 | + }, |
| 156 | + maintenance: { |
| 157 | + en: { |
| 158 | + body: `<b>System Maintenance Notice</b> |
| 159 | +
|
| 160 | +We will be performing scheduled maintenance: |
| 161 | +• Start: {{startTime}} |
| 162 | +• Duration: {{duration}} |
| 163 | +• Affected: {{affectedServices}} |
| 164 | +
|
| 165 | +We apologize for any inconvenience.`, |
| 166 | + parseMode: 'HTML' as const, |
| 167 | + }, |
| 168 | + }, |
| 169 | +}; |
| 170 | + |
| 171 | +export { notificationService, sendWelcomeNotification, sendMaintenanceNotifications, templates }; |
0 commit comments