Last active
July 11, 2023 13:59
-
-
Save matdave/98cf117b1b2a37eff84e063ca630228d to your computer and use it in GitHub Desktop.
Pipedream Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import os | |
from pipedream.script_helpers import (steps, export) | |
from datetime import timedelta, datetime | |
from dotenv import load_dotenv | |
load_dotenv() | |
# Zendesk API credentials | |
ZENDESK_SUBDOMAIN = os.getenv("ZENDESK_SUBDOMAIN") | |
ZENDESK_TOKEN = os.getenv("ZENDESK_TOKEN") | |
ZENDESK_ORGANIZATION_ID = os.getenv("ZENDESK_ORGANIZATION_ID") | |
ZENDESK_ASSIGNEE = os.getenv("ZENDESK_ASSIGNEE") | |
# Get Zendesk assigned tickets | |
def get_zendesk_assigned_tickets(): | |
zendesk = get_zendesk(f"search?query=type:ticket assignee:{ZENDESK_ASSIGNEE} status<hold") | |
tickets = zendesk['results'] | |
return tickets | |
# Get Zendesk CC'd tickets | |
def get_zendesk_cc_tickets(): | |
zendesk = get_zendesk(f"search?query=type:ticket -assignee:{ZENDESK_ASSIGNEE} cc:{ZENDESK_ASSIGNEE} status<hold") | |
tickets = zendesk['results'] | |
return tickets | |
def get_zendesk_organization(organization_id): | |
print(organization_id) | |
if organization_id == '' or organization_id == None: | |
return {'name' : ''} | |
zendesk = get_zendesk(f"organizations/{organization_id}") | |
organization = zendesk['organization'] | |
return organization | |
def get_zendesk(endpoint): | |
zendesk_url = f"https://{ZENDESK_SUBDOMAIN}.zendesk.com/api/v2/{endpoint}" | |
zendesk_headers = { | |
'Content-Type': 'application/json', | |
'Authorization': 'Basic ' + ZENDESK_TOKEN | |
} | |
response = requests.get(zendesk_url, headers=zendesk_headers) | |
response.raise_for_status() | |
return response.json() | |
# Main function | |
def main(): | |
message = "" | |
tickets = get_zendesk_assigned_tickets() | |
for ticket in tickets: | |
organization = get_zendesk_organization(ticket['organization_id']) | |
ticket_date = datetime.strptime(ticket['updated_at'], '%Y-%m-%dT%H:%M:%SZ') | |
message += f"{ticket_date.strftime('%Y-%m-%d')} {organization['name']} - {ticket['subject']} - https://{ZENDESK_SUBDOMAIN}.zendesk.com/agent/tickets/{ticket['id']}\n" | |
tickets = get_zendesk_cc_tickets() | |
for ticket in tickets: | |
organization = get_zendesk_organization(ticket['organization_id']) | |
ticket_date = datetime.strptime(ticket['updated_at'], '%Y-%m-%dT%H:%M:%SZ') | |
message += f"{ticket_date.strftime('%Y-%m-%d')} {organization['name']} - {ticket['subject']} - https://{ZENDESK_SUBDOMAIN}.zendesk.com/agent/tickets/{ticket['id']}\n" | |
print(message) | |
export("message", message) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm using this in pipedream, hence the closing export.