【问题标题】:How to parse parameters to a hubot script如何将参数解析为hubot脚本
【发布时间】:2018-09-12 21:39:12
【问题描述】:

hubot/coffeescript 的新手以及继承和现有的脚本。

我用谷歌搜索了一些无用的东西,比如:Hubot matching on multiple tokens per line?

我想要做的是能够将参数解析为我的 Hubot 消息。例如:

  startPlaceOrderListener = () ->
    robot.respond /order me (.*)/i, (res) ->

然后按照您要订购的内容进行操作。

我显然可以重新发明轮子并自己解析 res.match[1],但 hubot 似乎已经内置了一些正则表达式解析供自己使用,我想知道是否有办法为我的自己的邪恶目的。

【问题讨论】:

    标签: regex coffeescript hubot


    【解决方案1】:

    原来coffeescript内置了正则表达式。所以

    /order me (.*)/i
    

    是纯咖啡文字。

    要匹配正则表达式,您可以这样做:

    /order me (.*)/i.test("Bob")
    

    如果您不想忽略大小写,可以将 i 省略。

    【讨论】:

      【解决方案2】:

      要解析CoffeeScript 中的输入值,您可以执行以下操作:

      robot.respond /open the (.*) doors/i, (res) ->
        doorType = res.match[1]
        if doorType is "pod bay"
          res.reply "I'm afraid I can't let you do that."
        else
          res.reply "Opening #{doorType} doors"
      

      【讨论】:

        猜你喜欢
        • 2018-03-26
        • 1970-01-01
        • 2017-10-02
        • 2011-02-08
        • 1970-01-01
        • 2017-04-20
        • 2013-07-25
        • 2021-12-03
        • 2017-01-21
        相关资源
        最近更新 更多