I am trying to utilize an Internal DNC List. Using the Python SDK, I have successfully created and added numbers to the list. I also built out a data action so that, when a patient replies with a qualifying response, their number is added to the DNC list. What I can't figure out is how to access said list programmatically. I am able to use the API endpoint, /api/v2/outbound/dnclists/{dncListId}/export, to both initiate the list export and obtain the associated export's uri. What I have not been able to do is get the actual list of phone numbers that have been added. I have tried using the get method of both the requests and urllib Python libraries, but cannot seem to figure out how to obtain anything but a mess of html. Below is the code I tried:
# auth = authenticated API Client
# DNC_LIST = ID of Internal DNC List
# Using the requests library
api = PureCloudPlatformClientV2.OutboundAPI(auth)
response = api.post_outbound_dnclist_export(DNC_LIST)
if response.id:
uri = api.get_outbound_dnclist_export(DNC_LIST).uri
r = request.get(uri)
with open("test.csv", "wb") as f:
f.write(r.content)
# Using the urllib library
api = PureCloudPlatformClientV2.OutboundAPI(auth)
response = api.post_outbound_dnclist_export(DNC_LIST)
if response.id:
uri = api.get_outbound_dnclist_export(DNC_LIST).uri
r = urllib.request.urlretrieve(uri, "test.csv")
I know I missing a step somewhere, but cannot for the life of me figure out what it is. Any help would be much appreciated.
Poll GET /api/v2/outbound/dnclists/{dncListId}/export including the ?download=false query param every few seconds to get the download URL. How long this will take is entirely dependent on the size of your DNC list.
Make an authorized API request (i.e. with an Authorization header) to the URI returned by the polling step. You'll have to construct this request manually because this response includes the full URI instead of an ID you can use with the SDK.
You're probably getting a "mess of HTML" because you're not setting the download query param to false and it's redirecting you to the download URL via a 301 response. Following that redirect takes you to the login screen because following the 301 redirect doesn't pass your authorization header/token along with it.
# auth is the authorized API Client
# DNC_LIST is your DNC List ID
# ENVIRONMENT is your genesys cloud environment, (e.g., usw2.pure.cloud, etc.)
# Initiate DNC List Export
api = PureCloudPlatformClientV2.OutboundApi(auth)
api.post_outbound_dnclist_export(DNC_LIST)
# Poll for DNC List URI
uri = None
while uri is None:
try:
uri = api.get_outbound_dnclist_export(DNC_LIST, download=False).uri
print('DNC URI obtained.')
except:
print('DNC URI not yet ready. Waiting...')
time.sleep(1)
# Build Authorized Request Header
authorization = base64.b64encode(
bytes(CLIENT_ID + ":" + CLIENT_SECRET, "ISO-8859-1")
).decode("ascii")
request_headers = {
"Authorization": f"Basic {authorization}",
"Content-Type": "application/x-www-form-urlencoded"
}
request_body = {
"grant_type": "client_credentials"
}
response = requests.post(f"https://login.{ENVIRONMENT}/oauth/token",
data=request_body, headers=request_headers)
response_json = response.json()
requestHeaders = {
"Authorization": f"{response_json['token_type']} \
{response_json['access_token']}"
}
# Pull the DNC List Export
response = requests.get(uri, headers=requestHeaders)
From there you can either save the results to a file or parse them for use right in the script.