wobot icon indicating copy to clipboard operation
wobot copied to clipboard

Command event

Open jakajancar opened this issue 13 years ago • 3 comments

Added the "command" event, which works both on channels as well as privately.

This allows one to simply do:

bot.on 'command', (message, reply) ->
    reply 'You told me to "' + message + '"'

This will make the bot accept the command both privately as well as on channels, without a separate handler.

jakajancar avatar Oct 24 '11 18:10 jakajancar

I'm not sure if the constructor is the right place for this. Feel free if you think there's a better place to put it in.

jakajancar avatar Oct 24 '11 18:10 jakajancar

I'm not sure command is the best name. Maybe take a cue from hubot and add robot.hear() and robot.respond()? I added on a similar API to wobot like so:

bot.onConnect(function () {
  // add some hubot-ish helpers
  bot.hear = function (regex, fn) {
    bot.onMessage(regex, fn);
    bot.onPrivateMessage(regex, fn);
  };

  if (!bot.mention_name) throw "What, no mention name?";
  var respondRegex = new RegExp('^@' + bot.mention_name + ' (.*)');

  bot.respond = function (regex, responseFn) {
    var onFn = function(channel, from, message, matches) {
      if (regex.test(message)) {
        responseFn(channel, from, message, matches[0].match(regex));
      }
    };

    bot.onMessage(respondRegex, onFn);
    bot.onPrivateMessage(respondRegex, onFn);
  };

  bot.random = function (array) {
    return array[Math.floor(Math.random() * array.length)];
  };
});

uxdiogenes avatar May 28 '13 04:05 uxdiogenes

@uxdiogenes That is a much better solution and is what I will be using on my installation.

mbielski avatar May 23 '17 13:05 mbielski