122 lines
3.1 KiB
JavaScript
122 lines
3.1 KiB
JavaScript
const express = require('express');
|
|
const { exec } = require('child_process');
|
|
|
|
require('dotenv').config();
|
|
|
|
const contexts = {
|
|
"A1": {
|
|
context: 'custom-emergency.9001.1',
|
|
timeout: 30000,
|
|
cid: 'Live Page'
|
|
},
|
|
"E1": {
|
|
context: 'custom-emergency.9002.1',
|
|
timeout: 30000,
|
|
cid: 'Emergency Live Page'
|
|
},
|
|
"A2": {
|
|
context: 'custom-emergency.9003.1',
|
|
timeout: 30000,
|
|
cid: 'Announcement 2',
|
|
number: '2000' // Direct to page group, no phone needed
|
|
},
|
|
"E2": {
|
|
context: 'custom-emergency.9004.1',
|
|
timeout: 30000,
|
|
cid: 'Emergency 2',
|
|
number: '2000' // Direct to page group, no phone needed
|
|
},
|
|
"A3": {
|
|
context: 'custom-emergency.9005.1',
|
|
timeout: 30000,
|
|
cid: 'Announcement 3',
|
|
number: '2000' // Direct to page group, no phone needed
|
|
},
|
|
"E3": {
|
|
context: 'custom-emergency.9006.1',
|
|
timeout: 30000,
|
|
cid: 'Emergency 3',
|
|
number: '2000' // Direct to page group, no phone needed
|
|
}
|
|
}
|
|
|
|
function trigCall(pageType, phone) {
|
|
// If contexts[pageType] does not exist, return an error
|
|
if (!contexts[pageType]) {
|
|
throw new Error(`Invalid page type: ${pageType}`);
|
|
}
|
|
const { context, timeout, cid, number } = contexts[pageType];
|
|
const targetNumber = number || phone;
|
|
if (!targetNumber) {
|
|
throw new Error(`Phone number is required for page type: ${pageType}`);
|
|
}
|
|
originateCall(targetNumber, context, 0, timeout, cid).then((output) => {
|
|
console.log(`Call originated: ${output}`);
|
|
}).catch((error) => {
|
|
console.error(`Error originating call: ${error}`);
|
|
});
|
|
return true;
|
|
}
|
|
|
|
function originateCall(number, context, delay, timeout, cid, variables = {}) {
|
|
// Build the base command
|
|
let command = `/usr/bin/ast_originate ${number} ${context} ${delay} ${timeout} ${Buffer.from(cid).toString('base64')}`;
|
|
|
|
// Add variables if provided
|
|
if (variables && typeof variables === 'object') {
|
|
const varString = Object.entries(variables)
|
|
.map(([key, value]) => `${key}=${value}`)
|
|
.join(' ');
|
|
if (varString) {
|
|
command += ` ${varString}`;
|
|
}
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
exec(command, (error, stdout, stderr) => {
|
|
if (error) {
|
|
reject(error);
|
|
} else {
|
|
resolve(stdout);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
const app = express();
|
|
const HOST = process.env.HOST || 'localhost';
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
app.use(express.json());
|
|
app.set('view engine', 'ejs');
|
|
app.set('views', './views');
|
|
app.use(express.static('static'));
|
|
app.use(express.urlencoded({ extended: true }));
|
|
app.use(require('express-session')({
|
|
secret: process.env.SESSION_SECRET || 'fallback-secret-key',
|
|
resave: false,
|
|
saveUninitialized: false,
|
|
cookie: { secure: false, maxAge: 24 * 60 * 60 * 1000 }
|
|
}));
|
|
|
|
function auth(req, res, next) {
|
|
if (!req.session || !req.session.authenticated) {
|
|
return res.redirect('/login');
|
|
}
|
|
next();
|
|
}
|
|
|
|
app.get('/', (req, res) => {
|
|
res.render('index', { session: req.session });
|
|
});
|
|
|
|
app.post('/trig', async (req, res) => {
|
|
console.log('Triggering call with data:', req.body);
|
|
trigCall(req.body.pageType, req.body.phone);
|
|
res.status(200).send('Call triggered');
|
|
});
|
|
|
|
app.listen(PORT, HOST, () => {
|
|
console.log(`Server running on http://${HOST}:${PORT}`);
|
|
}); |