-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcomponent-chatbot.js
148 lines (100 loc) · 4.24 KB
/
component-chatbot.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require('dotenv').config( { silent : process.env.NODE_ENV === 'production' } );
const debug = require('debug')('web-components:component-chatbot');
const fs = require('fs');
const uuid = require('uuid/v4');
const bodyParser = require('body-parser');
const wires = {};
const connectionQueue = {};
const ALLOW_CORS = (req, res, next) => {
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET, POST');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
next();
};
module.exports = function(RED) {
RED.httpNode.all('/nr-component-chatbot/*', ALLOW_CORS);
RED.nodes.registerType("component-chatbot-receive", function(config){
debug('Creating node:', this, config);
RED.nodes.createNode(this, config);
const node = this;
debug('CONFIG:', config);
wires[config.id] = config.wires;
if(config.unique && config.unique !== ''){
const uniqueID = config.unique.split('/')[0];
debug(`Registering route: /nr-component-chatbot/${uniqueID}`);
RED.httpNode.post(`/nr-component-chatbot/${uniqueID}`, bodyParser.json(), function(req, res) {
debug(`Path ${req.originalUrl} hit.`);
node.updateWires(wires[node.id]);
if(process.env.ADDITIONAL_DEBUG){
debug("REQUEST:", req);
debug("REQUEST_BODY:", req.body);
}
node.send({
payload : req.body.message,
'nr-component-chatbot-id' : req.body.uuid,
params : {
context : req.body.context || {}
}
});
res.json({
status : "ok",
message : "Data received successfully"
});
});
} else {
debug(`'config' failed to meet requirements to register route. config.unique is ${config.unique}`);
}
});
RED.nodes.registerType("component-chatbot-reply", function(config){
debug('Creating node:', this, config);
RED.nodes.createNode(this, config);
const node = this;
debug('CONFIG:', config);
wires[config.id] = config.wires;
node.on('input', function(msg) {
debug('Reply node recieved input:', JSON.stringify(msg));
if(msg['nr-component-chatbot-id']){
connectionQueue[ msg[ 'nr-component-chatbot-id' ] ].messages.push(msg.payload);
}
});
RED.httpNode.get("/nr-component-chatbot/get-id", function(req, res) {
const queueUUID = uuid();
debug('New UUID for chatbot requested and created:', queueUUID);
connectionQueue[queueUUID] = {
messages : []
};
res.json({
status : "ok",
data : queueUUID
});
});
RED.httpNode.get(`/nr-component-chatbot/check-messages/:queueUUID`, function(req, res) {
debug(`Checking messages at ${req.originalUrl}`);
node.updateWires(wires[node.id]);
const queueUUID = req.params.queueUUID;
if(connectionQueue[queueUUID]){
res.json(connectionQueue[queueUUID]);
connectionQueue[queueUUID].messages = [];
} else {
debug(`Request tried to check for message queue that doesn't exist: ${req.params.queueUUID}`);
res.status(404);
res.json({
status : 'err',
message : `No message queue with id ${queueUUID} was found`
});
}
});
});
RED.httpNode.get("/web-components/chatbot", [ALLOW_CORS], function(req, res) {
fs.readFile(`${__dirname}/component-scripts/chatbot.js`, (err, data) => {
if(err){
debug('FS err:', err);
res.status(404);
res.end();
} else {
res.set('Content-Type', 'application/javascript');
res.send(data);
}
});
});
}