Recent searches


No recent searches

Incorrect Ticket ID is being output when trying to map ticket_metrics and tickets API



Posted Apr 23, 2024

I am trying to write a script that will outputs the following:

 

if group_id == 6177821616923

 

output ticket_id, created_at, reply_time_in_minutes, on_hold_time_in_minutes

 

What's happening is that each time my script outputs, it outputs the same ticket ID for each iteration. I am sure this is something small. I am trying to map these API's together based on the ticket ID. Open to suggestions on how to make this better and more efficient. 

 


import requests


# Constants
AUTH = 'andrew.e@coinmetrics.io/token', 'my_key'
HEADERS = {"Content-Type": "application/json"}


def cse_tickets(ticket_id):

    url = "https://coinmetricshelp.zendesk.com/api/v2/tickets/"

    response = requests.get(url, auth=AUTH, headers=HEADERS)

    if response.status_code == 200:
        data = response.json()
        tickets = data.get('tickets')

        for ticket in tickets:

            if ticket['group_id'] == 6177821616923:
                ticket_id = ticket.get('id')
                return ticket_id
            else:
                return None


def first_response_time():


    url = "https://coinmetricshelp.zendesk.com/api/v2/ticket_metrics"

    response = requests.get(url, auth=AUTH, headers=HEADERS)

    if response.status_code == 200:
        data = response.json()
        tickets = data.get('ticket_metrics')

        for ticket in tickets:
            ticket_id = ticket.get('ticket_id')
            created_at = ticket.get('created_at')
            first_reply = ticket.get("reply_time_in_minutes")['calendar']
            on_hold = ticket.get("on_hold_time_in_minutes")['calendar']

            cse = cse_tickets(ticket_id)

            print(f"Ticket ID: {cse} Created At: {created_at} First Reply: {first_reply}")


first_response_time()

 

An example of the output is below:

 

Ticket ID: 534 Created At: 2024-04-22T13:06:12Z First Reply: None
Ticket ID: 534 Created At: 2024-04-19T13:36:18Z First Reply: 0
Ticket ID: 534 Created At: 2024-04-19T13:12:33Z First Reply: 0
Ticket ID: 534 Created At: 2024-04-17T13:04:42Z First Reply: 1
Ticket ID: 534 Created At: 2024-04-17T12:27:31Z First Reply: 1197

0

3

3 comments

After playing around with this further, I may have spotted the issue. 

if ticket['group_id'] == 6177821616923:
                return ticket_id

 

import requests
from datetime import datetime

# Constants
AUTH = 'andrew.english@coinmetrics.io/token', ''
HEADERS = {"Content-Type": "application/json"}


def cse_tickets(ticket_id):

    url = f"https://coinmetricshelp.zendesk.com/api/v2/tickets/"

    response = requests.get(url, auth=AUTH, headers=HEADERS)

    if response.status_code == 200:
        data = response.json()
        tickets = data.get('tickets')

        for ticket in tickets:

            if ticket['group_id'] == 6177821616923:
                return ticket_id
            else:
                return None
                
                
                

 

However, I encountered another issue. I only want tickets that are part of group id 6177821616923. It is including tickets that are not part of this group. Is the logic correct?

 

0


Hi Andrew,

The “api/v2/tickets/” endpoint returns the first page with 100 entries from all tickets. You then loop over the list and return the first ticket that matches the group id.

 

Try using the following to get information for a particular ticket since you already have the ID.

GET /api/v2/tickets/{ticket_id}

import requests
from datetime import datetime

# Constants
AUTH = 'andrew.english@coinmetrics.io/token', ''
HEADERS = {"Content-Type": "application/json"}


def cse_tickets(ticket_id):

    url = f"https://coinmetricshelp.zendesk.com/api/v2/tickets/{ticket_id}"

    response = requests.get(url, auth=AUTH, headers=HEADERS)

    if response.status_code == 200:
        data = response.json()

        if data['ticket']['group_id'] == 6177821616923:
            return ticket_id
        else:
            return None

 

I would also add a check if the ticket taken from the metrics endpoint is in the required group to avoid printing empty lines.

……
cse = cse_tickets(ticket_id)

if cse:
    print(f"Ticket ID: {cse} Created At: {created_at} First Reply: {first_reply}")

 

Also note that using the “api/v2/ticket_metrics” endpoint without pagination might result with only 100 tickets out of all.
Read about pagination here:
https://developer.zendesk.com/api-reference/introduction/pagination/

Ned

0


Hi Ned, 

Thanks for the response. Your amendment has helped me. 

I've also added pagination. 

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post