// >> START recordings-downloader-step-5
        public static void downloadAllRecordings(string dates)
        {
            Console.WriteLine("Start batch request process.");
            BatchDownloadJobStatusResult completedBatchStatus = new BatchDownloadJobStatusResult();

            // Process and build the request for downloading the recordings
            // Get the conversations within the date interval and start adding them to batch request
            AnalyticsConversationQueryResponse conversationDetails = conversationsApi.PostAnalyticsConversationsDetailsQuery(new ConversationQuery(Interval: dates));

            foreach (var conversations in conversationDetails.Conversations)
            {
                addConversationRecordingsToBatch(conversations.ConversationId);
            }

            // Send a batch request and start polling for updates
            BatchDownloadJobSubmissionResult result = recordingApi.PostRecordingBatchrequests(batchRequestBody);

            completedBatchStatus = getRecordingStatus(result);

            // Start downloading the recording files individually
            foreach (var recording in completedBatchStatus.Results)
            {
                downloadRecording(recording);
            }
        }
        /// <summary>
        /// Plot conversationId and recordingId to request for batchDownload Recordings
        /// </summary>
        /// <param name="iterateRecordings"></param>
        /// <returns></returns>
        private static void getSpecificRecordings(Recording iterateRecordings)
        {
            List <BatchDownloadRequest> batchRequest         = new List <BatchDownloadRequest>();
            BatchDownloadRequest        batchDownloadRequest = new BatchDownloadRequest(ConversationId: iterateRecordings.ConversationId, RecordingId: iterateRecordings.Id);

            batchRequest.Add(batchDownloadRequest);

            // Create the batch job with the request list
            BatchDownloadJobSubmission batchSubmission = new BatchDownloadJobSubmission(BatchDownloadRequestList: batchRequest);

            BatchDownloadJobSubmissionResult recordingBatchRequestId = new BatchDownloadJobSubmissionResult();
            RecordingApi recordingApi = new RecordingApi();

            recordingBatchRequestId = recordingApi.PostRecordingBatchrequests(batchSubmission);

            recordingStatus(recordingBatchRequestId);
        }
