swap it around so it looks for the wav instead of the txt, it takes longest to write lol

This commit is contained in:
Christopher Cookman 2023-10-01 17:55:46 -06:00
parent 84d4c64ec0
commit 9df1f44fb5
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42

View file

@ -95,43 +95,43 @@ const watcher = chokidar.watch(config.freepbx.voicemaildir, {
}); });
watched = []; watched = [];
watcher.on("add", async (filePath, stats) => { watcher.on("add", async (filePath, stats) => {
if (startup) return; if (startup) return;
// if the file is already being watched, ignore it, this stops spam from the watcher at startup if (watched.includes(filePath)) return;
if (watched.includes(filePath)) return; watched.push(filePath);
watched.push(filePath); let filename = pathSplit(filePath)[pathSplit(filePath).length - 1];
// extract file name from path if (!filename.endsWith("wav")) return;
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];
let mailbox = pathSplit(filePath)[pathSplit(filePath).length - 2]; if (mailbox !== "INBOX") return;
if (mailbox !== "INBOX") return; // ignore anything that isn't in the inbox
vmData = fpbxFuncs.parseVoicemailInfo(fs.readFileSync(filePath, "utf8")) let txtFile = filePath.replace(".wav", ".txt");
// get the extension info from the origmailbox if (!fs.existsSync(txtFile)) return;
let extData = await lookupExtension(vmData.origmailbox, "ext").catch((error) => { vmData = fpbxFuncs.parseVoicemailInfo(fs.readFileSync(txtFile, "utf8"));
console.log(error);
}); let extData = await lookupExtension(vmData.origmailbox, "ext").catch((error) => {
if(!extData) return; // The extension doesnt have a discord ID set, ignore it console.log(error);
let discordId = extData.result.fetchVoiceMail.email; });
let discordUser = await client.users.fetch(discordId).catch((error) => { if (!extData) return;
console.log(error);
}); let discordId = extData.result.fetchVoiceMail.email;
// parse callerid "john doe" <1234> into just john doe and 1234 let discordUser = await client.users.fetch(discordId).catch((error) => {
let callerid = vmData.callerid; console.log(error);
let calleridName = callerid.split(" <")[0].replaceAll("\"", ""); });
let calleridNumber = callerid.split(" <")[1].replace(">", "");
// format the callerid let callerid = vmData.callerid;
vmData.callerid = `${calleridName} (${calleridNumber})`; let calleridName = callerid.split(" <")[0].replaceAll("\"", "");
// get the voicemail file (.wav) let calleridNumber = callerid.split(" <")[1].replace(">", "");
let vmFile = filePath.replace(".txt", ".wav"); vmData.callerid = `${calleridName} (${calleridNumber})`;
await discordUser.send({ await discordUser.send({
content: `:mailbox_with_mail: New voicemail from ${vmData.callerid}!`, content: `:mailbox_with_mail: New voicemail from ${vmData.callerid}!`,
files: [{ files: [{
attachment: vmFile, attachment: filePath,
name: `voicemail.wav` name: `voicemail.wav`
}] }]
}).catch((error) => { }).catch((error) => {
console.log(`Could not send voicemail to ${discordUser.tag}, probably because they have DMs disabled\n${error}`); console.log(`Could not send voicemail to ${discordUser.tag}, probably because they have DMs disabled\n${error}`);
}) })
}); });
// Setup Discord bot // Setup Discord bot