Skip to content

Commit f66ba9f

Browse files
committed
adding zendesk
1 parent f3529ba commit f66ba9f

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

src/scripts/zendesk.coffee

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Description:
2+
# Queries Zendesk for information about support tickets
3+
#
4+
# Configuration:
5+
# HUBOT_ZENDESK_USER
6+
# HUBOT_ZENDESK_PASSWORD
7+
# HUBOT_ZENDESK_SUBDOMAIN
8+
#
9+
# Commands:
10+
# (all) tickets - returns the total count of all unsolved tickets. The 'all'
11+
# keyword is optional.
12+
# new tickets - returns the count of all new tickets
13+
# open tickets - returns the count of all open tickets
14+
# list (all) tickets - returns a list of all unsolved tickets. The 'all'
15+
# keyword is optional.
16+
# list new tickets - returns a list of all new tickets
17+
# list open tickets - returns a list of all open tickets
18+
# ticket <ID> - returns informationa about the specified ticket
19+
20+
sys = require 'sys' # Used for debugging
21+
22+
queries =
23+
unsolved: "search.json?query=\"status<solved type:ticket\""
24+
open: "search.json?query=\"status:open type:ticket\""
25+
new: "search.json?query=\"status:new type:ticket\""
26+
tickets: "tickets"
27+
users: "users"
28+
29+
30+
zendesk_request = (msg, url, handler) ->
31+
zendesk_user = "#{process.env.HUBOT_ZENDESK_USER}"
32+
zendesk_password = "#{process.env.HUBOT_ZENDESK_PASSWORD}"
33+
auth = new Buffer("#{zendesk_user}:#{zendesk_password}").toString('base64')
34+
zendesk_url = "https://#{process.env.HUBOT_ZENDESK_SUBDOMAIN}.zendesk.com/api/v2"
35+
msg.http("#{zendesk_url}/#{url}")
36+
.headers(Authorization: "Basic #{auth}", Accept: "application/json")
37+
.get() (err, res, body) ->
38+
if err
39+
msg.send "zendesk says: #{err}"
40+
return
41+
content = JSON.parse(body)
42+
handler content
43+
44+
# FIXME this works about as well as a brick floats
45+
zendesk_user = (msg, user_id) ->
46+
zendesk_request msg, "#{queries.users}/#{user_id}.json", (result) ->
47+
if result.error
48+
msg.send result.description
49+
return
50+
result.user
51+
52+
53+
module.exports = (robot) ->
54+
55+
robot.respond /(all )?tickets$/i, (msg) ->
56+
zendesk_request msg, queries.unsolved, (results) ->
57+
ticket_count = results.count
58+
msg.send "#{ticket_count} unsolved tickets"
59+
60+
robot.respond /new tickets$/i, (msg) ->
61+
zendesk_request msg, queries.new, (results) ->
62+
ticket_count = results.count
63+
msg.send "#{ticket_count} new tickets"
64+
65+
robot.respond /open tickets$/i, (msg) ->
66+
zendesk_request msg, queries.open, (results) ->
67+
ticket_count = results.count
68+
msg.send "#{ticket_count} open tickets"
69+
70+
robot.respond /list (all )?tickets$/i, (msg) ->
71+
zendesk_request msg, queries.unsolved, (results) ->
72+
for result in results.results
73+
msg.send "#{result.id} is #{result.status}: #{result.subject}"
74+
75+
robot.respond /list new tickets$/i, (msg) ->
76+
zendesk_request msg, queries.new, (results) ->
77+
for result in results.results
78+
msg.send "#{result.id} is #{result.status}: #{result.subject}"
79+
80+
robot.respond /list open tickets$/i, (msg) ->
81+
zendesk_request msg, queries.open, (results) ->
82+
for result in results.results
83+
msg.send "#{result.id} is #{result.status}: #{result.subject}"
84+
85+
robot.respond /ticket ([\d]+)$/i, (msg) ->
86+
ticket_id = msg.match[1]
87+
zendesk_request msg, "#{queries.tickets}/#{ticket_id}.json", (result) ->
88+
if result.error
89+
msg.send result.description
90+
return
91+
message = "#{result.ticket.subject} ##{result.ticket.id} (#{result.ticket.status.toUpperCase()})"
92+
message += "\nUpdated: #{result.ticket.updated_at}"
93+
message += "\nAdded: #{result.ticket.created_at}"
94+
message += "\nDescription:\n-------\n#{result.ticket.description}\n--------"
95+
msg.send message

0 commit comments

Comments
 (0)