Beispiel #3
0
        /// <summary>
        /// Batch download recordings.
        /// Link: https://developer.mypurecloud.com/api/rest/v2/recording/#post-api-v2-recording-batchrequests
        /// </summary>
        /// <param name="conversationId">String, conversation id</param>
        /// <returns>BatchDownloadJobSubmissionResult, Job result object</returns>
        public async Task <BatchDownloadJobSubmissionResult> BatchRecordingDownloadByConversation(List <string> conversations)
        {
            BatchDownloadJobSubmissionResult result = new BatchDownloadJobSubmissionResult();

            using (HttpClient hc = new HttpClient())
            {
                hc.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Bearer {_token}");
                hc.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");

                HttpResponseMessage        responseMessage = new HttpResponseMessage();
                BatchDownloadJobSubmission queryParam      = new BatchDownloadJobSubmission
                {
                    BatchDownloadRequestList = conversations
                                               .Select(c => new BatchDownloadRequest()
                    {
                        ConversationId = c
                    }).ToList()
                };

                int tentatives = 0;
                do
                {
                    responseMessage = await hc.PostAsync(_uribase + "/api/v2/recording/batchrequests",
                                                         new StringContent(queryParam.ToJson(), Encoding.UTF8, "application/json"));

                    string jsonMessage = await responseMessage.Content.ReadAsStringAsync();

                    if (responseMessage.IsSuccessStatusCode)
                    {
                        result = JsonConvert.DeserializeObject <BatchDownloadJobSubmissionResult>(jsonMessage);
                    }
                    else if (responseMessage.StatusCode == HttpStatusCode.TooManyRequests)
                    {
                        await DelayTime(responseMessage);
                    }
                    else if ((int)responseMessage.StatusCode >= 300 && (int)responseMessage.StatusCode < 600)
                    {
                        throw new Exception(jsonMessage);
                    }

                    tentatives++;
                } while (!responseMessage.IsSuccessStatusCode && tentatives < 3);
            }

            return(result);
        }
        /// <summary>
        /// Check status of generating url for downloading, if the result is still unavailble. The function will be called again until the result is available.
        /// </summary>
        /// <param name="recordingBatchRequestId"></param>
        /// <returns></returns>
        private static void recordingStatus(BatchDownloadJobSubmissionResult recordingBatchRequestId)
        {
            BatchDownloadJobStatusResult getRecordingBatchRequestData = new BatchDownloadJobStatusResult();
            RecordingApi recordingApi = new RecordingApi();

            getRecordingBatchRequestData = recordingApi.GetRecordingBatchrequest(recordingBatchRequestId.Id);

            if (getRecordingBatchRequestData.ExpectedResultCount == getRecordingBatchRequestData.ResultCount)
            {
                // Pass the getRecordingBatchRequestData to getExtension function
                getExtension(getRecordingBatchRequestData);
            }
            else
            {
                Thread.Sleep(5000);
                recordingStatus(recordingBatchRequestId);
            }
        }
        // Plot conversationId and recordingId to request for batchdownload Recordings
        private static BatchDownloadJobStatusResult getRecordingStatus(BatchDownloadJobSubmissionResult recordingBatchRequest)
        {
            Console.WriteLine("Processing the recordings...");
            BatchDownloadJobStatusResult result = new BatchDownloadJobStatusResult();

            result = recordingApi.GetRecordingBatchrequest(recordingBatchRequest.Id);

            if (result.ExpectedResultCount != result.ResultCount)
            {
                Console.WriteLine("Batch Result Status:" + result.ResultCount + " / " + result.ExpectedResultCount);

                // Simple polling through recursion
                Thread.Sleep(5000);
                return(getRecordingStatus(recordingBatchRequest));
            }

            // Once result count reach expected.
            return(result);
        }
Beispiel #6
0
        public static async void Run(
            [EventHubTrigger("conversationhub", Connection = "EventhubConnectionString")] EventData[] events,
            [EventHub("conversationhub", Connection = "EventhubConnectionString")] IAsyncCollector <string> conversationhub,
            [ServiceBus("jobqueue", Connection = "ServiceBusConnectionString", EntityType = EntityType.Queue)] IAsyncCollector <string> jobQueue,
            ILogger log)
        {
            try
            {
                List <string> conversations = new List <string>();

                // TODO: get list of conversations from "eventHub"
                foreach (EventData eventData in events)
                {
                    string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                    conversations.Add(messageBody);
                    //await Task.Yield();
                }

                log.LogInformation($"Total conversations: {conversations.Count}");

                // TODO: batch job donwload
                PureCloudClient purecloudClient = new PureCloudClient();
                await purecloudClient.GetAccessToken();

                BatchDownloadJobSubmissionResult job = await purecloudClient.BatchRecordingDownloadByConversation(conversations);

                // TODO: add in "jobQueue"
                await jobQueue.AddAsync(job.Id);
            }
            catch (Exception ex)
            {
                TelemetryClient telemetry = new TelemetryClient();
                telemetry.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                telemetry.TrackException(ex);

                log.LogInformation($"Exception during execution: {ex.Message}");

                do
                {
                    try
                    {
                        await Task.Delay(Convert.ToInt32(Environment.GetEnvironmentVariable("deplaytime")));

                        //List<Task> taskList = new List<Task>(); // to mutch performatic kkkk :P
                        foreach (EventData eventData in events)
                        {
                            string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                            await conversationhub.AddAsync(messageBody);

                            log.LogInformation($"Readded conversation: {messageBody} to conversationhub");
                        }
                        //await Task.WhenAll(taskList); // to mutch performatic kkkk :P

                        break;
                    }
                    catch (Exception exEx)
                    {
                        telemetry.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                        telemetry.TrackException(exEx);

                        log.LogInformation($"Exception during execution: {exEx.Message}");
                    }
                } while (true);
            }
        }