diff --git a/index.js b/index.js index 3e23da9..2b7c394 100644 --- a/index.js +++ b/index.js @@ -77,30 +77,44 @@ function generateTransactionNumber() { } function sendAlert(accountNumber, transaction, placeName, systemName, zoneNumber, zoneName, event) { - if (handledTransactions.includes(transaction)) { - return; // Duplicate transaction - } - handledTransactions.push(transaction); - // Check if the account exists and is verified - db.get("SELECT * FROM accounts WHERE id = ? AND verified = 1", accountNumber, (err, row) => { - if (err) { - console.error(err); - } else if (row) { - // Account exists and is verified - // Send the alert - runCommand(`flite -t "Hello. This is an automated call from KCA SecuriNet Monitoring. ${systemName} has reported an ${event}, ZONE ${zoneNumber}, ${zoneName}, at ${placeName}" -o /tmp/${transaction}.wav`).then((output) => { - runCommand(`ffmpeg -y -i /tmp/${transaction}.wav -ar 8000 -ac 1 -c:a pcm_s16le /tmp/${transaction}-alert.wav`).then(() => { - runCommand(`rm /tmp/${transaction}.wav`) - // strip extension from filename - - runCommand(`/var/lib/asterisk/bin/originate ${row.phone} roblox.s.1 0 0 /tmp/${transaction}-alert "IktDQSBTZWN1cmlOZXQiIDw+"`).then(() => { - console.log(`Alert sent to ${row.phone}`); - }) - }) - }) + return new Promise((resolve, reject) => { + if (handledTransactions.includes(transaction)) { + resolve(); // Duplicate transaction } else { - return; - // Account does not exist or is not verified + handledTransactions.push(transaction); + // Check if the account exists and is verified + db.get("SELECT * FROM accounts WHERE id = ? AND verified = 1", accountNumber, (err, row) => { + if (err) { + console.error(err); + reject(err); + } else if (row) { + // Account exists and is verified + // Send the alert + runCommand(`flite -t "Hello. This is an automated call from KCA SecuriNet Monitoring. ${systemName} has reported an ${event}, ZONE ${zoneNumber}, ${zoneName}, at ${placeName}" -o /tmp/${transaction}.wav`).then((output) => { + runCommand(`ffmpeg -y -i /tmp/${transaction}.wav -ar 8000 -ac 1 -c:a pcm_s16le /tmp/${transaction}-alert.wav`).then(() => { + runCommand(`rm /tmp/${transaction}.wav`) + // strip extension from filename + + runCommand(`/var/lib/asterisk/bin/originate ${row.phone} roblox.s.1 0 0 /tmp/${transaction}-alert "IktDQSBTZWN1cmlOZXQiIDw+"`).then(() => { + console.log(`Alert sent to ${row.phone}`); + resolve(); + }).catch((error) => { + console.error(error); + reject(error); + }); + }).catch((error) => { + console.error(error); + reject(error); + }); + }).catch((error) => { + console.error(error); + reject(error); + }); + } else { + resolve(); + // Account does not exist or is not verified + } + }); } }); } @@ -301,9 +315,13 @@ client.on("interactionCreate", async (interaction) => { app.post("/api/v1/alert", (req, res) => { console.log(req.body); // send no content response - res.sendStatus(204); + sendAlert(req.body.accountNumber, req.body.transaction, req.body.placeName, req.body.systemName, req.body.zoneNumber, req.body.zoneName, req.body.event).then(() => { + res.status(204).send(); + }).catch((error) => { + res.status(500).send(); + }); }) -sendAlert("6371787150", generateTransactionNumber(), "KCA Product Showcase", "Building Security", 1, "Front Door", "alarm"); +// sendAlert("6371787150", generateTransactionNumber(), "KCA Product Showcase", "Building Security", 1, "Front Door", "alarm"); startTime = new Date(); client.login(process.env.DISCORD_TOKEN); \ No newline at end of file