简体   繁体   中英

How to parse parameters to a hubot script

New to hubot/coffeescript and inheriting and existing script.

I googled and found some unhelpful stuff like this: Hubot matching on multiple tokens per line?

What I want to do is be able to parse parameters to my Hubot message. For example:

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

and then follow it with what you want to order.

I can obviously re-invent the wheel and parse res.match[1] myself, but hubot already seems to have some regular expression parsing built in for its own use and I was wondering if there's a way to leverage that for my own nefarious purposes.

It turns out the coffeescript has regular expressions built in. So

/order me (.*)/i

is straight coffeescript.

To match a regular expression you can do:

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

Where the i can be left out if you don't want to ignore case.

To parse the input value in CoffeeScript you can do something like:

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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