ry14 | 2023-07-06 04:30:50 UTC | #1
Hi,
I am trying to export old recordings to AWS S3. I am using BELOW URL for steps: Download Recordings (genesys.cloud)
Getting error--SyntaxError Identifier 'platformClient' has already been declared
Code: // Import built in libraries needed. const http = require('https'); const fs = require('fs');
let batchRequestBody = { batchDownloadRequestList: [] };
// Set Genesys Cloud objects const platformClient = require('purecloud-platform-client-v2'); const client = platformClient.ApiClient.instance;
// Create API instances const conversationsApi = new platformClient.ConversationsApi(); const recordingApi = new platformClient.RecordingApi();
// Get client credentials from environment variables const CLIENTID = process.env.GENESYSCLOUDCLIENTID; const CLIENTSECRET = process.env.GENESYSCLOUDCLIENTSECRET; const ORGREGION = process.env.GENESYSCLOUDREGION; // eg. useast_1
// Set environment const environment = platformClient.PureCloudRegionHosts[ORG_REGION]; if(environment) client.setEnvironment(environment);
// OAuth input client.loginClientCredentialsGrant(CLIENTID, CLIENTSECRET)
.then(() => { let dates = "2021-03-09T13:00:00.000Z/2021-03-10T00:00:00.000Z"; downloadAllRecordings(dates); })
.catch((err) => { // Handle failure response console.log(err); });
// Process and build the request for downloading the recordings // Get the conversations within the date interval and start adding them to batch request function downloadAllRecordings (dates) { console.log('Start batch request process');
let body = { interval: dates }; // Object | query
conversationsApi.postAnalyticsConversationsDetailsQuery(body) .then((conversationDetails) => { let conversationDetail = []; for (conversations of conversationDetails.conversations) { conversationDetail.push(addConversationRecordingsToBatch(conversations.conversationId)); } return Promise.all(conversationDetail); }) // Send a batch request and start polling for updates .then(() => { return recordingApi.postRecordingBatchrequests(batchRequestBody); }) // Start downloading the recording files individually .then((result) => { return getRecordingStatus(result); }) .then((completedBatchStatus) => { for (recording of completedBatchStatus.results) { // If there is an errorMsg skip the recording download if (recording.errorMsg) { console.log("Skipping this recording. Reason: " + recording.errorMsg) continue; } else { downloadRecording(recording); } } }) .catch((err) => { console.log('There was an error: '); console.error(err); }); }
// Get all the recordings metadata of the conversation and add it to the global batch request object function addConversationRecordingsToBatch (conversationId) { return recordingApi.getConversationRecordingmetadata(conversationId) .then((recordingsData) => { // Iterate through every result, check if there are one or more recordingIds in every conversation for (recording of recordingsData) { let batchRequest = {}; batchRequest.conversationId = recording.conversationId; batchRequest.recordingId = recording.id; batchRequestBody.batchDownloadRequestList.push(batchRequest); console.log('Added ' + recording.conversationId + ' to batch request'); } }) .catch((err) => { console.log('There was a failure calling getConversationRecordingmetadata'); console.error(err); }); }
// Plot conversationId and recordingId to request for batchdownload Recordings function getRecordingStatus (recordingBatchRequest) { return new Promise((resolve, reject) => { let recursiveRequest = () => { recordingApi.getRecordingBatchrequest(recordingBatchRequest.id) .then((result) => { if (result.expectedResultCount !== result.resultCount) { console.log('Batch Result Status:' + result.resultCount + '/' + result.expectedResultCount)
// Simple polling through recursion setTimeout(() => recursiveRequest(), 5000); } else { // Once result count reach expected. resolve(result); } }) .catch((err) => { console.log('There was a failure calling getRecordingBatchrequest'); console.error(err); reject(err); }); }; recursiveRequest(); }); }
// Get extension of every recording function getExtension (recording) { // Store the contentType to a variable that will be used later to determine the extension of recordings let contentType = recording.contentType; // Split the text and gets the extension that will be used for the recording let ext = contentType.split('/').slice(-1); ext = String(ext);
// For the JSON special case if (ext.length >= 4) { console.log('length' + ext.length); ext = ext.substring(0, 4); return ext; } else { return ext; } }
// Download Recordings function downloadRecording (recording) { console.log('Downloading now. Please wait...'); let ext = getExtension(recording); let conversationId = recording.conversationId; let recordingId = recording.recordingId; let sourceURL = recording.resultUrl; let targetDirectory = '.'; let fileName = conversationId + '_' + recordingId;
const file = fs.createWriteStream((targetDirectory + fileName + '.' + ext)); http.get(sourceURL, function (response) { response.pipe(file); }); }
Declan_ginty | 2023-07-10 17:21:34 UTC | #2
Hi,
I am not able to replicate the issue using the code from the article and I can't see anywhere in your code where platformClient is being redeclared. Searching for the word platformClient in your code should help you find where it is being redeclared.
Regards, Declan
ry14 | 2023-07-11 02:02:43 UTC | #3
Hi,
Thank you for response. I am using the code given in below link and unable to find it too where it is already declared. PFA.
Bulk delete\export of recordings (genesys.cloud)
Declan_ginty | 2023-07-11 16:18:16 UTC | #4
Hi,
So the reason you are getting that error is because the code editor you are using already injects that value for you. However, that code editor is no longer supported and will soon disappear without anymore notice without a replacement so we strongly advise you stop using it. See the announcement about the removal of legacy developer tools for more details here https://developer.genesys.cloud/forum/t/removal-of-legacy-developer-tools/18367
Regards, Declan
ry14 | 2023-07-12 01:46:41 UTC | #5
What should I use now for code edit to do the needful?
Declan_ginty | 2023-07-12 08:24:10 UTC | #6
Hi,
As said in the announcement about the https://developer.genesys.cloud/forum/t/removal-of-legacy-developer-tools/18367, Users wishing to use a web-based JavaScript editor should use one of the many 3rd party services offering various runtimes, frameworks, and environments, such as https://codepen.io (not an official Genesys endorsement). You can also install Node JS locally on your machine and a code editor like VS Code if possible for you.
Regards, Declan
system | 2023-08-12 08:24:48 UTC | #7
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 20860