respond
robot.respond(regex, callback)
Public: Adds a Listener that attempts to match incoming messages directed
at the robot based on a Regex. All regexes treat patterns like they begin
with a ‘^’
regex - A Regex that determines if the callback should be called.
callback - A Function that is called with a Response object.
Returns nothing.
Robot.prototype.respond = function(regex, callback) {
var modifiers, name, newRegex, pattern, re;
re = regex.toString().split('/');
re.shift();
modifiers = re.pop();
if (re[0] && re[0][0] === '^') {
console.log("Anchors don't work well with respond, perhaps you want to use 'hear'");
console.log("The regex in question was " + (regex.toString()));
}
pattern = re.join('/');
name = this.name.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
newRegex = new RegExp("^\\s*[@]?" + name + "[:,]?\\s*(?:" + pattern + ")", modifiers);
return this.listeners.push(new Listener(this, newRegex, callback));
};