From 4e3b7096e1c9a0504f19e4dd047dd88f2b694547 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Tue, 11 Jun 2024 14:40:33 -0600 Subject: [PATCH] Attempt to make CDR command --- commands.json | 19 +++++++++++++++++++ index.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/commands.json b/commands.json index 056b44e..5a7ed6e 100644 --- a/commands.json +++ b/commands.json @@ -134,6 +134,25 @@ } ] }, + { + "name": "cdr", + "description": "Get the call detail records for your extension", + "type": 1, + "options": [ + { + "name": "start_date", + "description": "The start date for the CDR", + "type": 3, + "required": false + }, + { + "name": "end_date", + "description": "The end date for the CDR", + "type": 3, + "required": false + } + ] + }, { "name": "Lookup Extension", "type": 2 diff --git a/index.js b/index.js index 0140b70..1614fe7 100644 --- a/index.js +++ b/index.js @@ -1301,6 +1301,51 @@ dcClient.on('interactionCreate', async interaction => { break; } break; + case "cdr": // Get CDR records for the user + // default to beginning of time + let startDate = interaction.options.get("start_date").value || new Date(0); + let endDate = interaction.options.get("end_date").value || new Date(); + await interaction.deferReply({ + ephemeral: true + }); + lookupExtension(interaction.user.id, "uid").then((result) => { + if (result.status == "exists") { + // The user has an ext, use the cdrdb to get the records + cdrPool.getConnection().then((conn) => { + conn.query(`SELECT * FROM cdr WHERE src = '${result.result.fetchExtension.user.extension}' AND calldate BETWEEN '${startDate.toISOString()}' AND '${endDate.toISOString()}'`).then((result) => { + if (result.length == 0) { + interaction.editReply({ + content: "No CDR records found", + ephemeral: true + }); + } else { + // Generate the CSV + const fields = ["calldate", "src", "dst", "duration", "disposition", "recordingfile"]; + const json2csvParser = new Json2csvParser({ + fields + }); + const csv = json2csvParser.parse(result); + const buffer = Buffer.from(csv, 'utf-8'); + const attachment = new MessageAttachment(buffer, 'cdr.csv'); + interaction.editReply({ + files: [attachment], + content: "Here are your CDR records" + }); + } + }).catch((error) => { + interaction.editReply(`Error getting CDR records: ${error}`); + }); + conn.end(); + } + } + }).catch((error) => { + // The user doesn't have an extension, return an ephemeral message saying so + interaction.editReply({ + content: "You don't have an extension!", + ephemeral: true + }); + }); + break; default: break; }