example with jambonz api

This commit is contained in:
surajshivakumar
2024-06-19 15:09:54 -04:00
parent c073ab123f
commit 058fd88594
3 changed files with 98 additions and 8 deletions

View File

@@ -0,0 +1,90 @@
const { request } = require('undici');
const { pipeline } = require('stream');
const fs = require('fs');
const path = require('path');
const os = require('os');
const createAudioService = require('..');
const audioService = createAudioService();
async function downloadFile(url, tempFilePath, finalFilePath, token) {
try {
const { statusCode, body } = await request(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'Accept': '*/*'
}
});
if (statusCode !== 200) {
throw new Error(`Failed to download file: ${statusCode}`);
}
const tempWriteStream = fs.createWriteStream(tempFilePath);
pipeline(
body,
tempWriteStream,
async(err) => {
if (err) {
console.error('Pipeline failed:', err);
} else {
try {
await processAndRedactFile(tempFilePath, finalFilePath);
} catch (processingError) {
console.error('Error processing file:', processingError);
} finally {
// Clean up the temporary file
fs.unlink(tempFilePath, (err) => {
if (err) {
console.error('Error deleting temp file:', err);
} else {
console.log('Temporary file deleted.');
}
});
}
}
}
);
} catch (error) {
console.error('Error fetching file:', error);
}
}
// Function to process and redact the file
async function processAndRedactFile(audioFilePath, outputFilePath) {
const credentials = { 'vendor': 'deepgram', 'apiKey': process.env.DEEPGRAM_API_KEY };
try {
const transcriptionResults = await audioService.transcribe(credentials, audioFilePath);
console.log('Transcription Results:', transcriptionResults);
const timestampsToRedact = transcriptionResults.redactionTimestamps;
await audioService.redact({
credentials: credentials,
transcriptionData: timestampsToRedact,
audioPath: audioFilePath,
audioOutputPath: outputFilePath
});
console.log('File redacted and saved successfully.');
} catch (error) {
console.error('Failed to redact audio:', error);
}
}
const [,, callId, date, format] = process.argv;
// Construct the URL
const url = `https://jambonz.one/api/v1/Accounts/${process.env.ACCOUNT_SID}/RecentCalls/${callId}/record/${date}/${format}`;
// Define the path where the file will be saved
const finalFilePath = path.resolve(__dirname, path.join(process.env.OUTPUT_PATH, 'redacted_audio.wav'));
// Get the temporary folder from environment variable or use OS temp folder
const tempFolder = process.env.TEMP_FOLDER || os.tmpdir();
const tempFilePath = path.join(tempFolder, 'tempDownloadedFile.mp3');
// Call the function to download and process the file
downloadFile(url, tempFilePath, finalFilePath, process.env.JAMBONZ_API_TOKEN);

14
package-lock.json generated
View File

@@ -25,7 +25,7 @@
"google-protobuf": "^3.21.2",
"microsoft-cognitiveservices-speech-sdk": "1.36.0",
"openai": "^4.25.0",
"undici": "^6.18.2"
"undici": "^6.19.2"
},
"devDependencies": {
"config": "^3.3.10",
@@ -6383,9 +6383,9 @@
}
},
"node_modules/undici": {
"version": "6.18.2",
"resolved": "https://registry.npmjs.org/undici/-/undici-6.18.2.tgz",
"integrity": "sha512-o/MQLTwRm9IVhOqhZ0NQ9oXax1ygPjw6Vs+Vq/4QRjbOAC3B1GCHy7TYxxbExKlb7bzDRzt9vBWU6BDz0RFfYg==",
"version": "6.19.2",
"resolved": "https://registry.npmjs.org/undici/-/undici-6.19.2.tgz",
"integrity": "sha512-JfjKqIauur3Q6biAtHJ564e3bWa8VvT+7cSiOJHFbX4Erv6CLGDpg8z+Fmg/1OI/47RA+GI2QZaF48SSaLvyBA==",
"engines": {
"node": ">=18.17"
}
@@ -11503,9 +11503,9 @@
}
},
"undici": {
"version": "6.18.2",
"resolved": "https://registry.npmjs.org/undici/-/undici-6.18.2.tgz",
"integrity": "sha512-o/MQLTwRm9IVhOqhZ0NQ9oXax1ygPjw6Vs+Vq/4QRjbOAC3B1GCHy7TYxxbExKlb7bzDRzt9vBWU6BDz0RFfYg=="
"version": "6.19.2",
"resolved": "https://registry.npmjs.org/undici/-/undici-6.19.2.tgz",
"integrity": "sha512-JfjKqIauur3Q6biAtHJ564e3bWa8VvT+7cSiOJHFbX4Erv6CLGDpg8z+Fmg/1OI/47RA+GI2QZaF48SSaLvyBA=="
},
"undici-types": {
"version": "5.26.5",

View File

@@ -35,7 +35,7 @@
"google-protobuf": "^3.21.2",
"microsoft-cognitiveservices-speech-sdk": "1.36.0",
"openai": "^4.25.0",
"undici": "^6.18.2"
"undici": "^6.19.2"
},
"devDependencies": {
"config": "^3.3.10",