This commit is contained in:
Christopher Cookman 2024-08-01 00:02:08 -06:00
parent 3cb5ab9a5e
commit 85d9b8aeb8
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42

View file

@ -77,30 +77,44 @@ function generateTransactionNumber() {
} }
function sendAlert(accountNumber, transaction, placeName, systemName, zoneNumber, zoneName, event) { function sendAlert(accountNumber, transaction, placeName, systemName, zoneNumber, zoneName, event) {
if (handledTransactions.includes(transaction)) { return new Promise((resolve, reject) => {
return; // Duplicate transaction if (handledTransactions.includes(transaction)) {
} resolve(); // 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}`);
})
})
})
} else { } else {
return; handledTransactions.push(transaction);
// Account does not exist or is not verified // 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) => { app.post("/api/v1/alert", (req, res) => {
console.log(req.body); console.log(req.body);
// send no content response // 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(); startTime = new Date();
client.login(process.env.DISCORD_TOKEN); client.login(process.env.DISCORD_TOKEN);