forked from guidone/node-red-contrib-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot-topic.js
40 lines (30 loc) · 894 Bytes
/
chatbot-topic.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
var utils = require('./lib/helpers/utils');
module.exports = function(RED) {
function ChatBotTopic(config) {
RED.nodes.createNode(this, config);
var node = this;
node.rules = config.rules;
this.on('input', function(msg) {
var originalMessage = msg.originalMessage;
var chatContext = msg.chat();
var rules = node.rules;
// do nothing
if (originalMessage == null || rules.length == 0) {
return;
}
var output = [];
var currentTopic = chatContext.get('topic');
var matched = false;
rules.forEach(function(rule) {
if (!matched && utils.matchContext(currentTopic, rule.topic)) {
matched = true;
output.push(msg);
} else {
output.push(null);
}
});
node.send(output);
});
}
RED.nodes.registerType('chatbot-topic', ChatBotTopic);
};