Ok so, file upload doesnt work yet, gonna test here

This commit is contained in:
Christopher Cookman 2023-10-01 17:31:17 -06:00
parent 98179d9d7d
commit 8fceb4300d
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42
2 changed files with 59 additions and 28 deletions

View file

@ -76,6 +76,20 @@ const minifyQuery = (query) => {
return query.replace(/\s+/g, ' ').trim(); return query.replace(/\s+/g, ' ').trim();
} }
const parseVoicemailInfo = (data) => {
// split by new line
var lines = data.split("\n");
// make a json object, all remaining lines are key=value
var voicemail = {};
lines.forEach((line) => {
var split = line.split("=");
voicemail[split[0]] = split[1];
});
// remove any undefined values
Object.keys(voicemail).forEach((key) => (voicemail[key] == undefined) && delete voicemail[key]);
return voicemail;
}
module.exports = { module.exports = {
generateQuery, generateQuery,
minifyQuery, minifyQuery,
@ -93,5 +107,6 @@ module.exports = {
return true; return true;
break; break;
} }
} },
parseVoicemailInfo
} }

View file

@ -1,6 +1,11 @@
const config = require("./config.json"); const config = require("./config.json");
const fs = require("fs"); const fs = require("fs");
const fpbxFuncs = require("./fpbxFuncs.js"); const fpbxFuncs = require("./fpbxFuncs.js");
const path = require("path");
function pathSplit(p) {
return p.split("/");
}
var startup = true;
const lookupExtension = (ident, type) => { // type is either "ext" or "uid" const lookupExtension = (ident, type) => { // type is either "ext" or "uid"
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -89,35 +94,46 @@ const watcher = chokidar.watch(config.freepbx.voicemaildir, {
persistent: true persistent: true
}); });
watched = []; watched = [];
watcher.on("add", async (event, path) => { watcher.on("add", async (filePath, stats) => {
if (startup) return;
// if the file is already being watched, ignore it, this stops spam from the watcher at startup // if the file is already being watched, ignore it, this stops spam from the watcher at startup
if (watched.includes(path)) return; if (watched.includes(filePath)) return;
watched.push(path); watched.push(filePath);
// extract file name from path
// extract file name from path, needs to be node 16 compatible, path.split is not let filename = pathSplit(filePath)[pathSplit(filePath).length - 1];
let filename = path.substring(path.lastIndexOf("/") + 1); if (!filename.endsWith("txt")) return; // ignore anything that isn't a txt file (voicemail info file), we can get other file names based on the name without the extension
let mailbox = pathSplit(filePath)[pathSplit(filePath).length - 2];
// extract mailbox from path (path looks like /var/spool/asterisk/voicemail/default/1000/INBOX/file.wav), needs to be node 16 compatible, path.split is not
let mailbox = path.substring(path.lastIndexOf("/", path.lastIndexOf("/") - 1) + 1, path.lastIndexOf("/"));
if (mailbox !== "INBOX") return; // ignore anything that isn't in the inbox if (mailbox !== "INBOX") return; // ignore anything that isn't in the inbox
// if its a txt file (voicemail info), open it and get relavent info vmData = fpbxFuncs.parseVoicemailInfo(fs.readFileSync(filePath, "utf8"))
// make a json object with the callerid, duration, origdate, and origmailbox from the txt file // get the extension info from the origmailbox
if (filename.endsWith(".txt")) { let extData = await lookupExtension(vmData.origmailbox, "ext").catch((error) => {
let file = fs.readFileSync(path, "utf8"); console.log(error);
let lines = file.split("\n"); });
let callerid = lines[9].split("=")[1].replace(/"/g, ""); let discordId = extData.result.fetchVoiceMail.email;
let duration = lines[17].split("=")[1]; let discordUser = await client.users.fetch(discordId).catch((error) => {
let origdate = lines[11].split("=")[1]; console.log(error);
let origmailbox = lines[2].split("=")[1]; });
let message = { // get the voicemail file (.wav)
"callerid": callerid, let vmFile = filePath.replace(".txt", ".wav");
"duration": duration, // get buffer from voicemail wav
"origdate": origdate, let vmBuffer = fs.readFileSync(vmFile);
"origmailbox": origmailbox await discordUser.send(`:mailbox_with_mail: New voicemail from ${vmData.callerid}!`, {
} files: [{
// get the extension info from the callerid attachment: vmBuffer,
let ext = await lookupExtension(callerid, "uid"); name: `${vmData.callerid}.wav`
console.log(`New voicemail from ${message.callerid} (${message.duration}s)`); }]
} }).catch((error) => {
console.log(`Could not send voicemail to ${discordUser.tag}, probably because they have DMs disabled`);
})
});
// Setup Discord bot
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
startup = false;
});
client.login(config.discord.token).catch((error) => {
console.log(error);
}); });