
PythonでZendeskのチケットを取得する
この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
前回、Zendesk APIを使用してチケットやコメント情報を取得しました。
今回はPythonを使ってチケット情報やコメント情報を取得したのでサンプルコードをご紹介します。
※Python version 3.6.1を使用しています。
HTTPライブラリのrequestsをインストールしておきましょう。
$ pip3 install requests
ticketの情報を取得する
取得する情報
- id: Automatically assigned when the ticket is created
- subject: The value of the subject field for this ticket
- created_at: When this record was created
- updated_at: When this record last got updated
サンプルコード
import requests
from urllib.parse import urlencode
import time
# Set the request parameters
url = 'https://subdomain.zendesk.com/api/v2/search.json?'
user = 'email address' + '/token'
pwd = 'api key'
session = requests.Session()
session.auth = (user, pwd)
params = {'query': 'type:ticket status:closed brand:{brand_id}'}
url = url + urlencode(params)
ticket_results = []
while url:
    response = session.get(url)
    if response.status_code == 429:
        print('Rate limited! Please wait.')
        time.sleep(int(response.headers['retry-after']))
        continue
    if response.status_code != 200:
        print('Status:', response.status_code, 'Problem with the request. Exiting.')
        exit()
    data = response.json()
    ticket_results.extend(data['results'])
    # Noneになるまで取得する
    url = data['next_page']
for ticket in ticket_results:
    ticket_id = ticket['id']
    created_at = ticket['created_at']
    updated_at = ticket['updated_at']
    subject = ticket['subject']
    print(f'ticket_id: {ticket_id}, '
          f'create_at: {created_at}, '
          f'updata_at: {updated_at}, '
          f'subject: {subject}')
特定のブランドのステータスがclosedのチケット情報を取得しました。
params = {'query': 'type:ticket status:closed brand:{brand_id}'}
search.jsonでは100件までの情報しか取得できないのでnext_pageを使用して
全ての情報を取得するようにしています。
while url:
[...]
    url = data['next_page']
APIのRate limitedに届いてしまった場合にretry-after後に再取得するようにしています。
if response.status_code == 429:
    print('Rate limited! Please wait.')
    time.sleep(int(response.headers['retry-after']))
    continue
レスポンス例)
ticket_id: ****4, create_at: 2017-06-02T00:36:35Z, updata_at: 2017-06-12T17:02:09Z, subject: test ticket_id: ****5, create_at: 2017-06-01T19:12:09Z, updata_at: 2017-06-05T21:01:49Z, subject: test2 ticket_id: ****6, create_at: 2017-06-01T18:56:38Z, updata_at: 2017-06-05T20:03:50Z, subject: test3 ticket_id: ****7, create_at: 2017-05-31T14:58:46Z, updata_at: 2017-06-04T18:02:43Z, subject: test4
comment情報を取得する
ticketのidが取得できたのでcomments.jsonからコメントの情報も取得してみました。 こちらはticket idを指定して取得します。
ticket_id = '1'
取得する情報
- id: Automatically assigned when the comment is created
- public: true if a public comment; false if an internal note.
- body: The comment string
サンプルコード
import requests
# Set the request parameters
url = 'https://subdomain.zendesk.com/api/v2/tickets/'
user = 'email_address' + '/token'
pwd = 'api key'
session = requests.Session()
session.auth = (user, pwd)
ticket_id = '1'
url = url + ticket_id + "/comments.json"
response = session.get(url)
if response.status_code != 200:
    print('Status:', response.status_code, 'ticket_id:', ticket_id, 'Problem with the request. Exiting.')
    exit()
data = response.json()
for comment in data['comments']:
    comment_id = comment['id']
    public = comment['public']
    body = comment['body']
    print(f'comment_id: {comment_id}, '
          f'public: {public}, '
          f'body: {body}')
レスポンス例)
comment_id: 4***********, public: True, body: test comment comment_id: 4***********, public: False, body: private comment comment_id: 4***********, public: True, body: public test comment comment_id: 4***********, public: True, body: test2 comment comment_id: 4***********, public: True, body: closed comment
まとめ
今回はPythonを使ってZendeskの情報を取得しました。
ではまた。











