-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzendesk_service.py
More file actions
72 lines (63 loc) · 2.21 KB
/
Copy pathzendesk_service.py
File metadata and controls
72 lines (63 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import requests
import base64
def get_auth_header():
email = os.environ.get("ZENDESK_EMAIL")
token = os.environ.get("ZENDESK_TOKEN")
if not email or not token:
return None
# Zendesk uses email/token:token format for API token authentication
auth_str = f"{email}/token:{token}"
encoded_auth = base64.b64encode(auth_str.encode('ascii')).decode('ascii')
return {
"Authorization": f"Basic {encoded_auth}",
"Content-Type": "application/json"
}
def get_zendesk_url(path):
subdomain = os.environ.get("ZENDESK_SUBDOMAIN")
if not subdomain:
return None
return f"https://{subdomain}.zendesk.com/api/v2{path}"
def create_ticket(subject, comment_body, requester_name=None, requester_email=None):
"""
Creates a new ticket in Zendesk.
"""
url = get_zendesk_url("/tickets.json")
headers = get_auth_header()
if not url or not headers:
return {"error": "Zendesk configuration missing (email, token, or subdomain)"}
payload = {
"ticket": {
"subject": subject,
"comment": {
"body": comment_body
}
}
}
if requester_name and requester_email:
payload["ticket"]["requester"] = {
"name": requester_name,
"email": requester_email
}
try:
response = requests.post(url, json=payload, headers=headers, timeout=30)
if response.status_code >= 400:
return {"error": f"Zendesk API Error {response.status_code}: {response.text}"}
return {"status": "success", "ticket": response.json()}
except Exception as e:
return {"error": str(e)}
def get_users():
"""
Retrieves a list of users from Zendesk.
"""
url = get_zendesk_url("/users.json")
headers = get_auth_header()
if not url or not headers:
return {"error": "Zendesk configuration missing"}
try:
response = requests.get(url, headers=headers, timeout=30)
if response.status_code >= 400:
return {"error": f"Zendesk API Error {response.status_code}: {response.text}"}
return {"status": "success", "users": response.json()}
except Exception as e:
return {"error": str(e)}