agent: |
YnxD9BNoFucYNNvCy1cWAuto-Assign Zendesk Tickets between Indian and US Timings
Auto-Assign Zendesk Tickets between Indian and US Timings
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
inputs
outputs
import requests
import json
from requests.auth import HTTPBasicAuth
import datetime
import random
# Credentials for Zendesk API
creds = _get_creds(zendesk_cred_label)['creds']
ZENDESK_EMAIL = creds['username']
ZENDESK_TOKEN = creds['password']
# Function to get user ID by name
def get_user_id(subdomain, email, api_token, user_name):
api_url = f'https://{subdomain}.zendesk.com/api/v2/users/search.json?query={user_name}'
headers = {
'Content-Type': 'application/json'
}
basic_auth = HTTPBasicAuth(f'{email}/token', api_token)
response = requests.get(api_url, auth=basic_auth, headers=headers)
if response.status_code == 200:
users = response.json().get('users', [])
if users:
return users[0]['id']
return None
# Function to get user name by ID
def get_user_name_by_id(subdomain, email, api_token, user_id):
api_url = f'https://{subdomain}.zendesk.com/api/v2/users/{user_id}.json'
headers = {
'Content-Type': 'application/json'
}
basic_auth = HTTPBasicAuth(f'{email}/token', api_token)
response = requests.get(api_url, auth=basic_auth, headers=headers)
if response.status_code == 200:
user = response.json().get('user', {})
return user.get('name', 'Unknown')
return 'Unknown'
# Function to assign ticket to a user
def assign_ticket_to_user(subdomain, email, api_token, ticket_id, user_id):
api_url = f'https://{subdomain}.zendesk.com/api/v2/tickets/{ticket_id}.json'
headers = {
'Content-Type': 'application/json'
}
ticket_data = {
'ticket': {
'assignee_id': user_id
}
}
basic_auth = HTTPBasicAuth(f'{email}/token', api_token)
response = requests.put(api_url, auth=basic_auth, headers=headers, json=ticket_data)
return response.status_code, response.json()
# Function to determine the user to assign based on current time
def get_user_to_assign(current_hour_utc, user_ids):
# Define Indian working hours in UTC (IST is UTC+5:30)
start_ist_hour = 0 # 5:30 AM IST
end_ist_hour = 14 # 7:30 PM IST
if start_ist_hour <= current_hour_utc < end_ist_hour:
# Indian timings: Randomly choose between two users
return random.choice([user_ids['yash'], user_ids['yash_test']])
else:
# US timings: Assign to Sarang
return user_ids['sarang']
# Main function to assign a ticket
def assign_ticket(subdomain, email, api_token, ticket_id):
# Fetch user IDs dynamically
user_ids = {
'sarang': get_user_id(subdomain, email, api_token, 'Sarang Dharmapurikar'),
'yash': get_user_id(subdomain, email, api_token, 'Yash Yadav'),
'yash_test': get_user_id(subdomain, email, api_token, 'Yash Test User')
}
# Determine current UTC hour
current_hour_utc = datetime.datetime.utcnow().hour
# Determine user to assign based on time
user_to_assign = get_user_to_assign(current_hour_utc, user_ids)
# Assign the ticket to the determined user
status_code, response_json = assign_ticket_to_user(subdomain, email, api_token, ticket_id, user_to_assign)
print("Ticket Assignment Response:", json.dumps(response_json, indent=4)) # Comment out this line for raw response for assigning tickets
# Fetch the updated ticket details
ticket_details = response_json.get('ticket', {})
# Fetch the assignee name
assignee_name = get_user_name_by_id(subdomain, email, api_token, user_to_assign)
# Print relevant details
print("Ticket Assignment Status Code:", status_code)
print("Ticket ID:", ticket_details.get('id'))
print("Subject:", ticket_details.get('subject'))
print("Description:", ticket_details.get('description'))
print("Status:", ticket_details.get('status'))
print("Updated At:", ticket_details.get('updated_at'))
print("Assignee ID:", ticket_details.get('assignee_id'))
print("Assignee Name:", assignee_name)
# Usage
# subdomain and zendesk_cred_label to be received from upstream task
email = ZENDESK_EMAIL
api_token = ZENDESK_TOKEN
ticket_id = ticket['id']
print(f"Ticket ID being processed for Auto Assignment: {ticket_id}")
# Assign the ticket
assign_ticket(subdomain, email, api_token, ticket_id)
copied