2015-04-22 152 views
0

我是一個新的Hubot/Node.js/CoffeeScript用戶。聲明變量Coffee.Script

我正在查看Hubot的UptimeRobot腳本。當我包括在我的包,我發現了錯誤:

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) 

這裏是默認UptimeRobot.Coffee文件(位於在/ home/myhubot /腳本) http://pastebin.com/aeDgZu0B

例如,我已宣佈我的HUBOT_UPTIMEROBOT_APIKEY爲「11223344」

這是聲明全局變量的適當方式嗎?

# 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: 
# [email protected] 

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}" 

回答

1

你有HUBOT_UPTIMEROBOT_APIKEY="11223344",但你實際上並沒有使用它,因爲你也有apiKey = process.env.HUBOT_UPTIMEROBOT_APIKEY。這試圖把它從環境中拉出來。我假設你從來沒有設置任何環境變量,所以應該是未定義的。請參閱https://nodejs.org/api/process.html#process_process_env

+0

完全有道理,但是......我在Hubot討論中的任何地方都看不到此過程。我只是創建這個文件來聲明全局變量? – boostn

+0

您可能需要做一些不同的事情,如果你沒有使用bash shell – Transcendence