Add demo API
This commit is contained in:
parent
f01f4654ee
commit
51ea95c709
53
index.js
53
index.js
|
@ -76,6 +76,47 @@ function generateTransactionNumber() {
|
|||
return Math.floor(Math.random() * 10000000000);
|
||||
}
|
||||
|
||||
function sendDemo(accountNumber, transaction, placeName, systemName, zoneNumber, zoneName, event) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (handledTransactions.includes(transaction)) {
|
||||
resolve(); // Duplicate transaction
|
||||
} else {
|
||||
handledTransactions.push(transaction);
|
||||
// Check if the account exists and is verified
|
||||
// Account exists and is verified
|
||||
// Send the alert
|
||||
runCommand(`flite -t "Hello. This is an automated call from KCA SecuriNet Monitoring. ${systemName} has reported a ${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
|
||||
|
||||
client.channels.cache.get("1269078717552922745").send({
|
||||
embeds: [{
|
||||
title: "Demo Alert",
|
||||
description: `Place: ${placeName}\nSystem: ${systemName}\nZone: ${zoneNumber} - ${zoneName}\nEvent: ${event}`
|
||||
}],
|
||||
files: [{
|
||||
attachment: `/tmp/${transaction}-alert.wav`,
|
||||
name: `${transaction}-alert.wav`
|
||||
}]
|
||||
}).then(() => {
|
||||
resolve();
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
reject(error);
|
||||
});
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
reject(error);
|
||||
});
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
reject(error);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function sendAlert(accountNumber, transaction, placeName, systemName, zoneNumber, zoneName, event) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (handledTransactions.includes(transaction)) {
|
||||
|
@ -149,6 +190,7 @@ function generatePhoneCode() {
|
|||
}
|
||||
|
||||
client.on("ready", async () => {
|
||||
sendDemo("6371787150", generateTransactionNumber(), "KCA Product Showcase", "Building Security", 1, "Front Door", "alarm");
|
||||
console.log(`${colors.cyan("[Discord]")} Logged in as ${client.user.tag}`);
|
||||
|
||||
const commands = require("./commands.json");
|
||||
|
@ -325,12 +367,22 @@ app.post("/api/v1/alert", (req, res) => { // Legacy alert endpoint
|
|||
app.post("/api/v1/webhook/:brand/:accountNumber", (req, res) => {
|
||||
switch (req.params.brand) {
|
||||
case "kca":
|
||||
if (req.params.accountNumber == "DEMOTEST") {
|
||||
// Generate the audio files, then post it to discord
|
||||
sendDemo(req.params.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();
|
||||
});
|
||||
} else {
|
||||
|
||||
// send alert to accountNumber
|
||||
sendAlert(req.params.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();
|
||||
});
|
||||
}
|
||||
break;
|
||||
default:
|
||||
res.status(400).send("Brand not found");
|
||||
|
@ -338,6 +390,5 @@ app.post("/api/v1/webhook/:brand/:accountNumber", (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
// sendAlert("6371787150", generateTransactionNumber(), "KCA Product Showcase", "Building Security", 1, "Front Door", "alarm");
|
||||
startTime = new Date();
|
||||
client.login(process.env.DISCORD_TOKEN);
|
Loading…
Reference in a new issue