【问题标题】:Can I write scripts for hubot in Javascript?我可以用 Javascript 为 hubot 编写脚本吗?
【发布时间】:2013-03-21 07:43:15
【问题描述】:

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

现在完美运行。

【问题讨论】:

    标签: javascript coffeescript hubot


    【解决方案1】:

    是的,您可以使用纯 JavaScript 编写您的 hubot 脚本。下面是一个用纯 JavaScript 编写的简单 hubot 脚本,放在我自定义的 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");
        });
    }
    

    【讨论】:

      【解决方案2】:

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

      不过,在查看at the 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调用。

      【讨论】:

      • 谢谢,那个代码(我很难找到)导致我遇到了问题。我的文件放错了位置,我将 Coffeescript 注释语法复制到了我的 JS 文件中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多