Настройка автоматизации уведомлений (email + SMS + Telegram) через триггеры
Система автоматических уведомлений отправляет сообщения пользователям по триггерным событиям через несколько каналов. Единый сервис уведомлений принимает события из приложения и распределяет их по каналам с учётом предпочтений пользователя.
Архитектура Notification Service
// notification.service.ts
interface NotificationRequest {
userId: string;
type: NotificationType;
data: Record<string, unknown>;
channels?: Channel[]; // если не указаны — по настройкам пользователя
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'], // критично — все каналы
'password.reset': ['email'], // только email
'promo.discount': prefs.marketingChannels // по выбору пользователя
};
return channelMap[type] ?? ['email'];
}
}
Email через 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 })
});
}
// Шаблон order.shipped
const orderShippedTemplate = {
subject: (data) => `Ваш заказ #${data.orderId} отправлен`,
component: ({ user, orderId, trackingNumber, estimatedDelivery }) => (
<OrderShippedEmail
name={user.firstName}
orderId={orderId}
trackingNumber={trackingNumber}
trackingUrl={`https://example.com/track/${trackingNumber}`}
estimatedDelivery={estimatedDelivery}
/>
)
};
SMS через 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) =>
`Заказ #${d.orderId} отправлен. Трек: ${d.trackingNumber}. Ожидайте к ${d.date}`,
'payment.failed': (d) =>
`Оплата заказа #${d.orderId} не прошла. Обновите данные карты: ${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 через 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) => `📦 *Заказ #${d.orderId} отправлен*\n\nТрек-номер: \`${d.trackingNumber}\`\nОжидаемая доставка: ${d.date}`,
'payment.failed':
(d) => `⚠️ *Ошибка оплаты*\n\nЗаказ #${d.orderId} не оплачен. [Повторить оплату](${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 аккаунта пользователем
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 успешно подключён! Вы будете получать уведомления.');
}
});
Управление предпочтениями
// Таблица 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 и очередь
// Уведомления через Bull Queue с retry
const notificationQueue = new Bull('notifications', { redis: redisConfig });
notificationQueue.process(async (job) => {
const { userId, type, data } = job.data;
await notificationService.send({ userId, type, data });
});
// Добавление с delay для non-urgent
await notificationQueue.add(
{ userId, type: 'promo.discount', data },
{
delay: 5 * 60 * 1000, // 5 минут задержки
attempts: 3,
backoff: { type: 'exponential', delay: 2000 }
}
);
Сроки
Email + SMS + Telegram с управлением предпочтениями и очередью — 1–2 недели.







