Recent searches


No recent searches

Interact with Zendesk Data using zenpy in Python - An Example



Posted Jul 26, 2023

To extract data from Zendesk, we are going to use zenpy, a Python wrapper for the Zendesk API. This example uses zenpy to extract the first 1000 comments from all Zendesk tickets.  To extract more comments, use adjust the number in the counter.

Ref: Python Zenpy Examples, zenpy.Zenpy Python Examples - HotExamples

Ref: Zenpy — Zenpy 2.13 documentation (facetoe.com.au)

python

pip install zenpy
import csv
import logging
from zenpy import Zenpy

# Setup logging
logging.basicConfig(filename='error_log.log', level=logging.ERROR, 
                    format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)

# Setup Zenpy
creds = {
    'email' : 'your_zendesk_email/token',
    'token' : 'your_token_or_password',
    'subdomain': 'your_zendesk_subdomain'
}

try:
    zenpy_client = Zenpy(**creds)
except Exception as e:
    logger.error(f"Zenpy setup error: {e}")

# Extract ticket comments
try:
    tickets = zenpy_client.tickets()
    ticket_comments = []

    counter = 0
    for ticket in tickets:
        for comment in zenpy_client.tickets.comments(ticket=ticket):
            ticket_comments.append([ticket.id, comment.id, comment.type, comment.author_id, comment.created_at, comment.body, comment.public])
            counter += 1
            if counter >= 1000:
                break
        if counter >= 1000:
            break
except Exception as e:
    logger.error(f"Error extracting ticket comments: {e}")

# Transform to CSV
try:
    with open('ticket_comments.csv', 'w', newline='', encoding='utf-8') as file:
        writer = csv.writer(file)
        writer.writerow(["ticket_id", "comment_id", "type", "author_id", "created_at","body", "public"])
        writer.writerows(ticket_comments)
except Exception as e:
    logger.error(f"Error writing to CSV: {e}")

Daniel Moore | LinkedIn


1

0

0 comments

Please sign in to leave a comment.

Didn't find what you're looking for?

New post