const express = require('express'); const { exec } = require('child_process'); require('dotenv').config(); const phonesCfg = require('./phones.json'); const buttonsCfg = require('./buttons.json'); const contexts = {}; // Generate contexts from buttonsCfg Object.keys(buttonsCfg).forEach(category => { buttonsCfg[category].forEach(button => { if (button.name && button.context) { contexts[button.name] = { context: button.context.context, timeout: button.context.timeout, cid: button.context.cid, ...(button.context.dial && { dial: button.context.dial }) }; } }); }); console.log('Generated contexts:', contexts); 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, dial } = contexts[pageType]; const targetNumber = dial || 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, phones: phonesCfg, buttons: require("./buttons.json") }); }); app.get('/login', (req, res) => { res.render('login', { 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.post('/stop', async (req, res) => { console.log('Stopping page for phone:', req.body.phone); // Logic to stop the page would go here. // For now we will just log it, as the specific asterisk command to hangup a channel depends on implementation details not provided. // Typically it might involve 'asterisk -rx "channel request hangup "' or similar via AMI. // Assuming we might want to run a command similar to originate but for hangup if we knew the channel. // Since we don't have the channel ID easily available without tracking it, we might need to implement channel tracking or use a broad command. // Example placeholder: // exec(`/usr/sbin/asterisk -rx "channel request hangup all"`, (error, stdout, stderr) => { ... }); res.status(200).send('Stop request received'); }); app.listen(PORT, HOST, () => { console.log(`Server running on http://${HOST}:${PORT}`); });