From fdd761ba0ab8027c76ffefeaefcd984081b3d704 Mon Sep 17 00:00:00 2001 From: C'tri Date: Wed, 16 Mar 2022 16:00:48 +0000 Subject: [PATCH 1/3] Update freshdesk.py migrated get_worklogs into library migrated get_comments and renamed get_worklogs to fetch added new params to freshdeskTicket --- freshdesk.py | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 3 deletions(-) diff --git a/freshdesk.py b/freshdesk.py index 2b76137..29abe57 100644 --- a/freshdesk.py +++ b/freshdesk.py @@ -60,7 +60,7 @@ def _get_default_headers(self) -> dict: return {'Authorization':AuthString, 'Content-Type':'application/json'} - def get_worklogs(self,ticketID:str): + def fetch_worklogs(self,ticketID:str): returnObj = [] getNextPage = True oldResponse = "" @@ -95,7 +95,7 @@ def get_worklogs(self,ticketID:str): lo.debug(worklogs) return list(worklogs.values()) - def get_comments(self,ticket:str) -> list: + def fetch_comments(self,ticket:str) -> list: #'https://domain.freshdesk.com/api/v2/tickets/1/conversations?page=2' getNextPage = True @@ -138,6 +138,88 @@ def get_fd_tickets_updated_on(self,targetdate:str): "expects a string in the format %Y-%m-%d" return self.search_fd_tickets(f"updated_at:\'{targetdate}\'") + def get_worklogs(self,ticketID): + #/api/v2/tickets/[id]/time_entries + #returns all tickets associated with me, or unassigned + + returnObj = [] + getNextPage = True + oldResponse = "" + currentPage = 0 + while getNextPage == True: + currentPage = currentPage + 1 + + + url = 'https://%s/api/v2/tickets/%s/time_entries' % (self.host,ticketID) + AuthString = "Basic %s" % (self.api_key) + r = requests.get ( + url, + headers = {'Authorization':AuthString, + 'Content-Type':'application/json'} + ) + + if r.status_code != 200: + logging.warn("Unexpected response code [%s], whilst getting worklogs for %s" % (r.status_code,ticketID)) + print(r.content) + return + + + if r.content == oldResponse: + getNextPage = False + else : + oldResponse = r.content + + + response = json.loads(r.content) + + for worklog in response: + returnObj.append(worklog) + + + + return returnObj + + def get_comments(self,ticket): + #'https://domain.freshdesk.com/api/v2/tickets/1/conversations?page=2' + getNextPage = True + oldResponse = "" + + currentPage = 0 + messages = [] + while getNextPage == True: + currentPage = currentPage + 1 + url = 'https://%s/api/v2/tickets/%s/conversations?page=%s' % (cfg.FreshdeskURL,ticket,currentPage) + AuthString = "Basic %s" % (self.api_key) + try: + r = requests.get ( + url, + headers = {'Authorization':AuthString, + 'Content-Type':'application/json'} + ) + conversation = json.loads(r.content) + if len(conversation) < 30: + getNextPage = False + except Exception as e: + return [] + + if r.status_code != 200: + print("Unexpected response code [%s], whilst getting ticket list" % r.status_code) + print(r.content) + return + if r.content == oldResponse: + getNextPage = False + + else : + oldResponse = r.content + maybeJSON = r.content + for message in conversation: + messages.append(message) + return messages + + + + + class FreshdeskTicket(): def __init__(self) -> None: @@ -147,7 +229,10 @@ def __init__(self) -> None: self.priority = 0 self.subject = "" self.description = "" - + self.type = "" + self.custom_fields = {} + + def from_dict(self,new_params:dict) -> None: self.status = new_params["status"] if "status" in new_params else self.status self.id = new_params["id"] if "id" in new_params else self.id @@ -155,5 +240,7 @@ def from_dict(self,new_params:dict) -> None: self.priority = new_params["priority"] if "priority" in new_params else self.priority self.subject = new_params["subject"] if "subject" in new_params else self.subject self.description = new_params["description_text"] if "description_text" in new_params else self.description + self.type = new_params["type"] if "type" in new_params else self.type + self.custom_fields = new_params["custom_fields"] if "custom_fields" in new_params else self.custom_fields \ No newline at end of file From 56434b82345af16009679b9043d8a29b4711b355 Mon Sep 17 00:00:00 2001 From: C'tri Date: Wed, 16 Mar 2022 16:57:48 +0000 Subject: [PATCH 2/3] pruned and updated --- zendesk.py | 103 +++++++---------------------------------------------- 1 file changed, 13 insertions(+), 90 deletions(-) diff --git a/zendesk.py b/zendesk.py index 3699ef8..56aed72 100644 --- a/zendesk.py +++ b/zendesk.py @@ -8,24 +8,21 @@ -cfg = cfg.getCfgHelper() + lo = logging.getLogger("ZendeskMapper") -class ZendeskMapper: - def __init__(self): - self.ticketObj = [] - self.trelloCards = [] - self._URL = cfg.zendeskURL - self._headers = {"Authorization" : "Basic %s" % (cfg.zendeskKey)} - self._customFieldID = cfg.trelloCustomFieldIDforZD - self._labelID = cfg.trelloLabelforZDcards - self._myAssigneeID = cfg.zendeskAgentID +class zendesk: + def __init__(self, host,api_key): + + self.host = host + self.key = api_key + self._headers = {"Authorization" : "Basic %s" % (self.key)} + - def _getTickets(self): #gets the most recently 100 Zendesk tickets. - url = str.format("{0}/api/v2/search.json?query=form%3A360001936712&sort_by=created_at&sort_order=desc", self._URL) + def search_for_tickets(self,search_string): #gets the most recently 100 Zendesk tickets. + url = str.format("https://{0}/api/v2/search.json?query={1}", self.host,search_string) response = requests.get(url,headers=self._headers) - responseObj = {} if response.status_code != 200: lo.warn("couldn't retrieve zendesk tickets [%s]\n%s" % (response.status_code, response.content)) @@ -34,83 +31,9 @@ def _getTickets(self): #gets the most recently 100 Zendesk tickets. except: lo.warn("Couldn't parse zendesk tickets as json! \n%s" % (response.content)) return [] - for ticket in tickets: #tickets are now indexed by ID - if ticket["status"] in ["new","open","pending"]: - if ticket["assignee_id"] == self._myAssigneeID or ticket["assignee_id"] is None: - responseObj["%s" % (ticket["id"])] = ticket - return responseObj - - - def executeMap(self): - lo.info("beginning zendesk map") - self.trelloCards = self._getTrelloCards() - self.ticketObj = self._getTickets() - self._linkTicketsToCards() - self._createCardsForUnlinked() - lo.info("completed zendesk map, existing cards[%i] tickets[%i]",len(self.trelloCards),len(self.ticketObj)) - - def executePurge(self,level = 0 ): - """level 0 = only purge ones in the inbox \n - level 1 = purge all of the ones in the board""" - targetLists = "*" if level == 1 else [cfg.trelloListForNewCards] - targetFields = [cfg.trelloCustomFieldIDforZD,] - - TrelloHelper.purgeTrelloCards(targetLists=targetLists,customFieldIDs=targetFields) - return - + return tickets - def _linkTicketsToCards(self): - for cardID in self.trelloCards: - card = self.trelloCards[cardID] - - - - for field in card["customFieldItems"]: - if field["idCustomField"] == self._customFieldID: #this is a ZD card - self.trelloCards[card["id"]] = card #now you can do trelloCards[ID] - try: - ticket = self.ticketObj[field["value"]["text"]] - ticket["TrelloCardID"] = card["id"] - except: - #couldn't find matching ticket - the trello card is obsolete if still in untriaged inbox. - TrelloHelper.maybeRemoveTrelloCard(card) - - - def _createCardsForUnlinked(self): - for ticket in self.ticketObj.values(): - ticketID = ticket["id"] - if 'TrelloCardID' not in ticket.keys(): - if ticket["assignee_id"] is not None and ticket["assignee_id"] > 0: - emoji = "🔥" - else: - emoji = "👋" - - cardName = 'ZD#%s %s - %s' % (ticketID,emoji, ticket["subject"]) - cardDesc = "[Link to ticket](%s/agent/tickets/%s)" %(self._URL,ticketID) - - cardObj = TrelloHelper.makeTrelloCard(cardName, description = cardDesc, labelID = self._labelID, zendeskCustomFieldValue= "%s" % (ticketID)) - ticket = self._linkIDs(ticket,cardObj) - pass - - def _linkIDs(self,ticket,TrelloCard): - try: - ticket["TrelloCardID"] = TrelloCard["id"] - except: - pass - return ticket - - def _getTrelloCards(self): - cardObj = {} - cards = TrelloHelper.getTrelloCards() - for card in cards: - cardObj[card["id"]] = card - return cardObj - - def getAssignedUserID(self,ticketID): + def get_user_from_id(self,ticketID): """helper method to get the userID assigned to a ticket. Helpful for when the user doesn't know their own ID""" - - -if __name__ == "__main__": - zdm = ZendeskMapper() - zdm.executeMap() \ No newline at end of file + raise NotImplementedError \ No newline at end of file From c5726593d002bce4022a0767f98926c3ee7c0c3f Mon Sep 17 00:00:00 2001 From: C'tri Date: Wed, 16 Mar 2022 17:01:24 +0000 Subject: [PATCH 3/3] merged changes --- freshdesk.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/freshdesk.py b/freshdesk.py index 037dbfc..29abe57 100644 --- a/freshdesk.py +++ b/freshdesk.py @@ -150,11 +150,7 @@ def get_worklogs(self,ticketID): currentPage = currentPage + 1 -<<<<<<< HEAD - url = 'https://%s/api/v2/tickets/%s/time_entries' % (cfg.FreshdeskURL,ticketID) -======= url = 'https://%s/api/v2/tickets/%s/time_entries' % (self.host,ticketID) ->>>>>>> 56434b82345af16009679b9043d8a29b4711b355 AuthString = "Basic %s" % (self.api_key) r = requests.get ( url,