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?

2 Answers 2

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.');
    }
  });
1
  • 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? Nov 23, 2015 at 0: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.

1
  • 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, 2015 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.