Get ticket of OTRS using PyOTRS

2018.05.13

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Introduction

We will demonstrate how to get ticket of OTRS using PyOTRS, which is a Python wrapper for accessing OTRS using the REST API.

System Requirements

Outline

PyOTRS can access an OTRS instance as follows:

* create a new Ticket
* get the data of a specific Ticket
* search for Tickets
* update existing Tickets

Main methods are:

* Client.session_create (Use credentials to "log in")
* Client.ticket_create
* Client.ticket_get_by_list  (takes a list)
* Client.ticket_get_by_id  (takes an int)
* Client.ticket_search
* Client.ticket_update

We will now try to get Number of unassigned tickets by owner and Subject and ticket links for tickets assigned to me using the following method.

  • Client.session_create
  • Client.ticket_get_by_id
  • Client.ticket_search

Install

Install PyOTRS

$ pip install PyOTRS

sample code

from pyotrs import Client
from datetime import datetime, timedelta, timezone

# OTRS constant
URL = "https://your_otrs_domain"
USERNAME = "your_user_name"
PASSWORD = "your_password"
TICKET_LINK = URL + "/otrs/index.pl?Action=AgentTicketZoom;TicketID="

# Create session
client = Client(URL, USERNAME, PASSWORD)
client.session_create()

# Get my ticket (e.g. State:'new' or 'open'、Queue:'Raw'、OwnerIDs: 28)
ticket_ids = client.ticket_search(States=['new', 'open'], Queues=['Raw'], OwnerIDs=[28])

print("number of my tickets:" + str(len(ticket_ids)))

for ticket_id in ticket_ids:
    get_ticket = client.ticket_get_by_id(ticket_id)

    print(get_ticket.field_get("Title"))
    print(TICKET_LINK + ticket_id)

# Setting timezone
jst_time = timezone(timedelta(hours=+9), 'JST')
day = datetime.now(jst_time) - timedelta(days=1)

# Get ticket (e.g. younger than 1 day State:'new'、Queue:'Raw'、OwnerIDs: 1)
ticket_ids = client.ticket_search(TicketCreateTimeNewerDate=day, States=['new'], Queues=['Raw'], OwnerIDs=[1])

print("number of new tickets(younger than 1 day):" + str(len(ticket_ids)))

Create session

We create session using Client.session_create.

# Create session
client = Client(URL, USERNAME, PASSWORD)
client.session_create()

Get my ticket whose status is "new" or "opened".

We get tickets assigned to me using Client.ticket_search.

# Get my ticket (e.g. State:'new' or 'open'、Queue:'Raw'、OwnerIDs: 28)
ticket_ids = client.ticket_search(States=['new', 'open'], Queues=['Raw'], OwnerIDs=[28])

print("number of my tickets:" + str(len(ticket_ids)))

We get ticket data using Client.ticket_get_by_id.

for ticket_id in ticket_ids:
    get_ticket = client.ticket_get_by_id(ticket_id)

    print(get_ticket.field_get("Title"))
    print(TICKET_LINK + ticket_id)

Get number of unassigned ticket.

We get numbers of tickets matching the following criteria:

  • created less than 1 day ago
  • status: 'new'
  • queue: 'Raw'
  • ownerID: 1
# Setting timezone
jst_time = timezone(timedelta(hours=+9), 'JST')
day = datetime.now(jst_time) - timedelta(days=1)

# Get ticket (e.g. younger than 1 day State:'new'、Queue:'Raw'、OwnerIDs: 1)
ticket_ids = client.ticket_search(TicketCreateTimeNewerDate=day, States=['new'], Queues=['Raw'], OwnerIDs=[1])

print("number of new tickets(younger than 1 day):" + str(len(ticket_ids)))

Execute

$ python get-otrs-ticket-using-pyotrs.py
number of my tickets:2
ExampleTitle1
https://your_otrs_domain/otrs/index.pl?Action=AgentTicketZoom;TicketID=300**1
ExampleTitle2
https://your_otrs_domain/otrs/index.pl?Action=AgentTicketZoom;TicketID=300**2
number of new tickets(younger than 1 day):54

We successfully obtained the ticket data.

Conclusion

We illustrated how to get OTRS ticket data using PyOTRS. By sending the retrieved data to communication tools such as ChatWork and Slack, we can even check the ticket data even when we are not logged into OTRS.

References