Setting Up Notification Automation (Email + SMS + Telegram) via Triggers
An automated notification system sends messages to users on trigger events through multiple channels. A unified notification service receives events from the application and distributes them across channels based on user preferences.
Notification Service Architecture
// notification.service.ts
interface NotificationRequest {
userId: string;
type: NotificationType;
data: Record<string, unknown>;
channels?: Channel[]; // if not specified — by user settings
priority?: 'high' | 'normal' | 'low';
}
type NotificationType =
| 'order.placed'
| 'order.shipped'
| 'payment.failed'
| 'password.reset'
| 'promo.discount';
class NotificationService {
async send(request: NotificationRequest): Promise<void> {
const prefs = await this.userPrefsRepo.findByUserId(request.userId);
const channels = request.channels ?? this.resolveChannels(request.type, prefs);
await Promise.allSettled(
channels.map(channel => this.sendViaChannel(channel, request, prefs))
);
}
private resolveChannels(type: NotificationType, prefs: UserPrefs): Channel[] {
const channelMap: Record<NotificationType, Channel[]> = {
'order.placed': ['email', 'telegram'],
'order.shipped': ['email', 'sms', 'telegram'],
'payment.failed': ['email', 'sms'], // critical — all channels
'password.reset': ['email'], // email only
'promo.discount': prefs.marketingChannels // user choice
};
return channelMap[type] ?? ['email'];
}
}
Email via Resend
import { Resend } from 'resend';
const resend = new Resend(process.env.RESEND_API_KEY);
async function sendEmailNotification(
user: User,
type: NotificationType,
data: Record<string, unknown>
) {
const template = emailTemplates[type];
await resend.emails.send({
from: '[email protected]',
to: user.email,
subject: template.subject(data),
react: template.component({ user, ...data })
});
}
// Template for order.shipped
const orderShippedTemplate = {
subject: (data) => `Your order #${data.orderId} has been shipped`,
component: ({ user, orderId, trackingNumber, estimatedDelivery }) => (
<OrderShippedEmail
name={user.firstName}
orderId={orderId}
trackingNumber={trackingNumber}
trackingUrl={`https://example.com/track/${trackingNumber}`}
estimatedDelivery={estimatedDelivery}
/>
)
};
SMS via Twilio
import twilio from 'twilio';
const client = twilio(process.env.TWILIO_SID, process.env.TWILIO_TOKEN);
async function sendSmsNotification(
user: User,
type: NotificationType,
data: Record<string, unknown>
) {
if (!user.phone || !user.phoneVerified) return;
const templates: Record<NotificationType, (data: Record<string, unknown>) => string> = {
'order.shipped': (d) =>
`Order #${d.orderId} shipped. Track: ${d.trackingNumber}. Expect by ${d.date}`,
'payment.failed': (d) =>
`Payment for order #${d.orderId} failed. Update card: ${d.retryUrl}`
};
const text = templates[type]?.(data);
if (!text) return;
await client.messages.create({
to: user.phone,
from: process.env.TWILIO_PHONE,
body: text
});
}
Telegram via Bot API
import TelegramBot from 'node-telegram-bot-api';
const bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN);
async function sendTelegramNotification(
user: User,
type: NotificationType,
data: Record<string, unknown>
) {
if (!user.telegramChatId) return;
const messages: Record<string, (d: Record<string, unknown>) => string> = {
'order.shipped':
(d) => `📦 *Order #${d.orderId} shipped*\n\nTracking: \`${d.trackingNumber}\`\nExpected: ${d.date}`,
'payment.failed':
(d) => `⚠️ *Payment Error*\n\nOrder #${d.orderId} not paid. [Retry](${d.retryUrl})`
};
const text = messages[type]?.(data);
if (!text) return;
await bot.sendMessage(user.telegramChatId, text, {
parse_mode: 'Markdown',
disable_web_page_preview: true
});
}
// Telegram account binding by user
bot.onText(/\/start (.+)/, async (msg, match) => {
const linkToken = match[1];
const userId = await verifyLinkToken(linkToken);
if (userId) {
await userRepo.updateTelegramChatId(userId, msg.chat.id.toString());
bot.sendMessage(msg.chat.id, '✅ Telegram connected! You will receive notifications.');
}
});
Preference Management
// Table user_notification_prefs
interface UserNotificationPrefs {
userId: string;
emailEnabled: boolean;
smsEnabled: boolean;
telegramEnabled: boolean;
marketingEmailEnabled: boolean;
marketingSmsEnabled: boolean;
quietHoursStart: string; // '22:00'
quietHoursEnd: string; // '08:00'
timezone: string; // 'Europe/Moscow'
}
Retry and Queue
// Notifications via Bull Queue with retry
const notificationQueue = new Bull('notifications', { redis: redisConfig });
notificationQueue.process(async (job) => {
const { userId, type, data } = job.data;
await notificationService.send({ userId, type, data });
});
// Add with delay for non-urgent
await notificationQueue.add(
{ userId, type: 'promo.discount', data },
{
delay: 5 * 60 * 1000, // 5 minute delay
attempts: 3,
backoff: { type: 'exponential', delay: 2000 }
}
);
Timeline
Email + SMS + Telegram with preference management and queue — 1–2 weeks.







