1
|
|
|
require 'zendesk_api' |
2
|
|
|
|
3
|
|
|
module Lita |
4
|
|
|
module Handlers |
5
|
|
|
class Zendesk < Handler |
6
|
|
|
|
7
|
|
|
API_VERSION_URL_PATH = 'api/v2' |
8
|
|
|
WEB_TICKETS_URL_PATH = 'tickets' |
9
|
|
|
QUERY_TICKETS_ALL = 'type:ticket' |
10
|
|
|
QUERY_TICKETS_ESCALATED = 'type:ticket tags:escalated status:open status:pending' |
11
|
|
|
QUERY_TICKETS_HOLD = 'type:ticket status:hold' |
12
|
|
|
QUERY_TICKETS_OPEN = 'type:ticket status:open' |
13
|
|
|
QUERY_TICKETS_NEW = 'type:ticket status:new' |
14
|
|
|
QUERY_TICKETS_PENDING = 'type:ticket status:pending' |
15
|
|
|
QUERY_TICKETS_UNSOLVED = 'type:ticket status<solved' |
16
|
|
|
QUERY_USERS = 'users' |
17
|
|
|
|
18
|
|
|
config :subdomain, type: String, required: true |
19
|
|
|
config :username, type: String, required: true |
20
|
|
|
config :token, type: String, default: '' |
21
|
|
|
config :password, type: String, default: '' |
22
|
|
|
|
23
|
|
|
config :max_results, type: Integer, default: 10 |
24
|
|
|
|
25
|
|
|
def client |
26
|
|
|
return @_client if @_client |
27
|
|
|
Lita.logger.info "#{logger_prefix}Connecting Zendesk Client to #{api_version_url}" |
28
|
|
|
@_client = ZendeskAPI::Client.new do |client| |
29
|
|
|
client.url = api_version_url.to_s |
30
|
|
|
client.username = config.username || config.user |
31
|
|
|
client.token = config.token |
32
|
|
|
client.password = config.password |
33
|
|
|
end |
34
|
|
|
end |
35
|
|
|
|
36
|
|
|
def base_url |
37
|
|
|
"https://#{config.subdomain}.zendesk.com" |
38
|
|
|
end |
39
|
|
|
|
40
|
|
|
def api_version_url |
41
|
|
|
uri_join base_url, API_VERSION_URL_PATH |
42
|
|
|
end |
43
|
|
|
|
44
|
|
|
def web_tickets_url |
45
|
|
|
uri_join base_url, WEB_TICKETS_URL_PATH |
46
|
|
|
end |
47
|
|
|
|
48
|
|
|
# General |
49
|
|
|
|
50
|
|
|
route(/^(?:zd|zendesk)\s+connection\s*$/i, :zd_instance_info, command: true, help: { 'zd connection' => 'returns information on the Zendesk connection' }) |
51
|
|
|
def zd_instance_info(response) |
52
|
|
|
response.reply "Using Zendesk instance at: #{base_url}" |
53
|
|
|
end |
54
|
|
|
|
55
|
|
|
route(/^(?:zd|zendesk)\s+search\s+tickets?\s+(\S.*?)\s*$/i, :search_tickets, command: true, help: { 'zd search tickets <QUERY>' => 'returns search results' }) |
56
|
|
|
def search_tickets(response) |
57
|
|
|
ticket_search response, response.matches[0][0] |
58
|
|
|
end |
59
|
|
|
|
60
|
|
|
# Ticket Counts |
61
|
|
|
|
62
|
|
|
route(/^(?:zd|zendesk)(\s+unsolved)?\s+tickets?\s*$/i, :unsolved_tickets, command: true, help: { 'zd tickets' => 'returns the total count of all unsolved tickets' }) |
63
|
|
|
def unsolved_tickets(response) |
64
|
|
|
ticket_count response, QUERY_TICKETS_UNSOLVED, 'unsolved' |
65
|
|
|
end |
66
|
|
|
|
67
|
|
|
route(/^(?:zd|zendesk)\s+(all|total)\s+tickets?\s*$/i, :total_tickets, command: true, help: { 'zd all tickets' => 'returns the count of all tickets' }) |
68
|
|
|
def total_tickets(response) |
69
|
|
|
ticket_count response, QUERY_TICKETS_ALL, 'total' |
70
|
|
|
end |
71
|
|
|
|
72
|
|
|
route(/^(?:zd|zendesk)\s+pending\s+tickets?\s*$/i, :pending_tickets, command: true, help: { 'zd pending tickets' => 'returns a count of tickets that are pending' }) |
73
|
|
|
def pending_tickets(response) |
74
|
|
|
ticket_count response, QUERY_TICKETS_PENDING, 'pending' |
75
|
|
|
end |
76
|
|
|
|
77
|
|
|
route(/^(?:zd|zendesk)\s+new\s+tickets?\s*$/i, :new_tickets, command: true, help: { 'zd new tickets' => 'returns the count of all new (unassigned) tickets' }) |
78
|
|
|
def new_tickets(response) |
79
|
|
|
ticket_count response, QUERY_TICKETS_NEW, 'new' |
80
|
|
|
end |
81
|
|
|
|
82
|
|
|
route(/^(?:zd|zendesk)\s+escalated\s+tickets?\s*$/i, :escalated_tickets, command: true, help: { 'zd escalated tickets' => 'returns a count of tickets with escalated tag that are open or pending' }) |
83
|
|
|
def escalated_tickets(response) |
84
|
|
|
ticket_count response, QUERY_TICKETS_ESCALATED, 'escalated' |
85
|
|
|
end |
86
|
|
|
|
87
|
|
|
route(/^(?:zd|zendesk)\s+open\s+tickets?\s*$/i, :open_tickets, command: true, help: { 'zd open tickets' => 'returns the count of all open tickets' }) |
88
|
|
|
def open_tickets(response) |
89
|
|
|
ticket_count response, QUERY_TICKETS_OPEN, 'open' |
90
|
|
|
end |
91
|
|
|
|
92
|
|
|
route(/^(?:zd|zendesk)\s+on\s*hold\s+tickets?\s*$/i, :onhold_tickets, command: true, help: { 'zd on hold tickets' => 'returns the count of all on hold tickets' }) |
93
|
|
|
def onhold_tickets(response) |
94
|
|
|
ticket_count response, QUERY_TICKETS_HOLD, 'on hold' |
95
|
|
|
end |
96
|
|
|
|
97
|
|
|
# Ticket Lists |
98
|
|
|
|
99
|
|
|
route(/^(?:zd|zendesk)\s+list(\s+unsolved)?\s+tickets?\s*$/i, :unsolved_tickets_list, command: true, help: { 'zd list tickets' => 'returns a list of unsolved tickets' }) |
100
|
|
|
def unsolved_tickets_list(response) |
101
|
|
|
ticket_list response, QUERY_TICKETS_UNSOLVED, 'unsolved' |
102
|
|
|
end |
103
|
|
|
|
104
|
|
|
route(/^(?:zd|zendesk)\s+list\s+(all|total)\s+tickets?\s*$/i, :total_tickets_list, command: true, help: { 'zd list all tickets' => 'returns a list of all tickets' }) |
105
|
|
|
def total_tickets_list(response) |
106
|
|
|
ticket_list response, QUERY_TICKETS_ALL, 'total' |
107
|
|
|
end |
108
|
|
|
|
109
|
|
|
route(/^(?:zd|zendesk)\s+list\s+pending\s+tickets?\s*$/i, :pending_tickets_list, command: true, help: { 'zd list pending tickets' => 'returns a list of pending tickets' }) |
110
|
|
|
def pending_tickets_list(response) |
111
|
|
|
ticket_list response, QUERY_TICKETS_PENDING, 'pending' |
112
|
|
|
end |
113
|
|
|
|
114
|
|
|
route(/^(?:zd|zendesk)\s+list\s+new\s+tickets?\s*$/i, :new_tickets_list, command: true, help: { 'zd list new tickets' => 'returns a list of new tickets' }) |
115
|
|
|
def new_tickets_list(response) |
116
|
|
|
ticket_list response, QUERY_TICKETS_NEW, 'new' |
117
|
|
|
end |
118
|
|
|
|
119
|
|
|
route(/^(?:zd|zendesk)\s+list\s+escalated\s+tickets?\s*$/i, :escalated_tickets_list, command: true, help: { 'zd list esclated tickets' => 'returns a list of escalated tickets' }) |
120
|
|
|
def escalated_tickets_list(response) |
121
|
|
|
ticket_list response, QUERY_TICKETS_ESCALATED, 'escalated' |
122
|
|
|
end |
123
|
|
|
|
124
|
|
|
route(/^(?:zd|zendesk)\s+list\s+open\s+tickets?\s*$/i, :open_tickets_list, command: true, help: { 'zd list open tickets' => 'returns a list of open tickets' }) |
125
|
|
|
def open_tickets_list(response) |
126
|
|
|
ticket_list response, QUERY_TICKETS_OPEN, 'open' |
127
|
|
|
end |
128
|
|
|
|
129
|
|
|
route(/^(?:zd|zendesk)\s+list\s+on\s*hold\s+tickets?\s*$/i, :onhold_tickets_list, command: true, help: { 'zd list on hold tickets' => 'returns a list of on hold tickets' }) |
130
|
|
|
def onhold_tickets_list(response) |
131
|
|
|
ticket_list response, QUERY_TICKETS_HOLD, 'on hold' |
132
|
|
|
end |
133
|
|
|
|
134
|
|
|
# Ticket Details |
135
|
|
|
|
136
|
|
|
route(/^(?:zd|zendesk)\s+ticket\s+(\d+)\s*$/i, :ticket_details_with_comments, command: true, help: { 'zd ticket <ID>' => 'returns information about the specified ticket' }) |
137
|
|
|
def ticket_details_with_comments(response) |
138
|
|
|
Lita.logger.info "#{logger_prefix}Processing Zendesk Ticket Details" |
139
|
|
|
ticket_id = response.matches[0][0].to_i |
140
|
|
|
begin |
141
|
|
|
ticket = client.ticket.find!(id: ticket_id) |
142
|
|
|
response.reply get_text_for_ticket_with_comments(ticket) |
143
|
|
|
rescue => e |
144
|
|
|
Lita.logger.warn "#{logger_prefix}#{e}" |
145
|
|
|
response.reply "Error processing ticket #{ticket_id}" |
146
|
|
|
end |
147
|
|
|
end |
148
|
|
|
|
149
|
|
|
private |
150
|
|
|
|
151
|
|
|
def get_text_for_ticket(ticket, include_description = true) |
152
|
|
|
Lita.logger.info "#{logger_prefix}Processing Zendesk ticket details for [#{ticket.id}]" |
153
|
|
|
unless ticket.is_a? ZendeskAPI::Ticket |
154
|
|
|
raise 'ticket is not a ZendeskAPI::Ticket' |
155
|
|
|
end |
156
|
|
|
message = "# Ticket #{ticket.id}: #{ticket_url_web(ticket.id)}" |
157
|
|
|
message += "\n- Subject: #{ticket.subject}" |
158
|
|
|
message += "\n- Status: #{ticket.status}" |
159
|
|
|
message += "\n- Updated: #{ticket.updated_at}" |
160
|
|
|
message += "\n- Created: #{ticket.created_at}" |
161
|
|
|
message += "\n- Requester: #{user_display(ticket.requester)}" |
162
|
|
|
message += "\n- Description: #{ticket.description}" if include_description |
163
|
|
|
return message |
164
|
|
|
end |
165
|
|
|
|
166
|
|
|
def get_text_for_ticket_comments(ticket) |
167
|
|
|
Lita.logger.info "#{logger_prefix}Processing Zendesk ticket comments for [#{ticket.id}]" |
168
|
|
|
unless ticket.is_a? ZendeskAPI::Ticket |
169
|
|
|
raise 'ticket is not a ZendeskAPI::Ticket' |
170
|
|
|
end |
171
|
|
|
comments_text = [] |
172
|
|
|
ticket.audits.each_with_index do |audit,i| |
173
|
|
|
if (comment = audit.events.detect {|e| e.type.downcase == 'comment'}) |
174
|
|
|
author_text = user_display comment.author |
175
|
|
|
comment_text = "## Comment: #{author_text}" |
176
|
|
|
comment_text += "\n- Created: #{audit.created_at}" |
177
|
|
|
comment_text += "\n- Comment: #{comment.body}" |
178
|
|
|
comments_text.push comment_text |
179
|
|
|
end |
180
|
|
|
end |
181
|
|
|
return comments_text.reverse.join("\n") |
182
|
|
|
end |
183
|
|
|
|
184
|
|
|
def get_text_for_ticket_with_comments(ticket) |
185
|
|
|
Lita.logger.info "#{logger_prefix}Processing Zendesk ticket details for [#{ticket.id}] with comments" |
186
|
|
|
message = get_text_for_ticket ticket, false |
187
|
|
|
comments = get_text_for_ticket_comments ticket |
188
|
|
|
if !comments.nil? && comments.length>0 |
189
|
|
|
message += "\n#{comments}" |
190
|
|
|
end |
191
|
|
|
return message |
192
|
|
|
end |
193
|
|
|
|
194
|
|
|
def ticket_count(response, query, ticket_type = '') |
195
|
|
|
Lita.logger.info "#{logger_prefix}Processing Zendesk Ticket Count Query [#{query}]" |
196
|
|
|
begin |
197
|
|
|
ticket_count = client.search!(query: query).count |
198
|
|
|
Lita.logger.info "#{logger_prefix}Ticket count #{ticket_count}" |
199
|
|
|
ticket_desc = ticket_type == '' ? '' : "#{ticket_type} " |
200
|
|
|
ticket_word = ticket_count == 1 ? 'ticket' : 'tickets' |
201
|
|
|
response.reply "#{ticket_count} #{ticket_desc}#{ticket_word}." |
202
|
|
|
rescue |
203
|
|
|
response.reply "A Zendesk error has been encountered." |
204
|
|
|
end |
205
|
|
|
end |
206
|
|
|
|
207
|
|
|
def ticket_search(response, raw_query) |
208
|
|
|
Lita.logger.info "#{logger_prefix}Processing Zendesk Ticket Search" |
209
|
|
|
tickets = client.search!(query: "#{QUERY_TICKETS_ALL} #{raw_query}") |
210
|
|
|
serp_count = reply_tickets response, tickets |
211
|
|
|
response.reply "Listing #{serp_count} of #{tickets.count} #{ticket_word(tickets.count)} matching #{raw_query}." |
212
|
|
|
end |
213
|
|
|
|
214
|
|
|
def ticket_list(response, query, ticket_status = '') |
215
|
|
|
Lita.logger.info "#{logger_prefix}Processing Zendesk ticket list query" |
216
|
|
|
tickets = client.search!(query: query) |
217
|
|
|
serp_count = reply_tickets response, tickets |
218
|
|
|
ticket_desc = ticket_status == '' ? '' : "#{ticket_status} " |
219
|
|
|
response.reply "Listing #{serp_count} of #{tickets.count} #{ticket_desc}#{ticket_word(tickets.count)}." |
220
|
|
|
end |
221
|
|
|
|
222
|
|
|
def reply_tickets(response, tickets) |
223
|
|
|
serp_count = 0 |
224
|
|
|
tickets.each_with_index do |ticket, i| |
225
|
|
|
break if i >= config.max_results |
226
|
|
|
serp_count += 1 |
227
|
|
|
response.reply "Ticket #{ticket.id} is #{ticket.status}: #{ticket_url_web(ticket.id)} - #{ticket.subject}" |
228
|
|
|
end |
229
|
|
|
return serp_count |
230
|
|
|
end |
231
|
|
|
|
232
|
|
|
def ticket_url_web(ticket_id) |
233
|
|
|
uri_join web_tickets_url.to_s, ticket_id.to_s |
234
|
|
|
end |
235
|
|
|
|
236
|
|
|
def ticket_word(count) |
237
|
|
|
count == 1 ? 'ticket' : 'tickets' |
238
|
|
|
end |
239
|
|
|
|
240
|
|
|
def user_display(user) |
241
|
|
|
parts = [] |
242
|
|
|
if user.name.to_s.length > 0 |
243
|
|
|
parts.push user.name |
244
|
|
|
end |
245
|
|
|
if user.email.to_s.length > 0 |
246
|
|
|
if parts.length < 1 || user.email != user.name |
247
|
|
|
parts.push "(#{user.email})" |
248
|
|
|
end |
249
|
|
|
end |
250
|
|
|
user_text = parts.join(' ') |
251
|
|
|
end |
252
|
|
|
|
253
|
|
|
def uri_join(*args) |
254
|
|
|
args.join('/').gsub(/\/\s*\//, '/').gsub(/\/+/, '/').gsub(/^(https?:\/)/i, '\1/') |
255
|
|
|
end |
256
|
|
|
|
257
|
|
|
def logger_prefix |
258
|
|
|
" -- #{self.class.name}: " |
259
|
|
|
end |
260
|
|
|
end |
261
|
|
|
|
262
|
|
|
Lita.register_handler(Zendesk) |
263
|
|
|
end |
264
|
|
|
end |