This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbotCommandTrigger.js
80 lines (66 loc) · 2.57 KB
/
botCommandTrigger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var util = require("util");
var BaseTrigger = require("./baseTrigger.js").BaseTrigger;
/*
Trigger that runs a specified callback on a bot object
matches = list of strings - the strings to trigger on (case-insensitive exact match)
exact = boolean - true to only trigger on an exact match, false to trigger on partial match
callback = callback function, passed the bot, or:
callback can also be an array where the first element is the name of
a public method of the ChatBot. The rest of the array is passed in as args
*/
var BotCommandTrigger = function() {
BotCommandTrigger.super_.apply(this, arguments);
};
util.inherits(BotCommandTrigger, BaseTrigger);
var type = "BotCommandTrigger";
exports.triggerType = type;
exports.create = function(name, chatBot, options) {
var trigger = new BotCommandTrigger(type, name, chatBot, options);
trigger.allowMessageTriggerAfterResponse = true;
trigger.respectsMute = false;
return trigger;
};
// Return true if a message was sent
BotCommandTrigger.prototype._respondToFriendMessage = function(userId, message) {
return this._triggerOnMatch(userId,userId,message);
}
// Return true if a message was sent
BotCommandTrigger.prototype._respondToChatMessage = function(roomId, chatterId, message) {
return this._triggerOnMatch(chatterId,roomId,message);
}
BotCommandTrigger.prototype._triggerOnMatch = function(fromId,toId,message) {
if (this._messageTriggers(message) && this.options.callback) {
if ('function' === typeof this.options.callback) {
if (this.options.callback.length === 1) { // check number of args
this.options.callback(this.chatBot);
} else {
this.options.callback(this.chatBot, {toId: toId, fromId: fromId, message: message});
}
} else {
if ('string' === typeof this.options.callback) {
this.options.callback = [this.options.callback];
}
var callbackArgs = this.options.callback.slice();
var callback = callbackArgs.shift();
if (typeof callback === "string" && callback.substring(0, 1) !== "_" && callback in this.chatBot
&& this.chatBot[callback].length === callbackArgs.length) {
this.chatBot[callback].apply(this.chatBot, callbackArgs);
}
}
return true;
}
return false;
}
BotCommandTrigger.prototype._messageTriggers = function(message) {
if (!message) {
return false;
}
for (var i=0; i < this.options.matches.length; i++) {
var match = this.options.matches[i];
if ((this.options.exact && message.toLowerCase() === match.toLowerCase()) ||
(!this.options.exact && message.toLowerCase().indexOf(match.toLowerCase()) >= 0)) {
return true;
}
}
return false;
}