Attempt to make CDR command
This commit is contained in:
parent
c7fcf1f79a
commit
4e3b7096e1
|
@ -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
|
||||
|
|
45
index.js
45
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue