Fix jwt-token
This commit is contained in:
@@ -5,6 +5,8 @@ const { Op } = require('sequelize');
|
||||
class AlertService {
|
||||
constructor() {
|
||||
this.twilioClient = null;
|
||||
this.twilioPhone = null;
|
||||
this.twilioEnabled = false;
|
||||
this.initializeTwilio();
|
||||
}
|
||||
|
||||
@@ -89,13 +91,40 @@ class AlertService {
|
||||
}
|
||||
|
||||
initializeTwilio() {
|
||||
if (process.env.TWILIO_ACCOUNT_SID && process.env.TWILIO_AUTH_TOKEN) {
|
||||
this.twilioClient = twilio(
|
||||
process.env.TWILIO_ACCOUNT_SID,
|
||||
process.env.TWILIO_AUTH_TOKEN
|
||||
);
|
||||
} else {
|
||||
console.warn('Twilio credentials not configured. SMS alerts will be disabled.');
|
||||
// Check if Twilio credentials are provided
|
||||
const accountSid = process.env.TWILIO_ACCOUNT_SID;
|
||||
const authToken = process.env.TWILIO_AUTH_TOKEN;
|
||||
const phoneNumber = process.env.TWILIO_PHONE_NUMBER;
|
||||
|
||||
// If any Twilio credential is missing, disable SMS functionality
|
||||
if (!accountSid || !authToken || !phoneNumber ||
|
||||
accountSid.trim() === '' || authToken.trim() === '' || phoneNumber.trim() === '') {
|
||||
console.log('📱 Twilio credentials not configured - SMS alerts disabled');
|
||||
console.log('ℹ️ To enable SMS alerts, set TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, and TWILIO_PHONE_NUMBER');
|
||||
this.twilioEnabled = false;
|
||||
this.twilioClient = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate Twilio Account SID format
|
||||
if (!accountSid.startsWith('AC')) {
|
||||
console.log('⚠️ Invalid Twilio Account SID format - SMS alerts disabled');
|
||||
console.log('ℹ️ Account SID must start with "AC"');
|
||||
this.twilioEnabled = false;
|
||||
this.twilioClient = null;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.twilioClient = twilio(accountSid, authToken);
|
||||
this.twilioPhone = phoneNumber;
|
||||
this.twilioEnabled = true;
|
||||
console.log('📱 Twilio SMS service initialized successfully');
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to initialize Twilio:', error.message);
|
||||
console.log('📱 SMS alerts disabled due to Twilio initialization error');
|
||||
this.twilioEnabled = false;
|
||||
this.twilioClient = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,27 +372,63 @@ class AlertService {
|
||||
}
|
||||
|
||||
async sendSMSAlert(phoneNumber, message, rule, detection) {
|
||||
if (!this.twilioClient) {
|
||||
throw new Error('Twilio not configured');
|
||||
// Check if Twilio is enabled
|
||||
if (!this.twilioEnabled || !this.twilioClient) {
|
||||
console.log('📱 SMS alert skipped - Twilio not configured');
|
||||
console.log(`📱 Would have sent to ${phoneNumber}: ${message}`);
|
||||
|
||||
return await AlertLog.create({
|
||||
alert_rule_id: rule.id,
|
||||
detection_id: detection.id,
|
||||
alert_type: 'sms',
|
||||
recipient: phoneNumber,
|
||||
message: message,
|
||||
status: 'failed',
|
||||
sent_at: new Date(),
|
||||
external_id: null,
|
||||
priority: rule.priority,
|
||||
error_message: 'SMS service not configured'
|
||||
});
|
||||
}
|
||||
|
||||
const twilioMessage = await this.twilioClient.messages.create({
|
||||
body: message,
|
||||
from: process.env.TWILIO_PHONE_NUMBER,
|
||||
to: phoneNumber
|
||||
});
|
||||
try {
|
||||
console.log(`📱 Sending SMS alert to ${phoneNumber}`);
|
||||
|
||||
const twilioMessage = await this.twilioClient.messages.create({
|
||||
body: message,
|
||||
from: this.twilioPhone,
|
||||
to: phoneNumber
|
||||
});
|
||||
|
||||
return await AlertLog.create({
|
||||
alert_rule_id: rule.id,
|
||||
detection_id: detection.id,
|
||||
alert_type: 'sms',
|
||||
recipient: phoneNumber,
|
||||
message: message,
|
||||
status: 'sent',
|
||||
sent_at: new Date(),
|
||||
external_id: twilioMessage.sid,
|
||||
priority: rule.priority
|
||||
});
|
||||
console.log(`✅ SMS sent successfully: ${twilioMessage.sid}`);
|
||||
|
||||
return await AlertLog.create({
|
||||
alert_rule_id: rule.id,
|
||||
detection_id: detection.id,
|
||||
alert_type: 'sms',
|
||||
recipient: phoneNumber,
|
||||
message: message,
|
||||
status: 'sent',
|
||||
sent_at: new Date(),
|
||||
external_id: twilioMessage.sid,
|
||||
priority: rule.priority
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to send SMS:', error.message);
|
||||
|
||||
return await AlertLog.create({
|
||||
alert_rule_id: rule.id,
|
||||
detection_id: detection.id,
|
||||
alert_type: 'sms',
|
||||
recipient: phoneNumber,
|
||||
message: message,
|
||||
status: 'failed',
|
||||
sent_at: new Date(),
|
||||
external_id: null,
|
||||
priority: rule.priority,
|
||||
error_message: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async sendEmailAlert(email, message, rule, detection) {
|
||||
|
||||
Reference in New Issue
Block a user