简体   繁体   中英

Hubot - no arguments entered/parameters entered

I've just started using Hubot recently.

I'd like to know if a command is used, but no arguments have been entered.

robot.respond(/dothis (.*)/i, function(res) { ... };

This doesn't return anything if no arguments have been entered, even though it accepts 0 or more arguments.

robot.respond(/dothis/i, function(res) { ... };

This doesn't accept any arguments, but responds when called.

Not quite sure how to go about this, is it possible?

I think you'd need a regular expression engine that handled positive look-behinds to do this in a straightforward way, and I don't think V8 (which is what Node is using under the hood) has that as of this writing.

There are lots of other workarounds, though. Here's one using \\b which checks for a word-boundary:

  robot.respond(/dothis\b(.*)/i, function(res) { 
    if (res.match[1]) {
      res.send('We got the paramater: ' + res.match[1].trim());
    } else {
      res.send('Command called with no parameter.');
    }
  });
robot.respond(/dothis(.*)/i, function(res) { ... };

This works, that space makes all the difference. It will now take an empty string as an argument.

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