我可以在Javascript中为hubot编写脚本吗?

Jam*_*ght 32 javascript coffeescript hubot

Hubot是Github的聊天室机器人.这是一个很棒的工具,除了我们公司没有人想写Coffeescript ....但似乎我们不能用普通的旧Javascript编写Hubot的脚本.
这是真的?这里有什么我想念的吗?Coffeescript是"只是javascript",但我不能使用Javascript吗?
编辑
我做了2个荒谬简单的错误:
- 我将CoffeeScript注释语法复制到我的JS文件中
- 我在hubot-scripts node_module下有脚本,而不是在主项目的/ scripts /目录下.

现在工作得很好.

K M*_*lam 30

是的,您可以使用纯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");
    });
}
Run Code Online (Sandbox Code Playgroud)

  • 使用此网站将您的咖啡脚本转换为js http://js2.coffee/ :) (2认同)

Ble*_*der 22

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

然而,在查看源代码后,看起来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)
Run Code Online (Sandbox Code Playgroud)

这个方法叫做loadHubotScripts.