const express = require('express'); const { exec } = require('child_process'); require('dotenv').config(); const phonesCfg = require('./phones.json'); const buttonsCfg = require('./buttons.json'); const fs = require('fs'); const path = require('path'); if (!fs.existsSync(path.join(__dirname, 'slingtoken.txt'))) { // Create empty file fs.writeFileSync(path.join(__dirname, 'slingtoken.txt'), '', 'utf8'); console.warn('slingtoken.txt file created. Please add your Sling token to this file to use sling messaging!'); } var slingToken = fs.readFileSync(path.join(__dirname, 'slingtoken.txt'), 'utf8').trim(); 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 }), ...(button.sling_chat_id && { sling_chat_id: button.sling_chat_id }), ...(button.sling_chat_message && { sling_chat_message: button.sling_chat_message }) }; } }); }); //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, sling_chat_id, sling_chat_message } = contexts[pageType]; const targetNumber = dial || phone; if (!targetNumber) { throw new Error(`Phone number is required for page type: ${pageType}`); } // Slink chat notification if (sling_chat_id && sling_chat_message) { fetch(`https://api.getsling.com/v1/conversations/${sling_chat_id}/messages`, { method: 'POST', headers: { 'Authorization': `${slingToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ 'content': sling_chat_message }) }).then(res => res.json()).then(data => { if (data && data.success) { console.log('Sling chat message sent successfully.'); } else { console.error('Error sending Sling chat message:', data); } }); } 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); } }); }); } // Sling chat stuff; Check current token with GET https://api.getsling.com/v1/account/session, refresh with POST https://api.getsling.com/v1/account/session (GET NEW TOKEN FROM RETURN AUTHORIZATION HEADER) async function slingAuthLoop() { console.log("Start slingAuthLoop") if (!slingToken) { console.warn('No Sling token provided in environment variables.'); return; } setTimeout(slingAuthLoop, 15 * 60 * 1000); // Refresh every 15 minutes const sessionCheck = await fetch('https://api.getsling.com/v1/account/session', { method: 'GET', headers: { 'Authorization': `${slingToken}` } }); console.log(sessionCheck) if (sessionCheck && sessionCheck.ok) { console.log('Sling session is valid. Refreshing token...'); const sessionRefresh = await fetch('https://api.getsling.com/v1/account/session', { method: 'POST', headers: { 'Authorization': `${slingToken}` } }); const newToken = sessionRefresh.headers.get('Authorization'); if (newToken) { slingToken = newToken; fs.writeFileSync(path.join(__dirname, 'slingtoken.txt'), newToken, 'utf8'); console.log('Sling token refreshed.'); } } else { console.error('Sling session is invalid. Please get a new token manually!'); } } slingAuthLoop(); 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. exec(`/usr/bin/ast_drop ${process.env.PAGE_GROUP || '9000'}`, (error, stdout, stderr) => { if (error) { console.error(`Error stopping page: ${error}`); return res.status(500).send('Error stopping page'); } console.log(`Page stopped: ${stdout}`); }); res.status(200).send('Stop request received'); }); app.listen(PORT, HOST, () => { console.log(`Server running on http://${HOST}:${PORT}`); });