33

Hubot is Github's chatroom robot. It's a great tool, except that no one at our company wants to write in Coffeescript....but it appears that we can't write scripts for Hubot in plain old Javascript.
Is this true? Is there something I'm missing here? Coffeescript is "just javascript" but I can't use Javascript with it?
EDIT
I was making 2 absurdly simple mistakes:
- I copied the CoffeeScript comment syntax into my JS file
- I had the script under the hubot-scripts node_module, instead of just under the /scripts/ directory in the main project.

Works perfectly now.

4

2 回答 2

32

是的,你可以用纯 JavaScript 编写你的 hubot 脚本。以下是一个用纯 JavaScript 编写的简单 hubot 脚本,放在/scripts/我自定义的 hubot 目录下:

// Description:
//   holiday detector script
//
// Dependencies:
//   None
//
// Configuration:
//   None
//
// Commands:
//   hubot is it weekend ?  - returns whether is it weekend or not
//   hubot is it holiday ?  - returns whether is it holiday or not

module.exports = function(robot) {
    robot.respond(/is it (weekend|holiday)\s?\?/i, function(msg){
        var today = new Date();

        msg.reply(today.getDay() === 0 || today.getDay() === 6 ? "YES" : "NO");
    });
}
于 2014-08-06T20:00:06.350 回答
23

CoffeeScript 被编译成 JavaScript,但它不是 JavaScript 的超集,因此 JavaScript 代码不一定是有效的 CoffeeScript 代码。

尽管如此,在查看source之后,Hubot 似乎可以接受这两者:

  # Public: Loads a file in path.
  #
  # path - A String path on the filesystem.
  # file - A String filename in path on the filesystem.
  #
  # Returns nothing.
  loadFile: (path, file) ->
    ext  = Path.extname file
    full = Path.join path, Path.basename(file, ext)
    if ext is '.coffee' or ext is '.js'
      try
        require(full) @
        @parseHelp "#{path}/#{file}"
      catch error
        @logger.error "Unable to load #{full}: #{error.stack}"
        process.exit(1)

此方法由 调用loadHubotScripts

于 2013-03-30T20:34:40.093 回答