この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
Zendesk APIを使用してチケットやコメント、オープン中のチケットなど確認してみましたのでご紹介します。
APIトークンの作成はAPIトークンを作成して管理するをご覧ください。
作成後、APIトークンをセットします。
email_address='************'
password='*****************'
url='https://subdomain.zendesk.com/api/v2/'
Tickets
GET /api/v2/tickets.json
チケットの一覧を取得します。
$ curl ${url}tickets.json -u ${email_address}/token:${password} | jq .
チケット100件まで表示されます。チケットは古いものから作成日時順にソートされますがアーカイブされたチケットは表示されません。
レスポンス例)
{
"tickets": [
{
"url": "https://subdomain.zendesk.com/api/v2/tickets/{ticket_id}.json",
"id": {ticket_id},
"external_id": null,
[...]
"created_at": "yyyy-MM-ddThh:mm:ssZ",
"updated_at": "yyyy-MM-ddThh:mm:ssZ",
"type": null,
"subject": "{sample_subject}",
"raw_subject": "{sample_raw_subject}",
"description": "{sample_comment}",
[...]
],
"next_page": "https://subdomain.zendesk.com/api/v2/tickets.json?page=2",
"previous_page": null,
"count":****
}
GET /api/v2/incremental/tickets.json?start_time={unix_time}
先ほどのGET /api/v2/tickets.json
だと全てのチケットが確認できなかったのでstart_time={unix_time}
を指定して確認してみます。
2015年3月1日から確認しました。
$ curl ${url}incremental/tickets.json?start_time=1425135600 -u ${email_address}/token:${password} | jq .
こちらは1000件まで表示されます。next_page
を使用して順次確認することができます。
レスポンス例)
{
"tickets": [
{
"url": "https://subdomain.zendesk.com/api/v2/tickets/{ticket_id}.json",
"id": {ticket_id},
"external_id": null,
[...]
"created_at": "2015-03-27T01:32:49Z",
"updated_at": "2015-03-31T09:01:09Z",
"type": "incident",
"subject": "{sample_ subject}",
"raw_subject": "{raw_subject}",
"description": "{sample_comment}",
"priority": "high",
"status": "closed",
[...]
"count": 1000,
"next_page": "https://subdomain.zendesk.com/api/v2/incremental/tickets.json?start_time=1460707286",
"end_time": 1460707286
}
一番古いチケットから1000件ずつ取得できました。
Ticket Comments
GET /api/v2/tickets/{ticket_id}/comments.json
チケットIDを指定することでチケットのコメントを表示できます。
$ curl ${url}requests/{ticket_id}/comments.json -u ${email_address}/token:${password} | jq .
レスポンス例)
{
"comments": [
{
"url": "https://subdomain.zendesk.com/api/v2/requests/{ticket_id}/comments/{comment_id}.json",
"id": {comment_id},
"type": "Comment",
"request_id": {ticket_id},
"body": "test comment",
"html_body": "<div class=\"zd-comment\"><p dir=\"auto\">test comment</p></div>",
"plain_body": "test comment",
"public": true,
"author_id": {author_id},
"attachments": [],
"created_at": "2017-11-18T22:28:30Z"
},
{
"url": "https://subdomain.zendesk.com/api/v2/requests/{ticket_id}/comments/{comment_id}.json",
"id": {comment_id},
"type": "Comment",
"request_id": {ticket_id},
"body": "public test comment",
"html_body": "<div class=\"zd-comment\">public test comment<br>\n</div>",
"plain_body": "public test comment",
"public": true,
"author_id": {author_id},
"attachments": [],
"created_at": "2017-11-18T22:28:59Z"
},
{
"url": "https://subdomain.zendesk.com/api/v2/requests/{ticket_id}/comments/{comment_id}.json",
"id": {comment_id},
"type": "Comment",
"request_id": {ticket_id},
"body": "test2 comment",
"html_body": "<div class=\"zd-comment\"><p dir=\"auto\">test2 comment</p></div>",
"plain_body": "test2 comment",
"public": true,
"author_id": {author_id},
"attachments": [],
"created_at": "2017-11-18T22:29:13Z"
},
{
"url": "https://subdomain.zendesk.com/api/v2/requests/{ticket_id}/comments/{comment_id}.json",
"id": {comment_id},
"type": "Comment",
"request_id": {ticket_id},
"body": "closed comment",
"html_body": "<div class=\"zd-comment\">closed comment<br>\n</div>",
"plain_body": "closed comment",
"public": true,
"author_id":{author_id},
"attachments": [],
"created_at": "2017-11-18T22:29:55Z"
}
],
[...].
Search
Zendesk developers - Core API:Search
検索クエリを投げて特定のチケットを確認します。
.../api/v2/search.json?query={search_string}
query=type:ticket status<closed
クローズされていないチケットを確認してみます。
$ curl ${url}search.json -G --data-urlencode "query=type:ticket status<closed" -u ${email_address}/token:${password} | jq .
レスポンス例)
{
"results": [
{
"url": "https://subdomain.zendesk.com/api/v2/tickets/{ticket_id}.json",
"id": {ticket_id},
"external_id": null,
[...]
"created_at": "yyyy-MM-ddThh:mm:ssZ",
"updated_at": "yyyy-MM-ddThh:mm:ssZ",
"type": null,
"subject": "{subject}",
"raw_subject": "{raw_subject}",
"description": "{description}",
"priority": "low",
"status": "new",
[...]
"facets": null,
"next_page": "https://subdomain.zendesk.com/api/v2/search.json?page=2&query=type%3Aticket+status%3Cclosed",
"previous_page": null,
"count": {count}
}
query=type:ticket status:closed brand:{brand_id}
statusがclosedでブランドを絞って確認する。
count
で何件closedチケットがあるかも確認できます。
$ curl ${url}search.json -G --data-urlencode "query=type:ticket status:closed brand:{brand_id}" -v -u ${email_address}/token:${password} | jq .
[...]
"next_page": "https://subdomain.zendesk.com/api/v2/search.json?page=2&query=type%3Aticket+status%3Cclosed",
"previous_page": null,
"count": {count}
}
query=type:ticket brand:{brand_id} created>2017-10-01T00:00:00Z created<2017-11-01T00:00:00Z
10月に作成されたチケットを確認、ブランドも指定
curl ${url}search.json -G --data-urlencode "query=type:ticket brand:{brand_id} created>2017-10-01T00:00:00Z created<2017-11-01T00:00:00Z" -v -u ${email_address}/token:${password} | jq .
検索についてはZendesk Support検索リファレンスを参考に試してみてください。
まとめ
Zendesk APIでチケットを確認する方法をご紹介しました。jq
を使って必要な情報を取得することも容易にできそうです。
ではまた。