Skip to content

Commit

Permalink
Merge branch 'master' of heroku.com:bashubot
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Caswell committed Jun 2, 2015
2 parents 296b36f + 50dbde0 commit 4361494
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
36 changes: 33 additions & 3 deletions scripts/hipchat_rooms.coffee
Expand Up @@ -44,6 +44,34 @@ hipchatApi =
msg.reply err if err?
msg.reply resp if resp?

printTranscript: (msg, ticketnum) ->
(callback) =>
@roomFromTicket(msg, ticketnum) (err, room) =>
if err isnt null
msg.reply err
else
hipchatter.history room.id, (err, data) =>
if err isnt null
msg.reply err
else
startMessage = "Begin support session for ticket #{ticketnum}"
replyArray = []
printout = false
for item in data.items
printout = true if item.message is startMessage
#if we are at the current ticket session, start gathering transcript
if printout
d = new Date(parseInt(Date.parse(item.date)))
formattedDate = "#{d.getMonth() + 1}/#{d.getDate()}/#{d.getFullYear()} #{d.getHours()}:#{if d.getMinutes() < 10 then '0' else ''}#{d.getMinutes()}"
#filter out BashoBot and HipChat messages
if item.file isnt undefined
item.message = "File - #{item.file.name} - #{item.file.url}. " + item.message
if item.from.name? and (item.from.name isnt "Basho Bot")
replyArray.push "[#{formattedDate}] #{item.from.name}: #{item.message}"
pnote = "#{replyArray.join("\n")}"
msg.robot.zenDesk.uploadComment msg, ticketnum, "Adding chat transcript from recent HipChat conversation.", true, "ticket_#{ticketnum}_transcript.txt","text/plain", pnote, (retObj) ->
msg.reply("Updated ticket #{ticketnum}")

setGuestFromTicketAndTag: (msg,ticketnum,access) ->
@roomFromTicket(msg,ticketnum) (err,room) =>
if err isnt null
Expand Down Expand Up @@ -398,8 +426,10 @@ module.exports = (robot) ->
msg.reply err if err?
msg.reply created if created?

robot.respond /(?:attach|quote|chat)*\s*log to ticket [#]?([0-9]*)\s*$/i, (msg) ->
msg.reply "not yet implemented"
robot.respond /(?:attach |quote |chat )*\s*log to ticket [#]?([0-9]*)\s*$/i, (msg) ->
hipchatApi.printTranscript(msg, msg.match[1]) (err, generated) ->
msg.reply err if err?
msg.reply generated if generated?

robot.respond /(?:list |show |all )*(?:open|guest|accessible|public) rooms/i, (msg) ->
hipchatApi.listOpenRooms msg
Expand All @@ -409,7 +439,7 @@ module.exports = (robot) ->
msg.reply err if err?
msg.reply result if result?

robot.respond /get org for ticket (.*)$/i, (msg) ->
robot.respond /get org for ticket #]? (.*)$/i, (msg) ->
msg.robot.zenDesk.getOrgNameFromTicket(msg, msg.match[1]) (Org) ->
msg.reply Org

Expand Down
24 changes: 20 additions & 4 deletions scripts/zendesk.coffee
Expand Up @@ -24,16 +24,22 @@ zenDesk =
user: process.env.ZENDESKAPI_USER
token: process.env.ZENDESKAPI_TOKEN

httpClient: () ->
HttpClient.create(@url, headers: { 'Authorization': 'Basic ' + new Buffer("#{@user}/token:#{@token}").toString('base64'), 'Accept': 'application/json', 'Content-Type': 'application/json' })

httpClient: (contentType = "application/json", customHeaders = {}) ->
headerObj = {
'Authorization': 'Basic ' + new Buffer("#{@user}/token:#{@token}").toString('base64'),
'Accept': 'application/json',
'Content-Type': contentType
}
for k of customHeaders
headerObj[k] = customHeaders[k]
HttpClient.create(@url, headers: headerObj)

process: (msg, method, fun, field) ->
(err, res, body) ->
if err
msg.reply "Error processing #{method} request: #{err}"
else
if res.statusCode is 200
if res.statusCode is 200 or 201
bodydata = JSON.parse body
if fun
if field
Expand Down Expand Up @@ -70,6 +76,16 @@ zenDesk =
updateobject = '{"ticket":{"status":"pending","comment":{"public":"'+customercansee+'","body":"'+comment+'"}}}'
@put msg, "tickets/#{ticknum}.json", updateobject, "ticket"

uploadComment: (msg, ticknum, comment, customercansee, filename, contentType, data, callback) ->
@upload(msg, filename, contentType, data) (body) =>
filetoken = body.upload.token
updateobject = '{"ticket":{"status":"pending","comment":{"public":"'+customercansee+'","body":"'+comment+'","uploads":["'+filetoken+'"]}}}'
@put(msg, "tickets/#{ticknum}.json", updateobject, "ticket") (callback)

upload: (msg, filename, contentType, data) ->
(fun) =>
@httpClient(contentType, {"content-length":data.length}).path("/api/v2/uploads.json").query({"filename":filename}).post(data) @process msg, "POST", fun

getComments: (msg, ticketnum) ->
@get msg, "tickets/#{ticketnum}/comments.json"

Expand Down

0 comments on commit 4361494

Please sign in to comment.