-
Notifications
You must be signed in to change notification settings - Fork 4
/
simrasa.js
143 lines (124 loc) · 6.36 KB
/
simrasa.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
/*
node-red-contrib-rasa-actionserver v1.3.0
Copyright (c) 2023 Irene Weber
MIT License (http://www.opensource.org/licenses/mit-license.php)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
module.exports = function (RED) {
"use strict";
function SimRasaNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.name = (config.name == "") ? "simrasa" : config.name;
node.action = config.action;
node.slots = config.slots;
node.errtocatch = config.errtocatch; // if set, node sends error to catch and stops
this.on("input", function (msg, send, done) {
const errors = [];
let slots = this.slots;
let action = this.action;
let errtocatch = this.errtocatch;
if (!action || action.trim() === "") {
node.warn("SimRasa Node: Action is missing. Please configure an action in the node settings.");
if (errtocatch) {
node.error("SimRasa Node: Missing action.", msg);
done(new Error("SimRasa Node: Missing action."));
return;
}
}
if (msg.__user_inject_props__ && msg.__user_inject_props__.hasOwnProperty("next_action") && msg.__user_inject_props__.hasOwnProperty("slots")) { // Array.isArray(msg.__user_inject_props__)) {
action = msg.__user_inject_props__["next_action"];
slots = msg.__user_inject_props__["slots"];
errtocatch = false;
}
delete msg.__user_inject_props__;
let evalslots = {}
slots = [...slots]
function evaluateSlot(doneEvaluating) {
if (slots.length === 0) {
doneEvaluating()
return
}
const s = slots.shift()
const sname = s.slotname;
const snamet = s["slotname-type"];
const svalue = s.slotvalue;
const svaluet = s["slotvalue-type"];
let e_sname;
let e_svalue;
let ok = true;
try {
RED.util.evaluateNodeProperty(sname, snamet, node, msg, (err, newName) => {
if (err) {
ok = false;
errors.push(err.toString())
} else if (!newName || newName.length === 0) {
ok = false;
errors.push("Missing slotname");
} else {
e_sname = newName;
RED.util.evaluateNodeProperty(svalue, svaluet, node, msg, (err, newValue) => {
if (err) {
ok = false;
errors.push(err.toString())
} else if (svaluet === 'num' && isNaN(newValue)) {
ok = false;
errors.push("Faulty Number value in slot " + e_sname);
} else if (!newValue || newValue.length === 0) {
ok = false;
errors.push("No value in slot " + e_sname);
} else {
e_svalue = newValue;
}
})
if (ok) { evalslots[e_sname] = e_svalue; }
evaluateSlot(doneEvaluating)
}
})
} catch (err) {
errors.push(err.toString());
evaluateSlot(doneEvaluating)
}
}
evaluateSlot(() => {
if (errors.length && errtocatch) {
node.error("ActionServer: " + node.name + " found errors (" + errors.join('; ') + ")", msg);
} else {
RED.util.setMessageProperty(msg, "payload.next_action", action, true);
RED.util.setMessageProperty(msg, "payload.tracker.slots", evalslots, true);
RED.util.setMessageProperty(msg, "res", "dummy", true);
RED.util.setMessageProperty(msg, "req", "dummy", true);
if (errors.length) { node.warn("ActionServer: " + node.name + " found errors (" + errors.join('; ') + ")") };
send(msg);
}
done();
})
});
}
RED.nodes.registerType("simrasa", SimRasaNode);
SimRasaNode.prototype.close = function () {
if (this.onceTimeout) {
clearTimeout(this.onceTimeout);
}
};
RED.httpAdmin.post("/inject/:id", RED.auth.needsPermission("inject.write"), function (req, res) {
var node = RED.nodes.getNode(req.params.id);
if (node != null) {
try {
if (req.body && req.body.__user_inject_props__) {
node.receive(req.body);
} else {
node.receive();
}
res.sendStatus(200);
} catch (err) {
res.sendStatus(500);
node.error(RED._("simrasa.failed", { error: err.toString() }));
}
} else {
res.sendStatus(404);
}
});
}