阅读 PyOTRS 中的文章

Read Articles in PyOTRS

我想知道是否有人能够通过 pyOTRS 阅读 OTRS 系统的 Tickets 中的文章?我可以正常连接并获得门票,只是不知道如何获取门票的内容。我一直在 PyOTRS 文档上下翻阅,但我被卡住了。有没有人可以分享关于阅读文章的内容?

此处提到了 PyOTRS 的先决条件:https://pypi.org/project/PyOTRS/。 完成这些后,可以采取以下步骤来检索 OTRS 票证数据:

  1. 连接是通过创建客户端发起的。
  2. 进行了 OTRS 票证搜索。
  3. 使用 get_ticket.to_dct() 响应检索 OTRS 票证数据,包括动态字段和文章。
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()