           response = JSON.parse(body)
           handler response
         else
-          msg.send "Hmm.  I don't know how to that CircleCI response: #{res.statusCode}"
+          msg.send "Hmm.  I don't know how to process that CircleCI response: #{res.statusCode}"
 
-  # robot.router.post "/hubot/circle", (req, res) ->
-  #   query = querystring.parse url.parse(req.url).query
-  #   res.end JSON.stringify {
-  #      received: true #some client have problems with an empty response
-  #   }
+  robot.router.post "/hubot/circle", (req, res) ->
+    query = querystring.parse url.parse(req.url).query
+    res.end JSON.stringify {
+       received: true #some client have problems with an empty response
+    }
 
-  #   user = {}
-  #   user.room = query.room if query.room
-  #   user.type = query.type if query.type
+    user = {}
+    user.room = query.room if query.room
+    user.type = query.type if query.type
 
-  #   try
-  #     payload = JSON.parse req.body.payload
+    try
+      robot.send user, formatBuildStatus(req.body.payload)
 
-  #     gitio payload.compare_url, (err, data) ->
-  #       robot.send user, "#{payload.status_message.toUpperCase()} build (#{payload.build_url}) on #{payload.repository.name}:#{payload.branch} by #{payload.author_name} with commit (#{if err then payload.compare_url else data})"
+    catch error
+      console.log "circle hook error: #{error}. Payload: #{util.inspect(req.body.payload)}"
 
-  #   catch error
-  #     console.log "circle hook error: #{error}. Payload: #{req.body.payload}"
-

url = require('url')
util = require('util')
querystring = require('querystring')
gitio = require('gitio')

circle_ci_endpoint = 'https://circleci.com/api/v1/'

module.exports = (robot) ->
  
  robot.respond /circle me (\S*)\s*(\S*)/i, (msg) ->
    project = escape(msg.match[1])
    branch = if msg.match[2] then escape(msg.match[2]) else 'master'
    msg.http("https://circleci.com/api/v1/project/#{project}/tree/#{branch}?circle-token=#{process.env.HUBOT_CIRCLECI_TOKEN}")
      .headers("Accept": "application/json")
      .get() handleResponse  (response) ->
          if response.length == 0
            msg.send "Current build status for #{project}(#{branch}): unknown"
          else
            currentBuild = response[0]
            msg.send "Current build status for #{project}(#{currentBuild.branch}): #{currentBuild.status} #{currentBuild.build_num}"

  robot.respond /circle last (\S*)\s*(\S*)/i, (msg) ->
    project = escape(msg.match[1])
    branch = if msg.match[2] then escape(msg.match[2]) else 'master'
    msg.http("https://circleci.com/api/v1/project/#{project}/tree/#{branch}?circle-token=#{process.env.HUBOT_CIRCLECI_TOKEN}")
      .headers("Accept": "application/json")
      .get() handleResponse (response) ->
          if response.length == 0
            msg.send "Current build status for #{project}(#{branch}): unknown"
          else
            last = response[0]
            if last.status != 'running'
              msg.send "Current build status for #{project}(#{last.branch}): #{last.status} #{last.build_num}]"
            else if last.previous && last.previous.status
              msg.send "Last build status for #{project}(#{last.branch}): #{last.previous.status} #{last.previous.build_num}"
            else
              msg.send "Last build status for #{project}(#{branch}): unknown"

  robot.respond /circle retry (.*) (.*)/i, (msg) ->
    project = escape(msg.match[1])

#   Find the build status of aproject on CircleCI
#
# Dependencies:
#   None
#
# Configuration:
#   None
