wobot
wobot copied to clipboard
Command event
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.
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.
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 That is a much better solution and is what I will be using on my installation.