0

I'm a new Hubot/Node.js/CoffeeScript user.

I'm looking at the UptimeRobot script for Hubot. When I include that in my package I'm getting the error:

ERROR Error loading scripts from npm package - Error: Uptime Robot API Key must be provided
  at Client (/home/myhubot/node_modules/uptime-robot/index.js:11:11)
  at Robot.loadExternalScripts (/home/myhubot/node_modules/hubot/src/robot.coffee:263:11, <js>:212:39)
  at /home/myhubot/node_modules/hubot/bin/hubot:119:11, <js>:123:26
  at fs.js:268:14
  at Object.oncomplete (fs.js:107:15)

Here is the default UptimeRobot.Coffee file (located in /home/myhubot/scripts) http://pastebin.com/aeDgZu0B

For example, I've declared my HUBOT_UPTIMEROBOT_APIKEY as "11223344"

Is this the appropriate way of declaring global variables?

# Description
#   A hubot script to list/add monitors for the Uptime Robot service.
#
# Configuration:
#   HUBOT_UPTIMEROBOT_APIKEY
#   HUBOT_UPTIMEROBOT_CONTACT_ID (optional)
#
# Commands:
#   hubot uptime <filter> - Returns uptime for sites.
#   hubot uptime add-check <http://example.com> [as <friendlyname>]- Adds a new uptime check.
#
# Author:
#   patcon@myplanetdigital

HUBOT_UPTIMEROBOT_APIKEY="11223344"

apiKey = process.env.HUBOT_UPTIMEROBOT_APIKEY
alertContactId = process.env.HUBOT_UPTIMEROBOT_CONTACT_ID

module.exports = (robot) ->

  REGEX = ///
    uptime
    (       # 1)
      \s+   #    whitespace
      (.*)  # 2) filter
    )?
  ///i
  robot.respond REGEX, (msg) ->
    Client = require 'uptime-robot'
    client = new Client apiKey

    filter = msg.match[2]
    data = {}

    client.getMonitors data, (err, res) ->
      if err
        throw err

      monitors = res

      if filter
        query = require 'array-query'
        monitors = query('friendlyname')
          .regex(new RegExp filter, 'i')
          .on res

      for monitor, i in monitors
        name   = monitor.friendlyname
        url    = monitor.url
        uptime = monitor.alltimeuptimeratio
        status = switch monitor.status
          when "0" then "paused"
          when "1" then "not checked yet"
          when "2" then "up"
          when "8" then "seems down"
          when "9" then "down"

        msg.send "#{status.toUpperCase()} <- #{url} (#{uptime}% uptime)"

  robot.respond /uptime add-check (\S+)( as (.*))?$/i, (msg) ->
    url = require('url').parse(msg.match[1])
    friendlyName = msg.match[3] or url.href

    # Check that url format is correct.
    monitorUrl = url.href if url.protocol

    # Create monitor
    msg.http("http://api.uptimerobot.com/newMonitor")
      .query({
        apiKey: apiKey
        monitorFriendlyName: friendlyName
        monitorURL: monitorUrl
        monitorType: 1
        format: "json"
        noJsonCallback: 1
        monitorAlertContacts: [
          alertContactId
        ]
      })
      .get() (err, res, body) ->
        response = JSON.parse(body)

        if response.stat is "ok"
          msg.send "done"

        if response.stat is "fail"
          msg.send "#{response.message}"
boostn
  • 21
  • 1

1 Answers1

1

You have HUBOT_UPTIMEROBOT_APIKEY="11223344" but you're not actually using it because you also have apiKey = process.env.HUBOT_UPTIMEROBOT_APIKEY. That tries to pull it from the environment. I'm assuming you never set any environment variable so that should be undefined. See https://nodejs.org/api/process.html#process_process_env

Transcendence
  • 2,616
  • 3
  • 21
  • 31
  • Completely makes sense, however... I see no mention of this process.env anywhere in the Hubot discussions. Do I just create this file to declare the Global Variable? – boostn Apr 22 '15 at 20:46
  • It's not a global variable. It's an environment variable. Read this https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps – Transcendence Apr 22 '15 at 21:11
  • You may have to do something different if you're not using a bash shell – Transcendence Apr 22 '15 at 21:11
  • I'm using shell to setup everything up and test for now.. Thanks for the article, I placed the env variable in the /bin/hubot directory. I can echo the variable and it return my API. But, I'm still getting the API not found error. Not sure what else is left todo. I'm obviously missing something I just got to figure it all out. – boostn Apr 23 '15 at 01:24
  • try doing ```HUBOT_UPTIMEROBOT_APIKEY=1234 node your_filename.js``` on the command line – Transcendence Apr 24 '15 at 00:34