How to delete recordings that Delete recordings that is associated with a specific queue by recording bulk job in Python SDK

Hello,

I want to delete call recordings using bulk delete by python SDK.
Also as conditions, I want to delete only recordings that is associated with a specific queue.
However, I am amateur of Python program, it does not work. Here is my python codes.

import base64, sys, requests, time
import PureCloudPlatformClientV2
from pprint import pprint
from PureCloudPlatformClientV2.rest import ApiException

print('-------------------------------------------------------------')
print('- Execute Bulk Action on recordings-')
print('-------------------------------------------------------------')

# OAuth when using Client Credentials
client_id = 'xxxxxx'
client_secret = 'xxxxxx'

region = PureCloudPlatformClientV2.PureCloudRegionHosts.ap_northeast_1
PureCloudPlatformClientV2.configuration.host = region.get_api_host()

# Authenticate client
api_client = PureCloudPlatformClientV2.api_client.ApiClient().get_client_credentials_token(client_id, client_secret)

# Get the api
recording_api = PureCloudPlatformClientV2.RecordingApi(api_client)

access_token = recording_api.api_client.access_token

# Assign the token
PureCloudPlatformClientV2.configuration.access_token = access_token

# Build the create job query, for export action, set query.action = "EXPORT"
############################################################################
#segment_filters list definition

segment_filters = PureCloudPlatformClientV2.SegmentDetailQueryFilter()
segment_filters_predicates = PureCloudPlatformClientV2.SegmentDetailQueryPredicate()
segment_filters_clause = PureCloudPlatformClientV2.SegmentDetailQueryClause()
segment_filters.type = "and"
segment_filters_clause.type = "and"
segment_filters_predicates.type = "dimention"
segment_filters_predicates.dimention = "queueId"
segment_filters_predicates.operator = "matches"
segment_filters_predicates.value= "bce7bb8a-b2e5-4454-ab06-a1cc3a450eeb"

############################################################################
query = PureCloudPlatformClientV2.RecordingJobsQuery()
query.action = "DELETE"
query.action_date = "2022-02-07T23:00:00.000Z"
#query.integration_id = ""
query.conversation_query = {
    "interval": "2021-01-04T09:00:00.000Z/2021-01-09T23:00:00.000Z",
    "order": "asc",
    "orderBy": "conversationStart",
    "segment_filters": segment_filters,
    "segment_filters_clause":segment_filters_clause,
    "segment_filters_predicates":segment_filters_predicates
}
print(query)
try:
    # Call create_recording_job api
    create_job_response = recording_api.post_recording_jobs(query)
    job_id = create_job_response.id
    print(f"Succesfully created recording bulk job { create_job_response}")
    print(job_id)
except ApiException as e:
    print(f"Exception when calling RecordingApi->post_recording_jobs: { e }")
    sys.exit()

# Call get_recording_job api
while True:
    try:
        get_recording_job_response = recording_api.get_recording_job(job_id)
        job_state = get_recording_job_response.state
        if job_state != 'PENDING':
            break
        else:
            time.sleep(2)
    except ApiException as e:
        print(f"Exception when calling RecordingApi->get_recording_job: { e }")
        sys.exit()

if job_state == 'READY':
    try:
        execute_job_response = recording_api.put_recording_job(job_id, { "state": "PROCESSING"})
        print(f"Succesfully execute recording bulk job { execute_job_response}")
    except ApiException as e:
        print(f"Exception when calling RecordingApi->put_recording_job: { e }")
        sys.exit()
else:
    print(f"Expected Job State is: READY, however actual Job State is: { job_state }")

# Call delete_recording_job api
# Can be canceled also in READY and PENDING states
if job_state == 'PROCESSING':
    try:
        cancel_job_response = recording_api.delete_recording_job(job_id)
        print(f"Succesfully cancel recording bulk job { execute_job_response}")
    except ApiException as e:
        print(f"Exception when calling RecordingApi->delete_recording_job: { e }")
        sys.exit()

try:
    get_recording_jobs_response = recording_api.get_recording_jobs({
        "page_size": 25,
        "page_number": 1,
        "sort_by": "userId",  # or "dateCreated"
        "state": "READY",  # valid values FULFILLED, PENDING, READY, PROCESSING, CANCELLED, FAILED
        "show_only_my_jobs": True,
        "job_type": "DELETE",  # or "DELETE"
    })
    print(f"Succesfully get recording bulk jobs { execute_job_response}")
except ApiException as e:
    print(f"Exception when calling RecordingApi->get_recording_jobs: { e }")
    sys.exit()

I think generated JSON request is bad, it does not work because bad coding of "PureCloudPlatformClientV2.SegmentDetailQueryClause()".
I don't know how to write the code for Segmentqueryfilters.
I hope someone please help me out what problem is.

Sorry for my bad english.
Thank you.

Hi,

I would suggest using the analytics query builder and API explorer to firstly build the correct query. When you have the query built you can then write the corresponding Python code to match it.
Every python model can be printed as JSON using its to_json() method and you can compare this to your manually generated query.

Hi, Thank you very much for response. I understood. First I must use API explorer for coding. I will try these tools first.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.