From 8fceb4300d34b2687f7c0b739348c963515c68ca Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Sun, 1 Oct 2023 17:31:17 -0600 Subject: [PATCH] Ok so, file upload doesnt work yet, gonna test here --- fpbxFuncs.js | 17 ++++++++++++- index.js | 70 ++++++++++++++++++++++++++++++++-------------------- 2 files changed, 59 insertions(+), 28 deletions(-) diff --git a/fpbxFuncs.js b/fpbxFuncs.js index 1ace440..ce13baf 100644 --- a/fpbxFuncs.js +++ b/fpbxFuncs.js @@ -76,6 +76,20 @@ const minifyQuery = (query) => { 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 = { generateQuery, minifyQuery, @@ -93,5 +107,6 @@ module.exports = { return true; break; } - } + }, + parseVoicemailInfo } diff --git a/index.js b/index.js index de3ce10..175e132 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,11 @@ const config = require("./config.json"); const fs = require("fs"); 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" return new Promise((resolve, reject) => { @@ -89,35 +94,46 @@ const watcher = chokidar.watch(config.freepbx.voicemaildir, { persistent: true }); 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 (watched.includes(path)) return; - watched.push(path); + if (watched.includes(filePath)) return; + watched.push(filePath); + // extract file name from path + let filename = pathSplit(filePath)[pathSplit(filePath).length - 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]; + if (mailbox !== "INBOX") return; // ignore anything that isn't in the inbox + vmData = fpbxFuncs.parseVoicemailInfo(fs.readFileSync(filePath, "utf8")) + // get the extension info from the origmailbox + let extData = await lookupExtension(vmData.origmailbox, "ext").catch((error) => { + console.log(error); + }); + let discordId = extData.result.fetchVoiceMail.email; + let discordUser = await client.users.fetch(discordId).catch((error) => { + console.log(error); + }); + // get the voicemail file (.wav) + let vmFile = filePath.replace(".txt", ".wav"); + // get buffer from voicemail wav + let vmBuffer = fs.readFileSync(vmFile); + await discordUser.send(`:mailbox_with_mail: New voicemail from ${vmData.callerid}!`, { + files: [{ + attachment: vmBuffer, + name: `${vmData.callerid}.wav` + }] + }).catch((error) => { + console.log(`Could not send voicemail to ${discordUser.tag}, probably because they have DMs disabled`); + }) +}); - // extract file name from path, needs to be node 16 compatible, path.split is not - let filename = path.substring(path.lastIndexOf("/") + 1); +// Setup Discord bot +client.on("ready", () => { + console.log(`Logged in as ${client.user.tag}!`); + startup = false; +}); - // 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 its a txt file (voicemail info), open it and get relavent info - // make a json object with the callerid, duration, origdate, and origmailbox from the txt file - if (filename.endsWith(".txt")) { - let file = fs.readFileSync(path, "utf8"); - let lines = file.split("\n"); - let callerid = lines[9].split("=")[1].replace(/"/g, ""); - let duration = lines[17].split("=")[1]; - let origdate = lines[11].split("=")[1]; - let origmailbox = lines[2].split("=")[1]; - let message = { - "callerid": callerid, - "duration": duration, - "origdate": origdate, - "origmailbox": origmailbox - } - // get the extension info from the callerid - let ext = await lookupExtension(callerid, "uid"); - console.log(`New voicemail from ${message.callerid} (${message.duration}s)`); - } +client.login(config.discord.token).catch((error) => { + console.log(error); }); \ No newline at end of file