-
Notifications
You must be signed in to change notification settings - Fork 0
/
zendesk_client.py
98 lines (89 loc) · 3.42 KB
/
zendesk_client.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from typing import Dict, Any, List
from zenpy import Zenpy
from zenpy.lib.api_objects import Comment
class ZendeskClient:
def __init__(self, subdomain: str, email: str, token: str):
"""
Initialize the Zendesk client using zenpy lib.
"""
self.client = Zenpy(
subdomain=subdomain,
email=email,
token=token
)
def get_ticket(self, ticket_id: int) -> Dict[str, Any]:
"""
Query a ticket by its ID
"""
try:
ticket = self.client.tickets(id=ticket_id)
return {
'id': ticket.id,
'subject': ticket.subject,
'description': ticket.description,
'status': ticket.status,
'priority': ticket.priority,
'created_at': str(ticket.created_at),
'updated_at': str(ticket.updated_at),
'requester_id': ticket.requester_id,
'assignee_id': ticket.assignee_id,
'organization_id': ticket.organization_id
}
except Exception as e:
raise Exception(f"Failed to get ticket {ticket_id}: {str(e)}")
def get_ticket_comments(self, ticket_id: int) -> List[Dict[str, Any]]:
"""
Get all comments for a specific ticket.
"""
try:
comments = self.client.tickets.comments(ticket=ticket_id)
return [{
'id': comment.id,
'author_id': comment.author_id,
'body': comment.body,
'html_body': comment.html_body,
'public': comment.public,
'created_at': str(comment.created_at)
} for comment in comments]
except Exception as e:
raise Exception(f"Failed to get comments for ticket {ticket_id}: {str(e)}")
def create_ticket_comment(self, ticket_id: int, comment: str, public: bool = True) -> str:
"""
Create a comment for an existing ticket.
"""
try:
ticket = self.client.tickets(id=ticket_id)
ticket.comment = Comment(
html_body=comment,
public=public
)
self.client.tickets.update(ticket)
return comment
except Exception as e:
raise Exception(f"Failed to create comment on ticket {ticket_id}: {str(e)}")
def get_all_articles(self) -> Dict[str, Any]:
"""
Fetch help center articles as knowledge base.
Returns a Dict of section -> [article].
"""
try:
# Get all sections
sections = self.client.help_center.sections()
# Get articles for each section
kb = {}
for section in sections:
articles = self.client.help_center.sections.articles(section.id)
kb[section.name] = {
'section_id': section.id,
'description': section.description,
'articles': [{
'id': article.id,
'title': article.title,
'body': article.body,
'updated_at': str(article.updated_at),
'url': article.html_url
} for article in articles]
}
return kb
except Exception as e:
raise Exception(f"Failed to fetch knowledge base: {str(e)}")