0

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?

Pauline Kelly
  • 91
  • 1
  • 2

2 Answers2

1

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.');
    }
  });
Trott
  • 66,479
  • 23
  • 173
  • 212
  • I tried this, but it still didn't detect on hubot when there were no arguments listed. It waits for the next command. Would there be any other workarounds? – Pauline Kelly Nov 23 '15 at 00:46
0
robot.respond(/dothis(.*)/i, function(res) { ... };

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

Pauline Kelly
  • 91
  • 1
  • 2
  • This will also respond to `dothisfoo` which may not be what you want (or it may not make any difference at all--I don't know your specific use case after all). – Trott Nov 23 '15 at 03:57