     @log "Successfully 'connected' as", self.robot.name
     self.emit "connected"
 
-  # Convenience HTTP Methods for sending data back to slack
+
+  ###################################################################
+  # Convenience HTTP Methods for sending data back to slack.
+  ###################################################################
   get: (path, callback) ->
     @request "GET", path, null, callback
 

          self.logError data

        #console.log "HTTPS response:", data
        if callback
          callback null, data

        response.on "error", (err) ->
          self.logError "HTTPS response error:", err
          if callback
            callback err, null

    if method is "POST"
      request.end(body, 'binary')
    else
      request.end()

    request.on "error", (err) ->
      self.logError "HTTPS request error:", err
      self.logError err.stack
      if callback
        callback err

exports.use = (robot) ->
  new Slack robot


  parseOptions: ->
    @options =
      token : process.env.HUBOT_SLACK_TOKEN
      team  : process.env.HUBOT_SLACK_TEAM
      name  : process.env.HUBOT_SLACK_BOTNAME or 'slackbot'

  getMessageFromRequest: (req) ->
    # Parse the payload
    hubotMsg = req.param 'text'
    @unescapeHtml hubotMsg if hubotMsg

  getAuthorFromRequest: (req) ->
    # Return an author object
    id       : req.param 'user_id'
    name     : req.param 'user_name'
    reply_to : req.param 'channel_id'
    room     : req.param 'channel_name'


  ###################################################################
  # The star.
  ###################################################################
  run: ->
    self = @
    @parseOptions()

    @log "Slack adapter options:", @options

    return @logError "No services token provided to Hubot" unless @options.token
    return @logError "No team provided to Hubot" unless @options.team

    # Listen to incoming webhooks from slack
    self.robot.router.post "/hubot/slack-webhook", (req, res) ->
      self.log "Incoming message received"

      hubotMsg = self.getMessageFromRequest req
      author = self.getAuthorFromRequest req

      if hubotMsg and author
        # Pass to the robot
        self.log "Received #{hubotMsg} from #{author.name}"
        self.receive new TextMessage(author, hubotMsg)

      # Just send back an empty reply, since our actual reply,
      # if any, will be async above
