pythonotrs

Read Articles in PyOTRS


I am wondering if anyone is able to read the articles within Tickets of an OTRS system via pyOTRS? I am able to connect and get tickets fine, I just cannot find out how to get the content of the tickets. I have been up and down the PyOTRS documentation but I am stuck. Does anyone have anything they can share with regards to reading articles?


Solution

  • The pre-requisites for PyOTRS are mentioned here: https://pypi.org/project/PyOTRS/. Once these are complete, following steps can be taken for retrieving OTRS ticket data:

    1. A connection is initiated by creating a client.
    2. OTRS ticket search is conducted.
    3. OTRS Ticket data is retrieved including dynamic fields and articles using get_ticket.to_dct() response.
    from pyotrs import Article, Client, Ticket, DynamicField, Attachment
    
    # Initializing
    URL = config["url"]
    USERNAME = config["username"]
    PASSWORD = config["password"]
    TICKET_LINK = config["ticketLink"]
    
    # Create session
    def createSession():
        client = Client(URL, USERNAME, PASSWORD, https_verify=False)
        client.session_create()
        return client
    
    # Retrieve tickets based on condition
    def ticketData(client):
        # Specify ticket search condition
        data = client.ticket_search(Queues=['queue1Name', 'queue2Name'], States=['open'])
        print("Number of tickets retrieved:" + str(len(data)))
        
        # Iterating over all search results
        if data[0] is not u'':
            for ticket_id in data: 
                # Get ticket details
                get_ticket = client.ticket_get_by_id(ticket_id, articles=1, attachments=1)
                print(get_ticket)
                q1 = "Ticket id: " + str(get_ticket.field_get("TicketID")) + "\nTicket number: " + str(get_ticket.field_get("TicketNumber")) + "\nTicket Creation Time: " + str(get_ticket.field_get("Created")) + "\n\nTicket title: " + get_ticket.field_get("Title")
                print(q1)
                
                # Based on to_dct() response we can access dynamic field (list) and article values
                print(get_ticket.to_dct())
                
                # Accessing dynamic field values
                dynamicField3 = get_ticket.to_dct()["Ticket"]["DynamicField"][3]["Value"]
                dynamicField12 = get_ticket.to_dct()["Ticket"]["DynamicField"][12]["Value"]
                
                # Accessing articles
                article = get_ticket.to_dct()["Ticket"]["Article"]
                print(len(article))
                
                # Iterating through all articles of the ticket (in cases where tickets have multiple articles)
                for a1 in range(0, len(article)):
                    # Article subject
                    q2 = "Article " + str(a1+1) + ": " + get_ticket.to_dct()["Ticket"]["Article"][a1]["Subject"] + "\n"
                    print(q2)
                    # Article body
                    x = get_ticket.to_dct()["Ticket"]["Article"][a1]["Body"]
                    x = x.encode('utf-8') #encoded
                    q3 = "Body " + str(a1+1) + ": " + x + "\n"
                    print(q3)
                
                # Ticket link for reference
                q4 = "Ticket link: " + TICKET_LINK + ticket_id + "\n"
                print(q4, end="\n\n")          
    
    def main():
        client = createSession()
        ticketData(client)
        print("done")
    
    main()