如何将参数解析为hubot脚本

时间:2018-04-03 14:05:15

标签: regex coffeescript hubot

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

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

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

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

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

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

2 个答案:

答案 0 :(得分:0)

事实证明,coffeescript内置了正则表达式。所以

/order me (.*)/i

是直接的coffeescript。

要匹配正则表达式,您可以执行以下操作:

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

如果您不想忽视案例,我可以被遗漏。

答案 1 :(得分:-1)

要解析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"