This commit is contained in:
Baobhan Sith
2025-04-26 19:39:03 +08:00
parent dbd2ce7f0f
commit 62e53c3cdc
13 changed files with 846 additions and 144 deletions
@@ -0,0 +1,98 @@
import nodemailer from 'nodemailer';
import Mail from 'nodemailer/lib/mailer'; // Import Mail type for transporter
import { INotificationSender } from '../notification.dispatcher.service';
import { ProcessedNotification } from '../notification.processor.service';
import { EmailConfig } from '../../types/notification.types';
import { settingsService } from '../settings.service'; // Import settingsService
class EmailSenderService implements INotificationSender {
async send(notification: ProcessedNotification): Promise<void> {
const config = notification.config as EmailConfig;
const { to, subjectTemplate, smtpHost, smtpPort, smtpSecure, smtpUser, smtpPass, from } = config;
const subject = notification.subject || 'Notification'; // Use processed subject or default
const body = notification.body; // Use processed body (assuming HTML)
if (!to) {
console.error('[EmailSender] Missing recipient address (to) in configuration.');
throw new Error('Email configuration is incomplete (missing recipient address).');
}
try {
// Get global settings for fallback SMTP configuration using settingsService
const globalSmtpHost = await settingsService.getSetting('smtpHost');
const globalSmtpPortStr = await settingsService.getSetting('smtpPort');
const globalSmtpSecureStr = await settingsService.getSetting('smtpSecure');
const globalSmtpUser = await settingsService.getSetting('smtpUser');
const globalSmtpPass = await settingsService.getSetting('smtpPass');
const globalSmtpFrom = await settingsService.getSetting('smtpFrom');
// Determine SMTP settings: prioritize channel-specific, then global, then defaults
const finalSmtpHost = smtpHost || globalSmtpHost;
const finalSmtpPort = smtpPort ?? (globalSmtpPortStr ? parseInt(globalSmtpPortStr, 10) : 587); // Default port 587
const finalSmtpSecure = smtpSecure ?? (globalSmtpSecureStr === 'true') ?? false; // Default secure false
const finalSmtpUser = smtpUser || globalSmtpUser;
const finalSmtpPass = smtpPass || globalSmtpPass;
const finalFrom = from || globalSmtpFrom || 'noreply@nexus-terminal.local'; // Default from
if (!finalSmtpHost) {
console.error('[EmailSender] SMTP host is not configured (neither channel-specific nor global).');
throw new Error('SMTP host configuration is missing.');
}
// Basic validation for port
if (isNaN(finalSmtpPort) || finalSmtpPort <= 0) {
console.error(`[EmailSender] Invalid SMTP port configured: ${finalSmtpPort}. Using default 587.`);
// finalSmtpPort = 587; // Or throw error depending on strictness needed
throw new Error(`Invalid SMTP port configured: ${finalSmtpPort}`);
}
const transporterOptions: nodemailer.TransportOptions = {
host: finalSmtpHost,
port: finalSmtpPort,
secure: finalSmtpSecure, // true for 465, false for other ports
auth: (finalSmtpUser && finalSmtpPass) ? {
user: finalSmtpUser,
pass: finalSmtpPass,
} : undefined, // Only include auth if user/pass are provided
tls: {
// Do not fail on invalid certs if secure is false or not explicitly required
rejectUnauthorized: finalSmtpSecure // Stricter check based on finalSecure value
}
};
const transporter = nodemailer.createTransport(transporterOptions);
// Verify connection configuration (optional but recommended)
// try {
// await transporter.verify();
// console.log('[EmailSender] SMTP configuration verified successfully.');
// } catch (verifyError) {
// console.error('[EmailSender] SMTP configuration verification failed:', verifyError);
// throw new Error(`SMTP verification failed: ${verifyError.message}`);
// }
const mailOptions: Mail.Options = {
from: `"${finalFrom.split('@')[0]}" <${finalFrom}>`, // sender address format "Sender Name <sender@example.com>"
to: to, // list of receivers (comma-separated)
subject: subject, // Subject line
// text: 'Plain text body', // Plain text body (optional, provide if HTML is not supported well)
html: body, // html body
};
console.log(`[EmailSender] Sending email notification to: ${to} with subject: "${subject}"`);
const info = await transporter.sendMail(mailOptions);
console.log(`[EmailSender] Email sent successfully. Message ID: ${info.messageId}`);
} catch (error: any) {
console.error(`[EmailSender] Error sending email notification to ${to}:`, error);
// Provide more specific error message if possible
throw new Error(`Failed to send email notification: ${error.message || error}`);
}
}
}
// Create and export singleton instance
const emailSenderService = new EmailSenderService();
export default emailSenderService;
@@ -0,0 +1,53 @@
import axios from 'axios';
import { INotificationSender } from '../notification.dispatcher.service'; // Import the interface
import { ProcessedNotification } from '../notification.processor.service';
import { TelegramConfig } from '../../types/notification.types';
class TelegramSenderService implements INotificationSender {
async send(notification: ProcessedNotification): Promise<void> {
const config = notification.config as TelegramConfig;
const { botToken, chatId } = config;
const messageBody = notification.body;
if (!botToken || !chatId) {
console.error('[TelegramSender] Missing botToken or chatId in configuration.');
throw new Error('Telegram configuration is incomplete (missing botToken or chatId).');
}
const apiUrl = `https://api.telegram.org/bot${botToken}/sendMessage`;
try {
console.log(`[TelegramSender] Sending notification to chat ID: ${chatId}`);
const response = await axios.post(apiUrl, {
chat_id: chatId,
text: messageBody,
parse_mode: 'Markdown', // Use standard Markdown
disable_web_page_preview: true // Optional: disable link previews
}, {
timeout: 10000 // Set a timeout (e.g., 10 seconds)
});
if (response.data && response.data.ok) {
console.log(`[TelegramSender] Successfully sent notification to chat ID: ${chatId}`);
} else {
// Log Telegram's error description if available
const errorDescription = response.data?.description || 'Unknown error from Telegram API';
console.error(`[TelegramSender] Failed to send notification. Telegram API response: ${errorDescription}`, response.data);
throw new Error(`Telegram API error: ${errorDescription}`);
}
} catch (error: any) {
if (axios.isAxiosError(error)) {
console.error(`[TelegramSender] Axios error sending notification: ${error.message}`, error.response?.data);
throw new Error(`Failed to send Telegram notification (Axios Error): ${error.message}`);
} else {
console.error(`[TelegramSender] Unexpected error sending notification:`, error);
throw new Error(`Failed to send Telegram notification (Unexpected Error): ${error.message || error}`);
}
}
}
}
// Create and export singleton instance
const telegramSenderService = new TelegramSenderService();
export default telegramSenderService;
@@ -0,0 +1,107 @@
import axios, { Method } from 'axios';
import { INotificationSender } from '../notification.dispatcher.service';
import { ProcessedNotification } from '../notification.processor.service';
import { WebhookConfig } from '../../types/notification.types';
class WebhookSenderService implements INotificationSender {
async send(notification: ProcessedNotification): Promise<void> {
const config = notification.config as WebhookConfig;
const { url, method = 'POST', headers = {} } = config; // Default method to POST
const requestBody = notification.body; // Body is already processed by the processor
if (!url) {
console.error('[WebhookSender] Missing webhook URL in configuration.');
throw new Error('Webhook configuration is incomplete (missing URL).');
}
// Validate URL format (basic check)
try {
new URL(url);
} catch (e) {
console.error(`[WebhookSender] Invalid webhook URL format: ${url}`);
throw new Error(`Invalid webhook URL format: ${url}`);
}
// Prepare headers
const finalHeaders: Record<string, string> = {
'Content-Type': 'application/json', // Default Content-Type, can be overridden by config
...headers, // Merge custom headers from config
};
// Determine HTTP method
const requestMethod: Method = method.toUpperCase() as Method; // Ensure method is uppercase and valid Axios Method type
const validMethods: Method[] = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
if (!validMethods.includes(requestMethod)) {
console.error(`[WebhookSender] Invalid HTTP method specified: ${method}. Defaulting to POST.`);
// requestMethod = 'POST'; // Or throw an error
throw new Error(`Invalid HTTP method specified: ${method}`);
}
try {
console.log(`[WebhookSender] Sending ${requestMethod} notification to webhook URL: ${url}`);
// Prepare request data based on method
let requestData: any = undefined;
let requestParams: any = undefined;
// For GET requests, data is usually sent as query params.
// For POST/PUT/PATCH, data is sent in the body.
// We assume the processed `requestBody` is intended for the body.
// If the template was designed for GET params, this might need adjustment.
if (['POST', 'PUT', 'PATCH'].includes(requestMethod)) {
// Try to parse body as JSON if Content-Type suggests it, otherwise send as string
if (finalHeaders['Content-Type']?.toLowerCase().includes('application/json')) {
try {
requestData = JSON.parse(requestBody);
} catch (parseError) {
console.warn(`[WebhookSender] Failed to parse request body as JSON for Content-Type application/json. Sending as raw string. Body: ${requestBody.substring(0,100)}...`);
requestData = requestBody;
}
} else {
requestData = requestBody;
}
} else if (requestMethod === 'GET') {
// For GET, we might need to parse the body (if it's a query string) or handle differently.
// For simplicity now, we won't automatically convert body to params for GET.
// User should configure GET webhooks appropriately (e.g., URL includes params).
console.warn(`[WebhookSender] Sending data in body for GET request might not be standard. URL: ${url}`);
// If requestBody is intended as query params, parsing logic would be needed here.
// requestParams = querystring.parse(requestBody); // Example
}
const response = await axios({
method: requestMethod,
url: url,
headers: finalHeaders,
data: requestData,
params: requestParams,
timeout: 15000 // Set a timeout (e.g., 15 seconds)
});
// Check response status (e.g., 2xx indicates success)
if (response.status >= 200 && response.status < 300) {
console.log(`[WebhookSender] Successfully sent notification to webhook. Status: ${response.status}`);
} else {
console.warn(`[WebhookSender] Webhook endpoint responded with status: ${response.status}`, response.data);
// Consider throwing an error for non-2xx responses depending on requirements
// throw new Error(`Webhook endpoint responded with status: ${response.status}`);
}
} catch (error: any) {
if (axios.isAxiosError(error)) {
console.error(`[WebhookSender] Axios error sending notification to ${url}: ${error.message}`, error.response?.status, error.response?.data);
throw new Error(`Failed to send webhook notification (Axios Error): ${error.message}`);
} else {
console.error(`[WebhookSender] Unexpected error sending notification to ${url}:`, error);
throw new Error(`Failed to send webhook notification (Unexpected Error): ${error.message || error}`);
}
}
}
}
// Create and export singleton instance
const webhookSenderService = new WebhookSenderService();
export default webhookSenderService;