CSvUc3812xQey7mAez7zAuto-Assign Zendesk Tickets between Indian and US Timings using email ID
Auto-Assign Zendesk Tickets between Indian and US Timings using email ID
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(cred_label)['creds']
ZENDESK_EMAIL = creds['username']
ZENDESK_TOKEN = creds['password']
# Function to get user ID by email
def get_user_id_by_email(subdomain, email, api_token, user_email):
api_url = f'https://{subdomain}.zendesk.com/api/v2/users/search.json?query={user_email}'
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:
print(f"User ID for {user_email}: {users[0]['id']}") # For debugging
return users[0]['id']
print(f"Failed to get user ID for {user_email}. Response code: {response.status_code}") # For debugging
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', {})
print(f"User name for ID {user_id}: {user.get('name', 'Unknown')}") # For debugging
return user.get('name', 'Unknown')
print(f"Failed to get user name for ID {user_id}. Response code: {response.status_code}") # For debugging
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)
print(f"Assigning ticket {ticket_id} to user ID {user_id}. Response code: {response.status_code}") # For debugging
print(f"Assign Ticket Response: {response.json()}") # For debugging
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 = 2 #2UTC # 7:30 AM IST
end_ist_hour = 14 #14UTC # 7:30 PM IST
if start_ist_hour <= current_hour_utc < end_ist_hour:
# Indian timings: Randomly choose between two users
selected_user = random.choice([user_ids['yash'], user_ids['yash_test']])
print(f"Indian working hours. Randomly selected user ID: {selected_user}") # For debugging
return selected_user
else:
# US timings: Assign to sarang_another
print(f"US working hours. Selected user ID: {user_ids['sarang']}") # For debugging
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_by_email(subdomain, email, api_token, 'email@example.com'),
'yash': get_user_id_by_email(subdomain, email, api_token, 'email1@example.com'),
'yash_test': get_user_id_by_email(subdomain, email, api_token, 'email2@example.com')
}
print(f"Fetched User IDs: {user_ids}") # For debugging
# Determine current UTC hour
current_hour_utc = datetime.datetime.utcnow().hour
print(f"Current UTC Hour: {current_hour_utc}") # For debugging
# 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
email = ZENDESK_EMAIL
api_token = ZENDESK_TOKEN
ticket_id = ticket['id'] # Ensure `ticket` is defined appropriately in your context
print(f"Ticket ID being processed for Auto Assignment: {ticket_id}")
# Assign the ticket
assign_ticket(subdomain, email, api_token, ticket_id)
context.proceed = False
copied