简体   繁体   English

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

[英]Can I write scripts for hubot in Javascript?

Hubot is Github's chatroom robot. Hubot是Github的聊天室机器人。 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. 这是一个很棒的工具,除了我们公司没有人想写Coffeescript ....但似乎我们不能用普通的旧Javascript编写Hubot的脚本。
Is this true? 这是真的? Is there something I'm missing here? 这里有什么我想念的吗? Coffeescript is "just javascript" but I can't use Javascript with it? Coffeescript是“只是javascript”,但我不能使用Javascript吗?
EDIT 编辑
I was making 2 absurdly simple mistakes: 我犯了2个荒谬简单的错误:
- I copied the CoffeeScript comment syntax into my JS file - 我将CoffeeScript注释语法复制到我的JS文件中
- I had the script under the hubot-scripts node_module, instead of just under the /scripts/ directory in the main project. - 我在hubot-scripts node_module下有脚本,而不是在主项目的/ scripts /目录下。

Works perfectly now. 现在工作得很好。

Yes, you can write your hubot scripts in pure JavaScript. 是的,您可以使用纯JavaScript编写hubot脚本。 Following is a simple hubot script written in pure JavaScript and put under the /scripts/ directory of my customized hubot: 以下是一个简单的hubot脚本,用纯JavaScript编写,放在我定制的hubot的/scripts/目录下:

// 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");
    });
}

CoffeeScript is compiled into JavaScript, but it's not a superset of JavaScript, so JavaScript code isn't necessarily valid CoffeeScript code. CoffeeScript被编译成JavaScript,但它不是JavaScript的超集,因此JavaScript代码不一定是有效的CoffeeScript代码。

Nevertheless, after looking at the source , it looks like Hubot can accept both: 然而,在查看源代码后 ,看起来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)

This method is called by loadHubotScripts . loadHubotScripts调用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM