forked from guidone/node-red-contrib-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot-command.js
43 lines (38 loc) · 1.13 KB
/
chatbot-command.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
var _ = require('underscore');
module.exports = function(RED) {
function ChatBotCommand(config) {
RED.nodes.createNode(this, config);
var node = this;
this.command = config.command;
this.on('input', function(msg) {
var command = node.command;
var chatContext = msg.chat();
var match = command;
if (_.isEmpty(match)) {
node.error('Command is empty.');
return;
}
// add slash if missing
if (match[0] != '/') {
match = '/' + match;
}
// split command
if (msg.payload != null && _.isString(msg.payload.content)) {
var commands = msg.payload.content.split(' ');
// check
if (commands[0].replace(/(@.*?)$/, '') == match) {
// store parameters in chat context
if (commands.length > 1) {
var idx = 0;
for(idx = 1; idx < commands.length; idx++) {
chatContext.set('param' + idx, commands[idx]);
}
}
// pass through the command
node.send(msg);
}
}
});
}
RED.nodes.registerType('chatbot-command', ChatBotCommand);
};