class ZenWorker(object): def __init__(self, url, email, api, search_string): self.zendesk = Zendesk(url, zdesk_email=email, zdesk_api=api) self.barcode_re = re.compile(r"barcode\s(\d+)") self.username_re = re.compile(r"assigned_to\s(\w+)") self.search = search_string self.me = self.zendesk.users_me()['user'] def run(self): """ Iterates through the tickets found by SEARCH_STRING and """ try: result = self.zendesk.search(query=self.search, get_all_pages=True) except Exception as exc: logging.error("encountered an exception during a zendesk search: %s", str(exc)) return logging.debug("found %s tickets in the queue", str(result['count'])) tickets = {} # iterating over the tickets which haven't been touched for ticket in result['results']: try: comments = self.zendesk.ticket_comments(ticket_id=ticket['id']) except Exception as exc: logging.error("encountered an error %s while trying to fetch comments " "for ticket: %s", str(exc), str(ticket['id'])) # check if it's open and there's a cmment from a human if ticket['status'] == 'open' and comments.get("count", 0) > 1: #and #comments['comments'][-1].get('author_id') != self.me['id']): logging.info("ticket %s is open and has comments", str(ticket['id'])) continue # get the user, barcode and ticket id user_match = self.username_re.search(ticket['description']) barcode_match = self.barcode_re.search(ticket['description']) if not (user_match and barcode_match): continue # TODO: uncomment user = user_match.group(1) barcode = barcode_match.group(1) tickets[ticket['id']] = TicketData(user, barcode, ticket['status']) return tickets def update_ticket(self, ticket_id, message): """ Updates a ticket with given ticket_id with a message. """ data = { "ticket": { "id": ticket_id, "comment": { "public": True, "body": message } } } response = self.zendesk.ticket_update(ticket_id, data) logging.debug(response)
def zdeskNotify(self): room = "55a12b9bb82d04e3b232a99c" zendesk = Zendesk(**testconfig) results = zendesk.search(query='type:ticket sort:desc', page=1) for ticket in results['results']: if ticket['status'] == 'solved': #send message to ws #print "Hey man problem resolved" self.sayAnswer(room, "Андрей, мы нашли проблему и все исправили, пишите если опять будет регулярно повторяться проблема") ticket_id = ticket['id'] data = { "ticket": { "id": ticket_id, "status": "closed" } } response = zendesk.ticket_update(ticket_id, data) #self.sayAnswer(room, "ticket %s closed" % ticket_id) print "tiket %s closed" % ticket_id
"ticket": { "id": ticket_id, "comment": { "public": False, "body": commentbody } } } # I like to add this separately, because it's not an uncommon use case # to have an automated ticket update that may or may not have uploads. if upload_token != "": data['ticket']['comment']['uploads'] = [upload_token] # Post the comment to the ticket, which should reference the upload response = zendesk.ticket_update(ticket_id, data) # Delete zendesk.ticket_delete(id=ticket_id) ################################################################ # ORGANIZATIONS ################################################################ # List zendesk.organizations_list() # Create new_org = { 'organization': {
"ticket": { "id": ticket_id, "comment": { "public": False, "body": commentbody } } } # I like to add this separately, because it's not an uncommon use case # to have an automated ticket update that may or may not have uploads. if upload_token != "": data['ticket']['comment']['uploads'] = [upload_token] # Post the comment to the ticket, which should reference the upload response = zendesk.ticket_update(ticket_id, data) # Delete zendesk.ticket_delete(id=ticket_id) ################################################################ ## ORGANIZATIONS ################################################################ # List zendesk.organizations_list() # Create new_org = {'organization': {'name': 'Starbucks Corp'}} result = zendesk.organization_create(data=new_org) org_id = get_id_from_url(result)