Налаштування автоматизації повідомлень (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 });
});
// Додавання з затримкою для 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 тижня.







